-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
46 lines (37 loc) · 1.08 KB
/
index.ts
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
import {Compiler} from 'webpack'
import HtmlWebpackPlugin, { HtmlTagObject } from 'html-webpack-plugin'
interface AlterAssetTags {
assetTags: {
scripts: HtmlTagObject[];
styles: HtmlTagObject[];
meta: HtmlTagObject[];
};
outputName: string;
plugin: HtmlWebpackPlugin;
}
interface Options {
[x:string]: string
}
class HtmlWebpackScriptAttributesPlugin {
constructor(options?: Options) {
this.options = Object.assign({}, this.defaultOptions, options)
}
defaultOptions:Options = {}
options:Options = {}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('html-webapck-script-attributes-plugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('html-webapck-script-attributes-plugin', this.run)
})
}
run = (data: AlterAssetTags, cb: any) => {
if (this.options) {
data.assetTags.scripts.map((script) => {
for (const key in this.options) {
script.attributes[key] = this.options[key]
}
})
}
cb(null, data)
}
}
export default HtmlWebpackScriptAttributesPlugin