UNPKG

4.81 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource, PrefixSource, RawSource } = require("webpack-sources");
9const { RuntimeGlobals } = require("..");
10const HotUpdateChunk = require("../HotUpdateChunk");
11const Template = require("../Template");
12const { getCompilationHooks } = require("./JavascriptModulesPlugin");
13const {
14 generateEntryStartup,
15 updateHashForEntryStartup
16} = require("./StartupHelpers");
17
18/** @typedef {import("../Compiler")} Compiler */
19
20class ArrayPushCallbackChunkFormatPlugin {
21 /**
22 * Apply the plugin
23 * @param {Compiler} compiler the compiler instance
24 * @returns {void}
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.globalObject;
49 const source = new ConcatSource();
50 const runtimeModules =
51 chunkGraph.getChunkRuntimeModulesInOrder(chunk);
52 if (hotUpdateChunk) {
53 const hotUpdateGlobal =
54 runtimeTemplate.outputOptions.hotUpdateGlobal;
55 source.add(
56 `${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
57 );
58 source.add(`${JSON.stringify(chunk.id)},`);
59 source.add(modules);
60 if (runtimeModules.length > 0) {
61 source.add(",\n");
62 const runtimePart = Template.renderChunkRuntimeModules(
63 runtimeModules,
64 renderContext
65 );
66 source.add(runtimePart);
67 }
68 source.add(")");
69 } else {
70 const chunkLoadingGlobal =
71 runtimeTemplate.outputOptions.chunkLoadingGlobal;
72 source.add(
73 `(${globalObject}[${JSON.stringify(
74 chunkLoadingGlobal
75 )}] = ${globalObject}[${JSON.stringify(
76 chunkLoadingGlobal
77 )}] || []).push([`
78 );
79 source.add(`${JSON.stringify(chunk.ids)},`);
80 source.add(modules);
81 const entries = Array.from(
82 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
83 );
84 if (runtimeModules.length > 0 || entries.length > 0) {
85 const runtime = new ConcatSource(
86 (runtimeTemplate.supportsArrowFunction()
87 ? "__webpack_require__ =>"
88 : "function(__webpack_require__)") +
89 " { // webpackRuntimeModules\n"
90 );
91 if (runtimeModules.length > 0) {
92 runtime.add(
93 Template.renderRuntimeModules(runtimeModules, {
94 ...renderContext,
95 codeGenerationResults: compilation.codeGenerationResults
96 })
97 );
98 }
99 if (entries.length > 0) {
100 const startupSource = new RawSource(
101 generateEntryStartup(
102 chunkGraph,
103 runtimeTemplate,
104 entries,
105 chunk,
106 true
107 )
108 );
109 runtime.add(
110 hooks.renderStartup.call(
111 startupSource,
112 entries[entries.length - 1][0],
113 {
114 ...renderContext,
115 inlined: false
116 }
117 )
118 );
119 if (
120 chunkGraph
121 .getChunkRuntimeRequirements(chunk)
122 .has(RuntimeGlobals.returnExportsFromRuntime)
123 ) {
124 runtime.add("return __webpack_exports__;\n");
125 }
126 }
127 runtime.add("}\n");
128 source.add(",\n");
129 source.add(new PrefixSource("/******/ ", runtime));
130 }
131 source.add("])");
132 }
133 return source;
134 }
135 );
136 hooks.chunkHash.tap(
137 "ArrayPushCallbackChunkFormatPlugin",
138 (chunk, hash, { chunkGraph, runtimeTemplate }) => {
139 if (chunk.hasRuntime()) return;
140 hash.update(
141 `ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}`
142 );
143 const entries = Array.from(
144 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
145 );
146 updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
147 }
148 );
149 }
150 );
151 }
152}
153
154module.exports = ArrayPushCallbackChunkFormatPlugin;