UNPKG

1.54 kBJavaScriptView Raw
1const appPaths = require('../app-paths')
2
3function makeTag (tagName, attributes, closeTag = false) {
4 return {
5 tagName,
6 attributes,
7 closeTag
8 }
9}
10
11function makeScriptTag (innerHTML) {
12 return {
13 tagName: 'script',
14 closeTag: true,
15 innerHTML
16 }
17}
18
19function fillHtmlTags (data, cfg) {
20 if (cfg.build.appBase) {
21 data.head.push(
22 makeTag('base', { href: cfg.build.appBase })
23 )
24 }
25}
26
27module.exports.fillHtmlTags = fillHtmlTags
28
29module.exports.plugin = class HtmlAddonsPlugin {
30 constructor (cfg = {}) {
31 this.cfg = cfg
32 }
33
34 apply (compiler) {
35 compiler.hooks.compilation.tap('webpack-plugin-html-addons', compilation => {
36 compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('webpack-plugin-html-addons', (data, callback) => {
37 fillHtmlTags(data, this.cfg)
38
39 if (this.cfg.ctx.mode.cordova) {
40 data.body.unshift(
41 makeTag('script', { src: 'cordova.js' }, true)
42 )
43 }
44 else if (this.cfg.ctx.mode.electron && this.cfg.ctx.prod) {
45 // set statics path in production;
46 // the reason we add this is here is because the folder path
47 // needs to be evaluated at runtime
48 const bodyScript = `
49 window.__statics = require('path').join(__dirname, 'statics').replace(/\\\\/g, '\\\\');
50 `
51 data.body.push(
52 makeScriptTag(bodyScript)
53 )
54 }
55
56 // finally, inform Webpack that we're ready
57 callback(null, data)
58 })
59 })
60 }
61}