-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
30 lines (25 loc) · 971 Bytes
/
index.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
function AddAssetsWebpackPlugin(options) {
this.options = options;
}
AddAssetsWebpackPlugin.prototype.apply = function(compiler) {
const assetsToAdd = Array.isArray(this.options)
? this.options
: [this.options];
compiler.plugin("emit", function(compilation, callback) {
assetsToAdd.forEach(assetToAdd => {
const hasFilePath = assetToAdd.filePath != null;
const hasContent = assetToAdd.content != null;
if (!hasFilePath || !hasContent) {
const optionNeeded = !hasContent ? "content" : "filePath";
const message = `AddAssetsWebpackPlugin Error: The '${optionNeeded}' option must be passed`;
return console.error("\n\n\x1b[41m", message, "\x1b[0m", "\n");
}
compilation.assets[assetToAdd.filePath] = {
source: () => new Buffer(assetToAdd.content),
size: () => Buffer.byteLength(assetToAdd.content)
};
});
callback();
});
};
module.exports = AddAssetsWebpackPlugin;