UNPKG

983 BJavaScriptView Raw
1class InlineUmdHtmlPlugin {
2 constructor(htmlWebpackPlugin) {
3 this.htmlWebpackPlugin = htmlWebpackPlugin
4 }
5
6 getInlinedTag(inject, injectLink) {
7 return injectLink
8 .filter(tag => tag.inject == inject)
9 .map(tag => ({
10 tagName: 'script',
11 attributes: { src: tag.onlineUrl },
12 closeTag: true
13 }))
14 }
15
16 apply(compiler) {
17 compiler.hooks.afterCompile.tap(this.constructor.name, compilation => {
18 const { injectLink } = compiler.options
19
20 if (!injectLink || !injectLink.length) return
21
22 const headTags = this.getInlinedTag('head', injectLink)
23 const bodyTags = this.getInlinedTag('body', injectLink)
24
25 const hooks = this.htmlWebpackPlugin.getHooks(compilation)
26 hooks.alterAssetTagGroups.tap(this.constructor.name, assets => {
27 assets.headTags = headTags.concat(assets.headTags)
28 assets.bodyTags = bodyTags.concat(assets.bodyTags)
29 })
30 })
31 }
32}
33
34module.exports = InlineUmdHtmlPlugin