-
Notifications
You must be signed in to change notification settings - Fork 10
/
WexExtManifestPlugin.js
217 lines (196 loc) · 6.73 KB
/
WexExtManifestPlugin.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const fse = require('fs-extra')
const path = require('path')
const merge = require('deepmerge')
const open = require('open')
const argv = require('yargs-parser')(process.argv.slice(2))
class WexExtManifestPlugin {
constructor (options, neutrinoOpts) {
this.options = options
this.neutrinoOpts = neutrinoOpts
}
apply (compiler) {
compiler.hooks.done.tapPromise(
'WexExtManifestPlugin',
async ({ compilation }) => {
const neutrinoManifest = {
version: this.neutrinoOpts.packageJson.version
}
compilation.entrypoints.forEach((entry, name) => {
const htmlPluginOpts = this.neutrinoOpts.mains[name]
const entryOpts = htmlPluginOpts.webext
if (!entryOpts) {
return
}
switch (entryOpts.type) {
case 'content_scripts':
{
if (!neutrinoManifest.content_scripts) {
neutrinoManifest.content_scripts = []
}
const files = entry
.getFiles()
.map(file => file.replace(/\.(css|js)\?.*$/, '.$1'))
const js = files.filter(file => file.endsWith('.js'))
const css = files.filter(file => file.endsWith('.css'))
const opt = {
...(entryOpts.manifest || {})
}
if (this.options.polyfill) {
js.unshift('assets/browser-polyfill.min.js')
}
if (js.length > 0) {
opt.js = opt.js ? [...js, ...opt.js] : js
}
if (css.length > 0) {
opt.css = opt.css ? [...css, ...opt.css] : css
}
neutrinoManifest.content_scripts.push(opt)
}
break
case 'background':
{
const scripts = entry
.getFiles()
.map(file => file.replace(/\.js\?.*$/, '.$1'))
.filter(file => file.endsWith('.js'))
if (this.options.polyfill) {
scripts.unshift('assets/browser-polyfill.min.js')
}
neutrinoManifest[entryOpts.type] = {
scripts,
...(entryOpts.manifest || {})
}
}
break
case 'browser_action':
case 'page_action':
neutrinoManifest[entryOpts.type] = {
default_popup: htmlPluginOpts.filename || `${name}.html`,
...(entryOpts.manifest || {})
}
break
case 'options_page':
neutrinoManifest[entryOpts.type] =
htmlPluginOpts.filename || `${name}.html`
break
case 'options_ui':
neutrinoManifest[entryOpts.type] = {
page: htmlPluginOpts.filename || `${name}.html`,
...(entryOpts.manifest || {})
}
break
default:
break
}
})
let commonManifest = {}
try {
commonManifest = require(path.join(
this.options.manifest,
'common.manifest'
))
} catch(e) {}
const browsers = (await fse.readdir(path.join(this.options.manifest)))
.map(fileName => (fileName.match(/^([^.]+)\.manifest/) || [, ''])[1])
.filter(browserName => browserName !== 'common')
const tmpPath = path.join(this.neutrinoOpts.root, '.webext_tmp')
await fse.remove(tmpPath)
await fse.move(this.neutrinoOpts.output, tmpPath)
for (let i = 0; i < browsers.length; i++) {
// Didn't use Promise.all here because I want
// to move the last one instead of copying which
// "I think" might be faster if there are lost of files?
// Nevertheless, it writes less to the disk this way.
const browser = browsers[i]
const browserManifest = require(path.join(
this.options.manifest,
`${browser}.manifest`
))
const finalManifest = merge.all([
neutrinoManifest,
commonManifest,
browserManifest
])
const output = path.join(this.neutrinoOpts.output, browser)
if (i !== browsers.length - 1) {
await fse.copy(tmpPath, output)
} else {
await fse.move(tmpPath, output)
}
if (argv.livereload) {
// modify manifest before emitting file
liverreloadModify(finalManifest)
await fse.copy(
path.join(__dirname, '../livereload'),
path.join(output, 'livereload')
)
}
await fse.outputJSON(
path.join(output, 'manifest.json'),
finalManifest,
{ spaces: ' ' }
)
if (this.options.polyfill) {
const polyfill = await fse.readFile(
this.options.polyfill,
'utf8'
)
if (polyfill) {
await fse.outputFile(
path.join(output, 'assets/browser-polyfill.min.js'),
this.options.removePolyfillSourcemap
? polyfill.replace('//# sourceMappingURL=browser-polyfill.min.js.map', '')
: polyfill
)
}
}
}
if (argv.livereload) {
if (typeof argv.livereload === 'string') {
const app = [argv.livereload]
if (argv.livereloadargs) {
app.push(argv.livereloadargs)
}
await open(
'http://neutrino-webextension.reloads',
{ app, background: true }
)
} else {
await open(
'http://neutrino-webextension.reloads',
{ background: true }
)
}
}
}
)
}
}
module.exports = WexExtManifestPlugin
/** Add livereload related settings */
function liverreloadModify(manifest) {
if (!manifest.background) {
manifest.background = {}
}
// for webRequestBlocking
manifest.background.persistent = true
if (!manifest.background.scripts) {
manifest.background.scripts = []
}
manifest.background.scripts.push('livereload/background.js')
if (!manifest.web_accessible_resources) {
manifest.web_accessible_resources = []
}
manifest.web_accessible_resources.push("livereload/*")
if (!manifest.permissions) {
manifest.permissions = []
}
manifest.permissions = [...new Set([
...manifest.permissions,
"*://neutrino-webextension.reloads/*",
"webRequest",
"webRequestBlocking",
// remove history
"browsingData"
])]
}