Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 9, 2021. It is now read-only.

Commit 3a481ea

Browse files
author
TRTEUFE
committed
Merge branch 'msegers-patch-1'
2 parents d236d13 + 92e421c commit 3a481ea

File tree

1 file changed

+89
-35
lines changed

1 file changed

+89
-35
lines changed

‎index.js

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,102 @@
1-
'use strict';
1+
"use strict";
22
let js = [];
33
let run = 0;
4+
let outputFileNameRegex = [];
45

56
function HtmlWebpackMultiBuildPlugin(options) {
6-
this.options = options;
7+
this.options = options;
78
}
89

910
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);
3413

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+
}
4232
}
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+
}
4356

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+
}
46100
};
47101

48102
module.exports = HtmlWebpackMultiBuildPlugin;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /