From 71c52e1371c410f6df8578a09e52a2cf8b440111 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 28 May 2020 21:52:13 +0800 Subject: [PATCH] build: overwrite pjax-api on install --- package.json | 2 +- scripts/fix-pjax-api.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 scripts/fix-pjax-api.js diff --git a/package.json b/package.json index bfc74c8..ae00556 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "start": "VERSION=dev-v$(node scripts/get-version.js) webpack --watch", "debug-firefox": "web-ext run -s dist", "analyse-bundle": "ANALYSE= NODE_ENV=production webpack", - "postinstall": "rm -rf node_modules/@types/react-native", + "postinstall": "rm -rf node_modules/@types/react-native && node scripts/fix-pjax-api", "build": "VERSION=v$(node scripts/get-version.js) NODE_ENV=production webpack", "roll": "make release" }, diff --git a/scripts/fix-pjax-api.js b/scripts/fix-pjax-api.js new file mode 100644 index 0000000..6a638ad --- /dev/null +++ b/scripts/fix-pjax-api.js @@ -0,0 +1,36 @@ +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) + if (source.includes(original)) { + throw new Error(`More than one original string found`, JSON.stringify(original)) + } + } else { + throw new Error(`Original string not found`, JSON.stringify(original)) + } + } + + return source +} + +async function fixPJAXAPI() { + const pairs = [ + [ + `void xhr.open(method, requestURL.path, true);`, + `void xhr.open(method, requestURL.reference, true);`, + ], + [ + `this.document = this.xhr.responseType === 'document' ? this.xhr.responseXML.cloneNode(true) : html_1.parse(this.xhr.responseText).extract();`, + `this.document = this.xhr.responseType === 'document' ? this.xhr.responseXML : html_1.parse(this.xhr.responseText).extract();`, + ], + ] + 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) + fs.writeFile(filePath, modified, 'utf-8') +} + +fixPJAXAPI()