From dd3f611db8fe415975b50bf1157b599fbe2c7d43 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 19 May 2022 00:01:21 +0800 Subject: [PATCH] fix: solve compatibility issue in Firefox --- package.json | 5 +- scripts/fix-deps/index.js | 43 +++++++++++++++++ scripts/fix-deps/pjax-api.js | 45 +++++++++++++++++ scripts/fix-deps/styled-components.js | 20 ++++++++ scripts/fix-pjax-api.js | 69 --------------------------- 5 files changed, 112 insertions(+), 70 deletions(-) create mode 100644 scripts/fix-deps/index.js create mode 100644 scripts/fix-deps/pjax-api.js create mode 100644 scripts/fix-deps/styled-components.js delete mode 100644 scripts/fix-pjax-api.js diff --git a/package.json b/package.json index b07075f..509beca 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "dev-safari": "TARGET=safari yarn run dev", "debug-firefox": "web-ext run --source-dir=dist --firefox-profile=firefox-profile --profile-create-if-missing --keep-profile-changes --start-url github.com/EnixCoda/Gitako", "analyse-bundle": "ANALYSE= NODE_ENV=production webpack", - "postinstall": "node scripts/fix-pjax-api", + "postinstall": "node scripts/fix-deps", "build": "VERSION=v$(node scripts/get-version.js) NODE_ENV=production webpack", "roll": "make release", "test": "yarn run test:parallel && yarn run test:non-parallel", @@ -109,6 +109,9 @@ "plugin:react/recommended", "plugin:react-hooks/recommended" ], + "ignorePatterns": [ + "scripts" + ], "rules": { "@typescript-eslint/ban-types": "off" } diff --git a/scripts/fix-deps/index.js b/scripts/fix-deps/index.js new file mode 100644 index 0000000..ec1cc9c --- /dev/null +++ b/scripts/fix-deps/index.js @@ -0,0 +1,43 @@ +const fs = require('fs').promises +const path = require('path') + +/** + * This script rewrites local dependency files to resolve compatibility issues. + * This is a bit dirty but really effective. + */ + +function modify(source = '', pairs = []) { + for (const [original, replace] of pairs) { + if (source.includes(original)) { + source = source.replace(original, replace) + } else { + throw new Error(`Original string not found: ${JSON.stringify(original)}`) + } + + if (source.includes(original)) { + throw new Error(`More than one original string found`, JSON.stringify(original)) + } + } + + return source +} + +const nodeModulesPath = path.resolve(__dirname, '../../', `node_modules`) + +exports.fixDep = async function fixDep(targetFilePath, pairs) { + const filePath = path.resolve(nodeModulesPath, targetFilePath) + const source = await fs.readFile(filePath, 'utf-8') + const modified = modify(source, pairs,) + await fs.writeFile(filePath, modified, 'utf-8') +} + +async function fixDeps() { + for (const fix of [ + require('./pjax-api').fix, + require('./styled-components').fix, + ]) { + await fix() + } +} + +fixDeps() diff --git a/scripts/fix-deps/pjax-api.js b/scripts/fix-deps/pjax-api.js new file mode 100644 index 0000000..e2f1582 --- /dev/null +++ b/scripts/fix-deps/pjax-api.js @@ -0,0 +1,45 @@ +const { fixDep } = require('.') + +const targetFilePath = `pjax-api/dist/pjax-api.js`; +const pairs = [ + // Firefox + [ + `void xhr.open(method, requestURL.path, true);`, + `void xhr.open(method, requestURL.reference, true);`, + ], + // Firefox + [ + `this.document = this.xhr.responseXML.cloneNode(true);`, + `this.document = this.xhr.responseXML;`, + ], + // Chrome: modifying cross-context history state causes troubles + // Scroll position can still be restored without this function + [ + ` + function savePosition() { + var _a; + void window.history.replaceState({ + ...window.history.state, + position: { + ...(_a = window.history.state) === null || _a === void 0 ? void 0 : _a.position, + top: window.pageYOffset, + left: window.pageXOffset + } + }, document.title); + }`, + ` + function savePosition() { + return; + }`, + ], +] + +exports.fix = async () => { + try { + await fixDep(targetFilePath, pairs) + } catch (err) { + console.error((err && err.message) || err) + const shouldTerminate = process.env.IGNORE_FIX_PJAX_API_FAILURE !== 'true' + if (shouldTerminate) process.exit(1) + } +} diff --git a/scripts/fix-deps/styled-components.js b/scripts/fix-deps/styled-components.js new file mode 100644 index 0000000..b3022cf --- /dev/null +++ b/scripts/fix-deps/styled-components.js @@ -0,0 +1,20 @@ +const { fixDep } = require('.') + +const targetFilePath = `styled-components/dist/styled-components.browser.esm.js`; +const pairs = [ + // Firefox + // disable production check in `checkDynamicCreation` + [ + `function(e,t){if("production"!==process.env.NODE_ENV)`, + `function(e,t){if(false)`, + ], +] + +exports.fix = async () => { + try { + await fixDep(targetFilePath, pairs) + } catch (err) { + console.error((err && err.message) || err) + process.exit(1) + } +} diff --git a/scripts/fix-pjax-api.js b/scripts/fix-pjax-api.js deleted file mode 100644 index b2b8b4a..0000000 --- a/scripts/fix-pjax-api.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * This script rewrites code of pjax-api to resolve compatibility issues. - * This is a bit dirty but really effective. - */ -const fs = require('fs').promises -const path = require('path') - -function modify(source = '', pairs = []) { - for (const [original, replace] of pairs) { - if (source.includes(original)) { - source = source.replace(original, replace) - } else { - throw new Error(`Original string not found: ${JSON.stringify(original)}`) - } - - if (source.includes(original)) { - throw new Error(`More than one original string found`, JSON.stringify(original)) - } - } - - return source -} - -async function fixPJAXAPI(loose) { - const pairs = [ - // Firefox - [ - `void xhr.open(method, requestURL.path, true);`, - `void xhr.open(method, requestURL.reference, true);`, - ], - // Firefox - [ - `this.document = this.xhr.responseXML.cloneNode(true);`, - `this.document = this.xhr.responseXML;`, - ], - // Chrome: modifying cross-context history state causes troubles - // Scroll position can still be restored without this function - [ - ` - function savePosition() { - var _a; - void window.history.replaceState({ - ...window.history.state, - position: { - ...(_a = window.history.state) === null || _a === void 0 ? void 0 : _a.position, - top: window.pageYOffset, - left: window.pageXOffset - } - }, document.title); - }`, - ` - function savePosition() { - return; - }`, - ], - ] - try { - const filePath = path.resolve(__dirname, '..', `node_modules/pjax-api/dist/pjax-api.js`) - const source = await fs.readFile(filePath, 'utf-8') - const modified = modify(source, pairs, loose) - await fs.writeFile(filePath, modified, 'utf-8') - } catch (err) { - console.error((err && err.message) || err) - const shouldTerminate = process.env.IGNORE_FIX_PJAX_API_FAILURE !== 'true' - if (shouldTerminate) process.exit(1) - } -} - -fixPJAXAPI()