| [9af201e] | 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 | createChunkHashHandler,
|
|---|
| 14 | getChunkInfo
|
|---|
| 15 | } = require("./ChunkFormatHelpers");
|
|---|
| 16 | const {
|
|---|
| 17 | getChunkFilenameTemplate,
|
|---|
| 18 | getCompilationHooks
|
|---|
| 19 | } = require("./JavascriptModulesPlugin");
|
|---|
| 20 | const { generateEntryStartup } = require("./StartupHelpers");
|
|---|
| 21 |
|
|---|
| 22 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 23 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 24 |
|
|---|
| 25 | const PLUGIN_NAME = "CommonJsChunkFormatPlugin";
|
|---|
| 26 |
|
|---|
| 27 | class CommonJsChunkFormatPlugin {
|
|---|
| 28 | /**
|
|---|
| 29 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 30 | * @param {Compiler} compiler the compiler instance
|
|---|
| 31 | * @returns {void}
|
|---|
| 32 | */
|
|---|
| 33 | apply(compiler) {
|
|---|
| 34 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 35 | compilation.hooks.additionalChunkRuntimeRequirements.tap(
|
|---|
| 36 | PLUGIN_NAME,
|
|---|
| 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(PLUGIN_NAME, (modules, renderContext) => {
|
|---|
| 48 | const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
|---|
| 49 | const source = new ConcatSource();
|
|---|
| 50 | source.add(`exports.id = ${JSON.stringify(chunk.id)};\n`);
|
|---|
| 51 | source.add(`exports.ids = ${JSON.stringify(chunk.ids)};\n`);
|
|---|
| 52 | source.add("exports.modules = ");
|
|---|
| 53 | source.add(modules);
|
|---|
| 54 | source.add(";\n");
|
|---|
| 55 | const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
|---|
| 56 | if (runtimeModules.length > 0) {
|
|---|
| 57 | source.add("exports.runtime =\n");
|
|---|
| 58 | source.add(
|
|---|
| 59 | Template.renderChunkRuntimeModules(runtimeModules, renderContext)
|
|---|
| 60 | );
|
|---|
| 61 | }
|
|---|
| 62 | const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
|
|---|
| 63 | if (runtimeChunk) {
|
|---|
| 64 | const currentOutputName = compilation
|
|---|
| 65 | .getPath(
|
|---|
| 66 | getChunkFilenameTemplate(chunk, compilation.outputOptions),
|
|---|
| 67 | {
|
|---|
| 68 | chunk,
|
|---|
| 69 | contentHashType: "javascript"
|
|---|
| 70 | }
|
|---|
| 71 | )
|
|---|
| 72 | .replace(/^\/+/g, "")
|
|---|
| 73 | .split("/");
|
|---|
| 74 | const runtimeOutputName = compilation
|
|---|
| 75 | .getPath(
|
|---|
| 76 | getChunkFilenameTemplate(
|
|---|
| 77 | /** @type {Chunk} */
|
|---|
| 78 | (runtimeChunk),
|
|---|
| 79 | compilation.outputOptions
|
|---|
| 80 | ),
|
|---|
| 81 | {
|
|---|
| 82 | chunk: /** @type {Chunk} */ (runtimeChunk),
|
|---|
| 83 | contentHashType: "javascript"
|
|---|
| 84 | }
|
|---|
| 85 | )
|
|---|
| 86 | .replace(/^\/+/g, "")
|
|---|
| 87 | .split("/");
|
|---|
| 88 |
|
|---|
| 89 | // remove common parts
|
|---|
| 90 | while (
|
|---|
| 91 | currentOutputName.length > 1 &&
|
|---|
| 92 | runtimeOutputName.length > 1 &&
|
|---|
| 93 | currentOutputName[0] === runtimeOutputName[0]
|
|---|
| 94 | ) {
|
|---|
| 95 | currentOutputName.shift();
|
|---|
| 96 | runtimeOutputName.shift();
|
|---|
| 97 | }
|
|---|
| 98 | const last = runtimeOutputName.join("/");
|
|---|
| 99 | // create final path
|
|---|
| 100 | const runtimePath =
|
|---|
| 101 | getUndoPath(currentOutputName.join("/"), last, true) + last;
|
|---|
| 102 |
|
|---|
| 103 | const entrySource = new ConcatSource();
|
|---|
| 104 | entrySource.add(
|
|---|
| 105 | `(${
|
|---|
| 106 | runtimeTemplate.supportsArrowFunction() ? "() => " : "function() "
|
|---|
| 107 | }{\n`
|
|---|
| 108 | );
|
|---|
| 109 | entrySource.add("var exports = {};\n");
|
|---|
| 110 | entrySource.add(source);
|
|---|
| 111 | entrySource.add(";\n\n// load runtime\n");
|
|---|
| 112 | entrySource.add(
|
|---|
| 113 | `var ${RuntimeGlobals.require} = require(${JSON.stringify(
|
|---|
| 114 | runtimePath
|
|---|
| 115 | )});\n`
|
|---|
| 116 | );
|
|---|
| 117 | entrySource.add(`${RuntimeGlobals.externalInstallChunk}(exports);\n`);
|
|---|
| 118 | const startupSource = new RawSource(
|
|---|
| 119 | generateEntryStartup(
|
|---|
| 120 | chunkGraph,
|
|---|
| 121 | runtimeTemplate,
|
|---|
| 122 | entries,
|
|---|
| 123 | chunk,
|
|---|
| 124 | false
|
|---|
| 125 | )
|
|---|
| 126 | );
|
|---|
| 127 | entrySource.add(
|
|---|
| 128 | hooks.renderStartup.call(
|
|---|
| 129 | startupSource,
|
|---|
| 130 | entries[entries.length - 1][0],
|
|---|
| 131 | renderContext
|
|---|
| 132 | )
|
|---|
| 133 | );
|
|---|
| 134 | entrySource.add("\n})()");
|
|---|
| 135 | return entrySource;
|
|---|
| 136 | }
|
|---|
| 137 | return source;
|
|---|
| 138 | });
|
|---|
| 139 |
|
|---|
| 140 | hooks.chunkHash.tap(PLUGIN_NAME, createChunkHashHandler(PLUGIN_NAME));
|
|---|
| 141 | });
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | module.exports = CommonJsChunkFormatPlugin;
|
|---|