1 | const appPaths = require('../app-paths')
|
2 |
|
3 | function makeTag (tagName, attributes, closeTag = false) {
|
4 | return {
|
5 | tagName,
|
6 | attributes,
|
7 | closeTag
|
8 | }
|
9 | }
|
10 |
|
11 | function makeScriptTag (innerHTML) {
|
12 | return {
|
13 | tagName: 'script',
|
14 | closeTag: true,
|
15 | innerHTML
|
16 | }
|
17 | }
|
18 |
|
19 | function fillHtmlTags (data, cfg) {
|
20 | if (cfg.build.appBase) {
|
21 | data.head.push(
|
22 | makeTag('base', { href: cfg.build.appBase })
|
23 | )
|
24 | }
|
25 | }
|
26 |
|
27 | module.exports.fillHtmlTags = fillHtmlTags
|
28 |
|
29 | module.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 |
|
46 |
|
47 |
|
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 |
|
57 | callback(null, data)
|
58 | })
|
59 | })
|
60 | }
|
61 | }
|