1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | class InlineChunkHtmlPlugin {
|
11 | constructor(htmlWebpackPlugin, tests) {
|
12 | this.htmlWebpackPlugin = htmlWebpackPlugin;
|
13 | this.tests = tests;
|
14 | }
|
15 |
|
16 | getInlinedTag(publicPath, assets, tag) {
|
17 | if (tag.tagName !== 'script' || !(tag.attributes && tag.attributes.src)) {
|
18 | return tag;
|
19 | }
|
20 | const scriptName = publicPath
|
21 | ? tag.attributes.src.replace(publicPath, '')
|
22 | : tag.attributes.src;
|
23 | if (!this.tests.some(test => scriptName.match(test))) {
|
24 | return tag;
|
25 | }
|
26 | const asset = assets[scriptName];
|
27 | if (asset == null) {
|
28 | return tag;
|
29 | }
|
30 | return { tagName: 'script', innerHTML: asset.source(), closeTag: true };
|
31 | }
|
32 |
|
33 | apply(compiler) {
|
34 | let publicPath = compiler.options.output.publicPath || '';
|
35 | if (publicPath && !publicPath.endsWith('/')) {
|
36 | publicPath += '/';
|
37 | }
|
38 |
|
39 | compiler.hooks.compilation.tap('InlineChunkHtmlPlugin', compilation => {
|
40 | const tagFunction = tag =>
|
41 | this.getInlinedTag(publicPath, compilation.assets, tag);
|
42 |
|
43 | const hooks = this.htmlWebpackPlugin.getHooks(compilation);
|
44 | hooks.alterAssetTagGroups.tap('InlineChunkHtmlPlugin', assets => {
|
45 | assets.headTags = assets.headTags.map(tagFunction);
|
46 | assets.bodyTags = assets.bodyTags.map(tagFunction);
|
47 | });
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | });
|
59 | }
|
60 | }
|
61 |
|
62 | module.exports = InlineChunkHtmlPlugin;
|