-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.39 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
const HtmlWebpackPlugin = require('html-webpack-plugin');
const defaultTagName = 'div';
const defaultTagId = 'app';
const configWarning = 'HtmlWebpackElementPlugin: Invalid configuration. Using default options.';
class HtmlWebpackElementPlugin {
constructor(options) {
let pluginOptions = options;
if (typeof options !== 'object') {
let tagId;
if (typeof options === 'string') {
tagId = options;
} else if (typeof options !== 'undefined') {
this.configWarning = configWarning;
}
pluginOptions = {
tagName: defaultTagName,
tagId: tagId || defaultTagId,
};
}
this.tagName = pluginOptions.tagName || defaultTagName;
this.tagId = pluginOptions.tagId || defaultTagId;
}
apply(compiler) {
compiler.hooks.compilation.tap('html-webpack-element-plugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
'html-webpack-element-plugin',
(data, cb) => {
const { html } = data;
const index = html.indexOf('<body>') + '<body>'.length;
const [first, last] = [html.slice(0, index), html.slice(index, html.length)];
const newData = {
html: `${first}<${this.tagName} id="${this.tagId}"></${this.tagName}>${last}`,
};
cb(null, newData);
},
);
});
}
}
module.exports = HtmlWebpackElementPlugin;