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
63 lines (56 loc) · 1.71 KB
/
plugin.html-addons.js
File metadata and controls
63 lines (56 loc) · 1.71 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
54
55
56
57
58
59
60
61
62
63
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,
(found, 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 => {
if (this.cfg.build.publicPath) {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('webpack-plugin-html-base-tag', (data, callback) => {
data.html = fillBaseTag(data.html, this.cfg.build.publicPath)
callback(null, data)
})
}
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('webpack-plugin-html-addons', (data, callback) => {
if (this.cfg.ctx.mode.cordova) {
data.body.unshift(
makeTag('script', { src: 'cordova.js' }, true)
)
}
else if (this.cfg.ctx.mode.electron && this.cfg.ctx.prod) {
// set statics path in production;
// the reason we add this is here is because the folder path
// needs to be evaluated at runtime
const bodyScript = `
window.__statics = require('path').join(__dirname, 'statics').replace(/\\\\/g, '\\\\');
`
data.body.push(
makeScriptTag(bodyScript)
)
}
// finally, inform Webpack that we're ready
callback(null, data)
})
})
}
}