-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWorkerRunnerPlugin.js
65 lines (50 loc) · 1.66 KB
/
WorkerRunnerPlugin.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
const crypto = require('crypto');
class WorkerRunnerPlugin {
constructor(options) {
this.options = options;
if (!this.options.contentScripts || !this.options.contentScripts.length || !this.options.filename) {
throw new Error('contentScripts and filename are required.');
}
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const files = this.options.contentScripts.map((file) => {
const match = Object.keys(compilation.assets).find((asset) => asset.match(file));
if (!match) {
throw new Error('No file found.');
}
return match
});
const source = `
self.window = self;
${files.map((file) => `self.importScripts('/${file}');`).join('\n')}
`;
const name = this.options.filename.split('.').map((part) => {
if (part !== '[hash]') {
return part;
}
const hash = crypto.createHash('sha256');
hash.update(source, 'utf-8');
return hash.digest('hex').slice(0, 20)
}).join('.');
compilation.assets[name] = {
source: () => source,
size: () => source.length
};
if (this.options.replaceFilename === this.options.filename) {
return callback();
}
Object.keys(compilation.assets).forEach((key) => {
if (!this.options.replacer(key)) {
return
}
const file = compilation.assets[key];
const source = file.source().replace(this.options.replaceFilename, name);
file.source = () => source;
file.length = () => source.length;
});
callback();
});
}
}
module.exports = WorkerRunnerPlugin;