-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
72 lines (48 loc) · 2.08 KB
/
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
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
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Partial = require('./lib/partial');
const Util = require('./lib/util');
/**
* HtmlWebpackPartialsPlugin
* @description Webpack plugin based on HTML Webpack Plugin that allows partial injection into the compiled HTML
*/
class HtmlWebpackPartialsPlugin {
constructor(settings = {}) {
this.settings = settings;
}
apply(compiler) {
compiler.hooks.compilation.tap('HtmlWebpackPartialsPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).afterTemplateExecution.tapAsync('HtmlWebpackPartialsPlugin', (data, callback) => {
// If the input isn't an array, add it as one to simplify the process
if ( !Array.isArray(this.settings) ) {
this.settings = [ this.settings ];
}
const partial_collection = this.settings.map(partial => {
return new Partial(partial);
}).filter(partial => {
// User option to conditionally inject snippet to allow for config based
// injection management. Additionally check to see if the partial template
// filename matches the current HTML Webpack Plugin instance. This defaults
// to index.html if not set
return partial.should_inject && (
partial.template_filename === data.plugin.options.filename ||
Array.isArray(partial.template_filename)
? partial.template_filename.includes(data.plugin.options.filename)
: partial.template_filename === '*'
);
}).forEach(partial => {
// Once we know we're using the partial, read the file and create a template
partial.createTemplate();
// Finally inject the partial into the HTML stream
data.html = Util.injectPartial(data.html, {
options: partial.options,
html: partial.template(partial.options),
priority: partial.priority,
location: partial.location,
});
});
callback(null, data);
});
});
}
}
module.exports = HtmlWebpackPartialsPlugin;