From 72e1a3738634b47829c88ca3ccc915cd87900588 Mon Sep 17 00:00:00 2001 From: oToToT Date: Mon, 29 Mar 2021 03:24:02 +0800 Subject: [PATCH] Split browser emulation --- index.js | 51 ++++++++++++++++++++++++----------------------- package-lock.json | 4 +--- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index d5b4939..5f8b576 100644 --- a/index.js +++ b/index.js @@ -7,29 +7,32 @@ const through = require('through2'); const { JSDOM } = require('jsdom'); const { replace } = require('feather-icons'); -module.exports = (attrs = {}) => through.obj(function (file, encoding, cb) { +function emulateBrowser(dom, execFunc) { const savedVariables = new Map(); - function emulateBrowser(dom) { - // override document and DOMParser - if (typeof global.document !== 'undefined') { - savedVariables.set('document', global.document); - } - global.document = dom.window.document; - if (typeof global.DOMParser !== 'undefined') { - savedVariables.set('DOMParser', global.DOMParser); - } - global.DOMParser = dom.window.DOMParser; + // override document and DOMParser + if (typeof global.document !== 'undefined') { + savedVariables.set('document', global.document); } - function restoreVariables() { - // restore overrided variables - if (savedVariables.has('document')) { - global.document = savedVariables.get('document'); - } - if (savedVariables.has('DOMParser')) { - global.DOMParser = savedVariables.get('DOMParser'); - } + global.document = dom.window.document; + if (typeof global.DOMParser !== 'undefined') { + savedVariables.set('DOMParser', global.DOMParser); + } + global.DOMParser = dom.window.DOMParser; + + execFunc(); + + // restore overrided variables + if (savedVariables.has('document')) { + global.document = savedVariables.get('document'); + } + if (savedVariables.has('DOMParser')) { + global.DOMParser = savedVariables.get('DOMParser'); } +} + +module.exports = (attrs = {}) => through.obj(function (file, encoding, cb) { + if (file.isNull()) { // nothing to do @@ -46,12 +49,9 @@ module.exports = (attrs = {}) => through.obj(function (file, encoding, cb) { if (file.isBuffer()) { // create dom from html file const dom = new JSDOM(file.contents, { runScripts: 'outside-only' }); - // start emulation - emulateBrowser(dom); - // replace feathericons - replace(attrs); - // stop emulation - restoreVariables(); + + // replace feathericons in an emulated browser environment + emulateBrowser(dom, ()=>replace(attrs)); // output to file contents const HTMLResult = dom.serialize(); @@ -65,6 +65,7 @@ module.exports = (attrs = {}) => through.obj(function (file, encoding, cb) { return cb(null, file); } + // this should be unreachable const err = new PluginError(PLUGIN_NAME, 'Unexpected type of input'); this.emit('error', err); return cb(err); diff --git a/package-lock.json b/package-lock.json index 6abdcf3..4c1da62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -714,7 +714,6 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", - "fsevents": "~2.1.2", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -1170,8 +1169,7 @@ "esprima": "^4.0.1", "estraverse": "^4.2.0", "esutils": "^2.0.2", - "optionator": "^0.8.1", - "source-map": "~0.6.1" + "optionator": "^0.8.1" }, "bin": { "escodegen": "bin/escodegen.js",