fix: solve compatibility issue in Firefox

This commit is contained in:
EnixCoda 2022-05-19 00:01:21 +08:00
parent 536d191fb8
commit dd3f611db8
5 changed files with 112 additions and 70 deletions

View file

@ -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"
}

43
scripts/fix-deps/index.js Normal file
View file

@ -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()

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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()