|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 | let js = [];
|
3 | 3 | let run = 0;
|
| 4 | +let outputFileNameRegex = []; |
4 | 5 |
|
5 | 6 | function HtmlWebpackMultiBuildPlugin(options) {
|
6 | | - this.options = options; |
| 7 | + this.options = options; |
7 | 8 | }
|
8 | 9 |
|
9 | 10 | HtmlWebpackMultiBuildPlugin.prototype = {
|
10 | | - apply: function(compiler) { |
11 | | - if (compiler.hooks) { |
12 | | - // webpack 4 support |
13 | | - compiler.hooks.compilation.tap('HtmlWebpackMultiBuildPlugin', compilation => { |
14 | | - if (compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration) { |
15 | | - compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync( |
16 | | - 'HtmlWebpackMultiBuildPlugin', |
17 | | - this.beforeHtmlGeneration.bind(this), |
18 | | - ); |
19 | | - } else { |
20 | | - var HtmlWebpackPlugin = require('html-webpack-plugin'); |
21 | | - var hooks = HtmlWebpackPlugin.getHooks(compilation); |
22 | | - hooks.beforeAssetTagGeneration.tapAsync( |
23 | | - 'HtmlWebpackMultiBuildPlugin', |
24 | | - this.beforeHtmlGeneration.bind(this), |
25 | | - ); |
26 | | - } |
27 | | - }); |
28 | | - } else { |
29 | | - compiler.plugin('compilation', compilation => { |
30 | | - compilation.plugin('html-webpack-plugin-before-html-generation', this.beforeHtmlGeneration.bind(this)); |
31 | | - }); |
32 | | - } |
33 | | - }, |
| 11 | + apply: function(compiler) { |
| 12 | + this.createOutputRegexes(compiler.options); |
34 | 13 |
|
35 | | - beforeHtmlGeneration: function(data, cb) { |
36 | | - ++run; |
37 | | - js = js.concat(data.assets.js); |
38 | | - data.assets.js = js; |
39 | | - if (run === 2) { |
40 | | - data.plugin.options.modernScripts = js.filter((value) => value.indexOf('legacy') === -1); |
41 | | - data.plugin.options.legacyScripts = js.filter((value) => value.indexOf('legacy') > 0); |
| 14 | + if (compiler.hooks) { |
| 15 | + // webpack 4 support |
| 16 | + compiler.hooks.compilation.tap( |
| 17 | + "HtmlWebpackMultiBuildPlugin", |
| 18 | + compilation => { |
| 19 | + if (compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration) { |
| 20 | + compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync( |
| 21 | + "HtmlWebpackMultiBuildPlugin", |
| 22 | + this.beforeHtmlGeneration.bind(this) |
| 23 | + ); |
| 24 | + } else { |
| 25 | + var HtmlWebpackPlugin = require("html-webpack-plugin"); |
| 26 | + var hooks = HtmlWebpackPlugin.getHooks(compilation); |
| 27 | + hooks.beforeAssetTagGeneration.tapAsync( |
| 28 | + "HtmlWebpackMultiBuildPlugin", |
| 29 | + this.beforeHtmlGeneration.bind(this) |
| 30 | + ); |
| 31 | + } |
42 | 32 | }
|
| 33 | + ); |
| 34 | + } else { |
| 35 | + compiler.plugin("compilation", compilation => { |
| 36 | + compilation.plugin( |
| 37 | + "html-webpack-plugin-before-html-generation", |
| 38 | + this.beforeHtmlGeneration.bind(this) |
| 39 | + ); |
| 40 | + }); |
| 41 | + } |
| 42 | + }, |
| 43 | + beforeHtmlGeneration: function(data, cb) { |
| 44 | + this.clearOldScripts(data); |
| 45 | + ++run; |
| 46 | + js = js.concat(data.assets.js); |
| 47 | + data.assets.js = js; |
| 48 | + if (run === 2) { |
| 49 | + data.plugin.options.modernScripts = js.filter( |
| 50 | + value => value.indexOf("legacy") === -1 |
| 51 | + ); |
| 52 | + data.plugin.options.legacyScripts = js.filter( |
| 53 | + value => value.indexOf("legacy") > 0 |
| 54 | + ); |
| 55 | + } |
43 | 56 |
|
44 | | - cb(null, data); |
45 | | - }, |
| 57 | + cb(null, data); |
| 58 | + }, |
| 59 | + createOutputRegexes: function(options) { |
| 60 | + if (options.output && options.output.filename) { |
| 61 | + // default webpack entry |
| 62 | + let entry = ["main"]; |
| 63 | + if (options.entry) { |
| 64 | + // when object is provided we have custom entry names |
| 65 | + if (typeof options.entry === "object") { |
| 66 | + entry = Object.keys(options.entry); |
| 67 | + } |
| 68 | + } |
| 69 | + entry.forEach(e => { |
| 70 | + const outFilePathForEntry = options.output.filename.replace( |
| 71 | + "[name]", |
| 72 | + e |
| 73 | + ); |
| 74 | + const matches = outFilePathForEntry.match(/\[hash(:\d+)?]/); |
| 75 | + if (matches) { |
| 76 | + // max hash length is 20 characters so limit the regex to 20 |
| 77 | + const hashLength = matches[1] ? +matches[1].substr(1) : 20; |
| 78 | + outputFileNameRegex.push( |
| 79 | + new RegExp( |
| 80 | + outFilePathForEntry.replace( |
| 81 | + matches[0], |
| 82 | + `[\\w\\d]{${Math.min(hashLength, 20)}}` |
| 83 | + ) |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + }, |
| 90 | + clearOldScripts: function(data) { |
| 91 | + outputFileNameRegex.forEach(r => { |
| 92 | + data.assets.js.forEach(a => { |
| 93 | + // we have one of our entries |
| 94 | + if (r.test(a)) { |
| 95 | + js = js.filter(j => !r.test(j)); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
46 | 100 | };
|
47 | 101 |
|
48 | 102 | module.exports = HtmlWebpackMultiBuildPlugin;
|
0 commit comments