-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 2 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
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
class HtmlWebpackBasePathPlugin {
_processReplacePath(basePath, obj) {
if (obj.attributes.src)
obj.attributes.src = path.join(basePath, path.basename(obj.attributes.src));
else if (obj.attributes.href)
obj.attributes.href = path.join(basePath, path.basename(obj.attributes.href));
}
apply(compiler) {
// HtmlWebpackPlugin version 4.0.0-beta.5
if (HtmlWebpackPlugin.getHooks) {
compiler.hooks.compilation.tap('HtmlWebpackInjectorPlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync(
'HtmlWebpackInjectorPlugin', (data, callback) => {
let basePath = data.plugin.options.basePath || false;
data.bodyTags.forEach((obj) => {
if (basePath) this._processReplacePath(basePath, obj);
});
data.headTags.forEach((obj) => {
if (basePath) this._processReplacePath(basePath, obj);
});
callback(null, data)
}
)
});
} else {
// HtmlWebpackPlugin version 3.2.0
compiler.plugin("compilation", compilation => {
compilation.plugin("html-webpack-plugin-alter-asset-tags", data => {
let basePath = data.plugin.options.basePath || false;
data.bodyTags.forEach((obj) => {
if (basePath) this._processReplacePath(basePath, obj);
});
data.headTags.forEach((obj) => {
if (basePath) this._processReplacePath(basePath, obj);
});
});
});
}
}
}
module.exports = HtmlWebpackBasePathPlugin;