1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ConcatSource, PrefixSource, RawSource } = require("webpack-sources");
|
9 | const { RuntimeGlobals } = require("..");
|
10 | const HotUpdateChunk = require("../HotUpdateChunk");
|
11 | const Template = require("../Template");
|
12 | const { getCompilationHooks } = require("./JavascriptModulesPlugin");
|
13 | const {
|
14 | generateEntryStartup,
|
15 | updateHashForEntryStartup
|
16 | } = require("./StartupHelpers");
|
17 |
|
18 |
|
19 |
|
20 | class ArrayPushCallbackChunkFormatPlugin {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | apply(compiler) {
|
27 | compiler.hooks.thisCompilation.tap(
|
28 | "ArrayPushCallbackChunkFormatPlugin",
|
29 | compilation => {
|
30 | compilation.hooks.additionalChunkRuntimeRequirements.tap(
|
31 | "ArrayPushCallbackChunkFormatPlugin",
|
32 | (chunk, set, { chunkGraph }) => {
|
33 | if (chunk.hasRuntime()) return;
|
34 | if (chunkGraph.getNumberOfEntryModules(chunk) > 0) {
|
35 | set.add(RuntimeGlobals.onChunksLoaded);
|
36 | set.add(RuntimeGlobals.require);
|
37 | }
|
38 | set.add(RuntimeGlobals.chunkCallback);
|
39 | }
|
40 | );
|
41 | const hooks = getCompilationHooks(compilation);
|
42 | hooks.renderChunk.tap(
|
43 | "ArrayPushCallbackChunkFormatPlugin",
|
44 | (modules, renderContext) => {
|
45 | const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
46 | const hotUpdateChunk =
|
47 | chunk instanceof HotUpdateChunk ? chunk : null;
|
48 | const globalObject = runtimeTemplate.outputOptions.globalObject;
|
49 | const source = new ConcatSource();
|
50 | const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(
|
51 | chunk
|
52 | );
|
53 | if (hotUpdateChunk) {
|
54 | const hotUpdateGlobal =
|
55 | runtimeTemplate.outputOptions.hotUpdateGlobal;
|
56 | source.add(
|
57 | `${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
|
58 | );
|
59 | source.add(`${JSON.stringify(chunk.id)},`);
|
60 | source.add(modules);
|
61 | if (runtimeModules.length > 0) {
|
62 | source.add(",\n");
|
63 | const runtimePart = Template.renderChunkRuntimeModules(
|
64 | runtimeModules,
|
65 | renderContext
|
66 | );
|
67 | source.add(runtimePart);
|
68 | }
|
69 | source.add(")");
|
70 | } else {
|
71 | const chunkLoadingGlobal =
|
72 | runtimeTemplate.outputOptions.chunkLoadingGlobal;
|
73 | source.add(
|
74 | `(${globalObject}[${JSON.stringify(
|
75 | chunkLoadingGlobal
|
76 | )}] = ${globalObject}[${JSON.stringify(
|
77 | chunkLoadingGlobal
|
78 | )}] || []).push([`
|
79 | );
|
80 | source.add(`${JSON.stringify(chunk.ids)},`);
|
81 | source.add(modules);
|
82 | const entries = Array.from(
|
83 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
84 | );
|
85 | if (runtimeModules.length > 0 || entries.length > 0) {
|
86 | const strictBailout = hooks.strictRuntimeBailout.call(
|
87 | renderContext
|
88 | );
|
89 | const runtime = new ConcatSource(
|
90 | (runtimeTemplate.supportsArrowFunction()
|
91 | ? "__webpack_require__ =>"
|
92 | : "function(__webpack_require__)") +
|
93 | " { // webpackRuntimeModules\n",
|
94 | strictBailout
|
95 | ? `// runtime can't be in strict mode because ${strictBailout}.\n\n`
|
96 | : '"use strict";\n\n'
|
97 | );
|
98 | if (runtimeModules.length > 0) {
|
99 | runtime.add(
|
100 | Template.renderRuntimeModules(runtimeModules, {
|
101 | ...renderContext,
|
102 | codeGenerationResults: compilation.codeGenerationResults,
|
103 | useStrict: !!strictBailout
|
104 | })
|
105 | );
|
106 | }
|
107 | if (entries.length > 0) {
|
108 | const startupSource = new RawSource(
|
109 | generateEntryStartup(
|
110 | chunkGraph,
|
111 | runtimeTemplate,
|
112 | entries,
|
113 | chunk,
|
114 | true
|
115 | )
|
116 | );
|
117 | runtime.add(
|
118 | hooks.renderStartup.call(
|
119 | startupSource,
|
120 | entries[entries.length - 1][0],
|
121 | {
|
122 | ...renderContext,
|
123 | inlined: false
|
124 | }
|
125 | )
|
126 | );
|
127 | if (
|
128 | chunkGraph
|
129 | .getChunkRuntimeRequirements(chunk)
|
130 | .has(RuntimeGlobals.returnExportsFromRuntime)
|
131 | ) {
|
132 | runtime.add("return __webpack_exports__;\n");
|
133 | }
|
134 | }
|
135 | runtime.add("}\n");
|
136 | source.add(",\n");
|
137 | source.add(new PrefixSource("/******/ ", runtime));
|
138 | }
|
139 | source.add("])");
|
140 | }
|
141 | return source;
|
142 | }
|
143 | );
|
144 | hooks.chunkHash.tap(
|
145 | "ArrayPushCallbackChunkFormatPlugin",
|
146 | (chunk, hash, { chunkGraph, runtimeTemplate }) => {
|
147 | if (chunk.hasRuntime()) return;
|
148 | hash.update("ArrayPushCallbackChunkFormatPlugin");
|
149 | hash.update("1");
|
150 | hash.update(`${runtimeTemplate.outputOptions.chunkLoadingGlobal}`);
|
151 | hash.update(`${runtimeTemplate.outputOptions.hotUpdateGlobal}`);
|
152 | hash.update(`${runtimeTemplate.outputOptions.globalObject}`);
|
153 | const entries = Array.from(
|
154 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
155 | );
|
156 | updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
|
157 | }
|
158 | );
|
159 | }
|
160 | );
|
161 | }
|
162 | }
|
163 |
|
164 | module.exports = ArrayPushCallbackChunkFormatPlugin;
|