2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const { ConcatSource, RawSource } = require("webpack-sources");
9 const RuntimeGlobals = require("../RuntimeGlobals");
10 const Template = require("../Template");
12 getChunkFilenameTemplate,
14 } = require("./JavascriptModulesPlugin");
17 updateHashForEntryStartup
18 } = require("./StartupHelpers");
20 /** @typedef {import("../Compiler")} Compiler */
22 class CommonJsChunkFormatPlugin {
25 * @param {Compiler} compiler the compiler instance
29 compiler.hooks.thisCompilation.tap(
30 "CommonJsChunkFormatPlugin",
32 compilation.hooks.additionalChunkRuntimeRequirements.tap(
33 "CommonJsChunkLoadingPlugin",
34 (chunk, set, { chunkGraph }) => {
35 if (chunk.hasRuntime()) return;
36 if (chunkGraph.getNumberOfEntryModules(chunk) > 0) {
37 set.add(RuntimeGlobals.require);
38 set.add(RuntimeGlobals.startupEntrypoint);
39 set.add(RuntimeGlobals.externalInstallChunk);
43 const hooks = getCompilationHooks(compilation);
44 hooks.renderChunk.tap(
45 "CommonJsChunkFormatPlugin",
46 (modules, renderContext) => {
47 const { chunk, chunkGraph, runtimeTemplate } = renderContext;
48 const source = new ConcatSource();
49 source.add(`exports.id = ${JSON.stringify(chunk.id)};\n`);
50 source.add(`exports.ids = ${JSON.stringify(chunk.ids)};\n`);
51 source.add(`exports.modules = `);
54 const runtimeModules =
55 chunkGraph.getChunkRuntimeModulesInOrder(chunk);
56 if (runtimeModules.length > 0) {
57 source.add("exports.runtime =\n");
59 Template.renderChunkRuntimeModules(
65 const entries = Array.from(
66 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
68 if (entries.length > 0) {
69 const runtimeChunk = entries[0][1].getRuntimeChunk();
70 const currentOutputName = compilation
72 getChunkFilenameTemplate(chunk, compilation.outputOptions),
75 contentHashType: "javascript"
79 const runtimeOutputName = compilation
81 getChunkFilenameTemplate(
83 compilation.outputOptions
87 contentHashType: "javascript"
92 // remove filename, we only need the directory
93 currentOutputName.pop();
95 // remove common parts
97 currentOutputName.length > 0 &&
98 runtimeOutputName.length > 0 &&
99 currentOutputName[0] === runtimeOutputName[0]
101 currentOutputName.shift();
102 runtimeOutputName.shift();
107 (currentOutputName.length > 0
108 ? "../".repeat(currentOutputName.length)
109 : "./") + runtimeOutputName.join("/");
111 const entrySource = new ConcatSource();
114 runtimeTemplate.supportsArrowFunction()
119 entrySource.add("var exports = {};\n");
120 entrySource.add(source);
121 entrySource.add(";\n\n// load runtime\n");
123 `var __webpack_require__ = require(${JSON.stringify(
128 `${RuntimeGlobals.externalInstallChunk}(exports);\n`
130 const startupSource = new RawSource(
131 generateEntryStartup(
140 hooks.renderStartup.call(
142 entries[entries.length - 1][0],
149 entrySource.add("\n})()");
156 "CommonJsChunkFormatPlugin",
157 (chunk, hash, { chunkGraph }) => {
158 if (chunk.hasRuntime()) return;
159 hash.update("CommonJsChunkFormatPlugin");
161 const entries = Array.from(
162 chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
164 updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
172 module.exports = CommonJsChunkFormatPlugin;