1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { ConcatSource, RawSource } = require("webpack-sources");
|
---|
9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
10 | const Template = require("../Template");
|
---|
11 | const { getUndoPath } = require("../util/identifier");
|
---|
12 | const {
|
---|
13 | getChunkFilenameTemplate,
|
---|
14 | getCompilationHooks
|
---|
15 | } = require("./JavascriptModulesPlugin");
|
---|
16 | const {
|
---|
17 | generateEntryStartup,
|
---|
18 | updateHashForEntryStartup
|
---|
19 | } = require("./StartupHelpers");
|
---|
20 |
|
---|
21 | /** @typedef {import("../Chunk")} Chunk */
|
---|
22 | /** @typedef {import("../Compiler")} Compiler */
|
---|
23 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
---|
24 |
|
---|
25 | class CommonJsChunkFormatPlugin {
|
---|
26 | /**
|
---|
27 | * Apply the plugin
|
---|
28 | * @param {Compiler} compiler the compiler instance
|
---|
29 | * @returns {void}
|
---|
30 | */
|
---|
31 | apply(compiler) {
|
---|
32 | compiler.hooks.thisCompilation.tap(
|
---|
33 | "CommonJsChunkFormatPlugin",
|
---|
34 | compilation => {
|
---|
35 | compilation.hooks.additionalChunkRuntimeRequirements.tap(
|
---|
36 | "CommonJsChunkFormatPlugin",
|
---|
37 | (chunk, set, { chunkGraph }) => {
|
---|
38 | if (chunk.hasRuntime()) return;
|
---|
39 | if (chunkGraph.getNumberOfEntryModules(chunk) > 0) {
|
---|
40 | set.add(RuntimeGlobals.require);
|
---|
41 | set.add(RuntimeGlobals.startupEntrypoint);
|
---|
42 | set.add(RuntimeGlobals.externalInstallChunk);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | );
|
---|
46 | const hooks = getCompilationHooks(compilation);
|
---|
47 | hooks.renderChunk.tap(
|
---|
48 | "CommonJsChunkFormatPlugin",
|
---|
49 | (modules, renderContext) => {
|
---|
50 | const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
---|
51 | const source = new ConcatSource();
|
---|
52 | source.add(`exports.id = ${JSON.stringify(chunk.id)};\n`);
|
---|
53 | source.add(`exports.ids = ${JSON.stringify(chunk.ids)};\n`);
|
---|
54 | source.add("exports.modules = ");
|
---|
55 | source.add(modules);
|
---|
56 | source.add(";\n");
|
---|
57 | const runtimeModules =
|
---|
58 | chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
---|
59 | if (runtimeModules.length > 0) {
|
---|
60 | source.add("exports.runtime =\n");
|
---|
61 | source.add(
|
---|
62 | Template.renderChunkRuntimeModules(
|
---|
63 | runtimeModules,
|
---|
64 | renderContext
|
---|
65 | )
|
---|
66 | );
|
---|
67 | }
|
---|
68 | const entries = Array.from(
|
---|
69 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
---|
70 | );
|
---|
71 | if (entries.length > 0) {
|
---|
72 | const runtimeChunk =
|
---|
73 | /** @type {Entrypoint} */
|
---|
74 | (entries[0][1]).getRuntimeChunk();
|
---|
75 | const currentOutputName = compilation
|
---|
76 | .getPath(
|
---|
77 | getChunkFilenameTemplate(chunk, compilation.outputOptions),
|
---|
78 | {
|
---|
79 | chunk,
|
---|
80 | contentHashType: "javascript"
|
---|
81 | }
|
---|
82 | )
|
---|
83 | .replace(/^\/+/g, "")
|
---|
84 | .split("/");
|
---|
85 | const runtimeOutputName = compilation
|
---|
86 | .getPath(
|
---|
87 | getChunkFilenameTemplate(
|
---|
88 | /** @type {Chunk} */
|
---|
89 | (runtimeChunk),
|
---|
90 | compilation.outputOptions
|
---|
91 | ),
|
---|
92 | {
|
---|
93 | chunk: /** @type {Chunk} */ (runtimeChunk),
|
---|
94 | contentHashType: "javascript"
|
---|
95 | }
|
---|
96 | )
|
---|
97 | .replace(/^\/+/g, "")
|
---|
98 | .split("/");
|
---|
99 |
|
---|
100 | // remove common parts
|
---|
101 | while (
|
---|
102 | currentOutputName.length > 1 &&
|
---|
103 | runtimeOutputName.length > 1 &&
|
---|
104 | currentOutputName[0] === runtimeOutputName[0]
|
---|
105 | ) {
|
---|
106 | currentOutputName.shift();
|
---|
107 | runtimeOutputName.shift();
|
---|
108 | }
|
---|
109 | const last = runtimeOutputName.join("/");
|
---|
110 | // create final path
|
---|
111 | const runtimePath =
|
---|
112 | getUndoPath(currentOutputName.join("/"), last, true) + last;
|
---|
113 |
|
---|
114 | const entrySource = new ConcatSource();
|
---|
115 | entrySource.add(
|
---|
116 | `(${
|
---|
117 | runtimeTemplate.supportsArrowFunction()
|
---|
118 | ? "() => "
|
---|
119 | : "function() "
|
---|
120 | }{\n`
|
---|
121 | );
|
---|
122 | entrySource.add("var exports = {};\n");
|
---|
123 | entrySource.add(source);
|
---|
124 | entrySource.add(";\n\n// load runtime\n");
|
---|
125 | entrySource.add(
|
---|
126 | `var ${RuntimeGlobals.require} = require(${JSON.stringify(
|
---|
127 | runtimePath
|
---|
128 | )});\n`
|
---|
129 | );
|
---|
130 | entrySource.add(
|
---|
131 | `${RuntimeGlobals.externalInstallChunk}(exports);\n`
|
---|
132 | );
|
---|
133 | const startupSource = new RawSource(
|
---|
134 | generateEntryStartup(
|
---|
135 | chunkGraph,
|
---|
136 | runtimeTemplate,
|
---|
137 | entries,
|
---|
138 | chunk,
|
---|
139 | false
|
---|
140 | )
|
---|
141 | );
|
---|
142 | entrySource.add(
|
---|
143 | hooks.renderStartup.call(
|
---|
144 | startupSource,
|
---|
145 | entries[entries.length - 1][0],
|
---|
146 | {
|
---|
147 | ...renderContext,
|
---|
148 | inlined: false
|
---|
149 | }
|
---|
150 | )
|
---|
151 | );
|
---|
152 | entrySource.add("\n})()");
|
---|
153 | return entrySource;
|
---|
154 | }
|
---|
155 | return source;
|
---|
156 | }
|
---|
157 | );
|
---|
158 | hooks.chunkHash.tap(
|
---|
159 | "CommonJsChunkFormatPlugin",
|
---|
160 | (chunk, hash, { chunkGraph }) => {
|
---|
161 | if (chunk.hasRuntime()) return;
|
---|
162 | hash.update("CommonJsChunkFormatPlugin");
|
---|
163 | hash.update("1");
|
---|
164 | const entries = Array.from(
|
---|
165 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
---|
166 | );
|
---|
167 | updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
|
---|
168 | }
|
---|
169 | );
|
---|
170 | }
|
---|
171 | );
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | module.exports = CommonJsChunkFormatPlugin;
|
---|