forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.html-addons.js
More file actions
53 lines (44 loc) · 1.19 KB
/
plugin.html-addons.js
File metadata and controls
53 lines (44 loc) · 1.19 KB
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
const HtmlWebpackPlugin = require('html-webpack-plugin')
function makeTag (tagName, attributes, closeTag = false) {
return {
tagName,
attributes,
closeTag
}
}
function makeScriptTag (innerHTML) {
return {
tagName: 'script',
closeTag: true,
innerHTML
}
}
function fillBaseTag (html, base) {
return html.replace(
/(<head[^>]*)(>)/i,
(_, start, end) => `${start}${end}<base href="${base}">`
)
}
module.exports.fillBaseTag = fillBaseTag
module.exports.plugin = class HtmlAddonsPlugin {
constructor (cfg = {}) {
this.cfg = cfg
}
apply (compiler) {
compiler.hooks.compilation.tap('webpack-plugin-html-addons', compilation => {
const hooks = HtmlWebpackPlugin.getHooks(compilation)
hooks.afterTemplateExecution.tapAsync('webpack-plugin-html-addons', (data, callback) => {
if (this.cfg.build.appBase) {
data.html = fillBaseTag(data.html, this.cfg.build.appBase)
}
if (this.cfg.ctx.mode.cordova) {
data.bodyTags.unshift(
makeTag('script', { src: 'cordova.js' }, true)
)
}
// finally, inform Webpack that we're ready
callback(null, data)
})
})
}
}