-
-
Notifications
You must be signed in to change notification settings - Fork 729
/
firefox-fix.js
125 lines (104 loc) · 3.09 KB
/
firefox-fix.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Firefox does not support dynamic import in WebExtension
// https://bugzilla.mozilla.org/show_bug.cgi?id=1536094
const path = require('path')
const fs = require('fs-extra')
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const ffPath = path.join(__dirname, '../build/firefox')
const manifestPath = path.join(ffPath, 'manifest.json')
const manifest = require(manifestPath)
main()
async function main() {
const htmls = (await fs.readdir(ffPath)).filter(name =>
name.endsWith('.html')
)
const htmlTexts = await Promise.all(
htmls.map(html => fs.readFile(path.join(ffPath, html), 'utf8'))
)
const staticChunkIds = await getStaticChunks(htmlTexts)
const allChunks = await fs.readdir(path.join(ffPath, 'assets'))
const dynamicChunks = allChunks
.filter(name => {
const m = /^([^.]+)\.[^.]+\.js$/.exec(name)
if (m) {
return !staticChunkIds.has(m[1])
}
return false
})
.map(filename => `/assets/${filename}`)
const dynamicChunksWithoutAntd = dynamicChunks.filter(
name => !name.startsWith('/assets/antd')
)
manifest.content_scripts.forEach(item => {
if (item.js) {
if (item.js.some(name => name.startsWith('assets/selection'))) {
return
}
item.js.push(...dynamicChunksWithoutAntd)
}
})
manifest.background.scripts.push(...dynamicChunksWithoutAntd)
await fs.outputJSON(manifestPath, manifest, { spaces: 2 })
await Promise.all(
htmlTexts.map((text, i) => {
const dom = new JSDOM(text)
let chunks = dynamicChunksWithoutAntd
if (
htmls[i] === 'options.html' ||
htmls[i] === 'notebook.html' ||
htmls[i] === 'history.html'
) {
chunks = dynamicChunks
}
chunks.forEach(name => {
const script = dom.window.document.createElement('script')
script.src = name
dom.window.document.head.appendChild(script)
})
return fs.outputFile(
path.join(ffPath, htmls[i]),
dom.window.document.documentElement.outerHTML
)
})
)
// urgh
// https://github.com/mozilla/addons-linter/issues/2498
const runtime = allChunks.find(filename => filename.startsWith('runtime.'))
const runtimePath = path.join(ffPath, 'assets', runtime)
await fs.outputFile(
runtimePath,
(await fs.readFile(runtimePath, 'utf8')).replace(
/import\(/g,
'saladictImport('
)
)
}
async function getStaticChunks(htmls) {
const staticChunks = new Set()
htmls.forEach(text => {
const matcher = /"\/assets\/([^.]+)\.[^.]+\.js"/g
let m
// eslint-disable-next-line no-cond-assign
while ((m = matcher.exec(text))) {
staticChunks.add(m[1])
}
})
manifest.content_scripts.forEach(item => {
if (item.js) {
item.js.forEach(name => {
const m = /assets\/([^.]+)\.[^.]+\.js/.exec(name)
if (m) {
staticChunks.add(m[1])
}
})
}
})
manifest.background.scripts.forEach(name => {
const m = /assets\/([^.]+)\.[^.]+\.js/.exec(name)
if (m) {
staticChunks.add(m[1])
}
})
staticChunks.delete('franc')
return staticChunks
}