forked from AlexKlimenkov/wrapper-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper-webpack-plugin.js
76 lines (65 loc) · 2.74 KB
/
wrapper-webpack-plugin.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
'use strict';
const { Compilation } = require('webpack');
const ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers");
class WrapperPlugin {
/**
* @param {Object} args
* @param {string | Function} [args.header] Text that will be prepended to an output file.
* @param {string | Function} [args.footer] Text that will be appended to an output file.
* @param {string | RegExp} [args.test] Tested against output file names to check if they should be affected by this
* plugin.
* @param {boolean} [args.afterOptimizations=false] Indicating whether this plugin should be activated before
* (`false`) or after (`true`) the optimization stage. Example use case: Set this to true if you want to avoid
* minification from affecting the text added by this plugin.
*/
constructor(args) {
if (typeof args !== 'object') {
throw new TypeError('Argument "args" must be an object.');
}
this.header = args.hasOwnProperty('header') ? args.header : '';
this.footer = args.hasOwnProperty('footer') ? args.footer : '';
this.afterOptimizations = args.hasOwnProperty('afterOptimizations') ? !!args.afterOptimizations : false;
this.test = args.hasOwnProperty('test') ? args.test : '';
}
apply(compiler) {
const WebpackSources = compiler.webpack.sources;
const ConcatSource = WebpackSources.ConcatSource;
const header = this.header;
const footer = this.footer;
const tester = {test: this.test};
compiler.hooks.compilation.tap('WrapperPlugin', (compilation) => {
if (this.afterOptimizations) {
compilation.hooks.processAssets.tap({ name: 'WrapperPlugin', stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE }, (assets) => {
wrapChunks(compilation, assets, footer, header);
});
} else {
compilation.hooks.processAssets.tapAsync({ name: 'WrapperPlugin', stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE }, (assets, done) => {
wrapChunks(compilation, assets, footer, header);
done();
});
}
});
function wrapFile(compilation, assets, fileName) {
const headerContent = (typeof header === 'function') ? header(fileName) : header;
const footerContent = (typeof footer === 'function') ? footer(fileName) : footer;
assets[fileName] = new ConcatSource(
String(headerContent),
assets[fileName],
String(footerContent),
);
}
function wrapChunks(compilation, assets) {
for (const fileName of Object.keys(assets)) {
// if (!chunk.rendered) {
// // Skip already rendered (cached) chunks
// // to avoid rebuilding unchanged code.
// continue;
// }
if (ModuleFilenameHelpers.matchObject(tester, fileName)) {
wrapFile(compilation, assets, fileName);
}
}
} // wrapChunks
}
}
module.exports = WrapperPlugin;