-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlWorkerScriptPlugin.js
37 lines (31 loc) · 1.46 KB
/
HtmlWorkerScriptPlugin.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
class HtmlWorkerScriptPlugin {
apply(compiler) {
compiler.hooks.compilation.tap('HtmlWorkerScriptPlugin', (compilation) => {
// Hook into HtmlWebpackPlugin's emit stage
const HtmlWebpackPlugin = compiler.options.plugins.find((plugin) => plugin.constructor.name === 'HtmlWebpackPlugin');
if (HtmlWebpackPlugin) {
HtmlWebpackPlugin.constructor.getHooks(compilation).beforeEmit.tapAsync('HtmlWorkerScriptPlugin', (data, callback) => {
console.log('HtmlWorkerScriptPlugin modifying HTML asset');
const jsAsset = compilation.assets['index.js'];
if (jsAsset) {
const jsCode = jsAsset.source();
// Append inline script to the end of the html
data.html += `\n<script type="text/worker">${jsCode}</script>`;
// Replace placeholders and clean up
const currentDate = new Date().toUTCString();
data.html = data.html.replace('$$CURRENTRELEASEDATE$$', currentDate);
if (process.env.NODE_ENV === 'production') {
data.html = data.html.replace(/^\s*<!--DEVELOPMENT CODE\. NOT READY FOR PRODUCTION YET-->\s*\r?\n?/gm, '');
}
} else {
console.error('HtmlWorkerScriptPlugin: JavaScript asset not found.');
}
callback(null, data);
});
} else {
console.error('HtmlWorkerScriptPlugin: HtmlWebpackPlugin not found.');
}
});
}
}
module.exports = HtmlWorkerScriptPlugin;