forked from polywock/globalSpeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceCtx.js
31 lines (23 loc) · 1.03 KB
/
replaceCtx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Must be done after Webpack builds the ctx.js and contentScript.js file.
// Replaces $$$CTX$$$ placeholder within contentScript.js to ctx.js code.
const { readFileSync, writeFileSync } = require("fs")
const { env } = require("process")
const unpackedRoot = `${env.FIREFOX ? "buildFf" : "build"}/unpacked`
let csPath = `${unpackedRoot}/contentScript.js`
let cs = readFileSync(csPath, {encoding: "utf8"})
let ctx = readFileSync(`${unpackedRoot}/ctx.js`, {encoding: "utf8"})
let placeholderCount = 0
// In loop, since if webpack is being built for development their may be multiple copies.
// In production mode, only 1 copy should remain.
while (true) {
if (cs.indexOf("$$$CTX$$$") === -1) {
if (!placeholderCount) {
throw Error("This shouldn't happen. Could not find $$$CTX$$$ placeholder.")
}
break
}
cs = cs.replace("$$$CTX$$$", JSON.stringify(ctx).slice(1, -1))
placeholderCount++
}
writeFileSync(csPath, cs, {encoding: "utf8", flags: "w+"})
console.log(`REPLACED $$$CTX$$$ PLACEHOLDER (${placeholderCount})`)