-
Notifications
You must be signed in to change notification settings - Fork 10
/
WexExtManifestPlugin.js
171 lines (150 loc) · 4.91 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
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))
const manifestV3Compilation = require('./manifestV3');
const manifestV2Compilation = require('./manifestV2');
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
}
let commonManifest = {}
try {
commonManifest = require(path.join(
this.options.manifest,
'common.manifest'
))
} catch(e) {}
switch (commonManifest.manifest_version) {
case 3:
manifestV3Compilation(this, compilation, neutrinoManifest)
break
case 2:
default:
manifestV2Compilation(this, compilation, neutrinoManifest)
break
}
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,
manifest.version === 2 && "*://neutrino-webextension.reloads/*",
"webRequest",
"webRequestBlocking",
// remove history
"browsingData"
])]
if (manifest.version === 3) {
if (!manifest.host_permissions) {
manifest.host_permissions = []
}
manifest.host_permissions = [
...new Set([
...manifest.host_permissions,
"*://neutrino-webextension.reloads/*"
])
]
}
}