| [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, 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 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 19 | /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
|
|---|
| 20 | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
|
|---|
| 21 |
|
|---|
| 22 | const PLUGIN_NAME = "ArrayPushCallbackChunkFormatPlugin";
|
|---|
| 23 |
|
|---|
| 24 | class ArrayPushCallbackChunkFormatPlugin {
|
|---|
| 25 | /**
|
|---|
| 26 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 27 | * @param {Compiler} compiler the compiler instance
|
|---|
| 28 | * @returns {void}
|
|---|
| 29 | */
|
|---|
| 30 | apply(compiler) {
|
|---|
| 31 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 32 | compilation.hooks.additionalChunkRuntimeRequirements.tap(
|
|---|
| 33 | PLUGIN_NAME,
|
|---|
| 34 | (chunk, set, { chunkGraph }) => {
|
|---|
| 35 | if (chunk.hasRuntime()) return;
|
|---|
| 36 | if (chunkGraph.getNumberOfEntryModules(chunk) > 0) {
|
|---|
| 37 | set.add(RuntimeGlobals.onChunksLoaded);
|
|---|
| 38 | set.add(RuntimeGlobals.exports);
|
|---|
| 39 | set.add(RuntimeGlobals.require);
|
|---|
| 40 | }
|
|---|
| 41 | set.add(RuntimeGlobals.chunkCallback);
|
|---|
| 42 | }
|
|---|
| 43 | );
|
|---|
| 44 | const hooks = getCompilationHooks(compilation);
|
|---|
| 45 | hooks.renderChunk.tap(PLUGIN_NAME, (modules, renderContext) => {
|
|---|
| 46 | const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
|---|
| 47 | const hotUpdateChunk = chunk instanceof HotUpdateChunk ? chunk : null;
|
|---|
| 48 | const globalObject = runtimeTemplate.globalObject;
|
|---|
| 49 | const source = new ConcatSource();
|
|---|
| 50 | const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
|---|
| 51 | if (hotUpdateChunk) {
|
|---|
| 52 | const hotUpdateGlobal = runtimeTemplate.outputOptions.hotUpdateGlobal;
|
|---|
| 53 | source.add(`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`);
|
|---|
| 54 | source.add(`${JSON.stringify(chunk.id)},`);
|
|---|
| 55 | source.add(modules);
|
|---|
| 56 | if (runtimeModules.length > 0) {
|
|---|
| 57 | source.add(",\n");
|
|---|
| 58 | const runtimePart = Template.renderChunkRuntimeModules(
|
|---|
| 59 | runtimeModules,
|
|---|
| 60 | renderContext
|
|---|
| 61 | );
|
|---|
| 62 | source.add(runtimePart);
|
|---|
| 63 | }
|
|---|
| 64 | source.add(")");
|
|---|
| 65 | } else {
|
|---|
| 66 | const chunkLoadingGlobal =
|
|---|
| 67 | runtimeTemplate.outputOptions.chunkLoadingGlobal;
|
|---|
| 68 | source.add(
|
|---|
| 69 | `(${globalObject}[${JSON.stringify(
|
|---|
| 70 | chunkLoadingGlobal
|
|---|
| 71 | )}] = ${globalObject}[${JSON.stringify(
|
|---|
| 72 | chunkLoadingGlobal
|
|---|
| 73 | )}] || []).push([`
|
|---|
| 74 | );
|
|---|
| 75 | source.add(`${JSON.stringify(chunk.ids)},`);
|
|---|
| 76 | source.add(modules);
|
|---|
| 77 | /** @type {EntryModuleWithChunkGroup[]} */
|
|---|
| 78 | const entries = [
|
|---|
| 79 | ...chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
|---|
| 80 | ];
|
|---|
| 81 | if (runtimeModules.length > 0 || entries.length > 0) {
|
|---|
| 82 | const runtime = new ConcatSource(
|
|---|
| 83 | `${
|
|---|
| 84 | runtimeTemplate.supportsArrowFunction()
|
|---|
| 85 | ? `${RuntimeGlobals.require} =>`
|
|---|
| 86 | : `function(${RuntimeGlobals.require})`
|
|---|
| 87 | } { // webpackRuntimeModules\n`
|
|---|
| 88 | );
|
|---|
| 89 | if (runtimeModules.length > 0) {
|
|---|
| 90 | runtime.add(
|
|---|
| 91 | Template.renderRuntimeModules(runtimeModules, {
|
|---|
| 92 | ...renderContext,
|
|---|
| 93 | codeGenerationResults:
|
|---|
| 94 | /** @type {CodeGenerationResults} */
|
|---|
| 95 | (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 | renderContext
|
|---|
| 114 | )
|
|---|
| 115 | );
|
|---|
| 116 | if (
|
|---|
| 117 | chunkGraph
|
|---|
| 118 | .getChunkRuntimeRequirements(chunk)
|
|---|
| 119 | .has(RuntimeGlobals.returnExportsFromRuntime)
|
|---|
| 120 | ) {
|
|---|
| 121 | runtime.add(`return ${RuntimeGlobals.exports};\n`);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | runtime.add("}\n");
|
|---|
| 125 | source.add(",\n");
|
|---|
| 126 | source.add(new PrefixSource("/******/ ", runtime));
|
|---|
| 127 | }
|
|---|
| 128 | source.add("])");
|
|---|
| 129 | }
|
|---|
| 130 | return source;
|
|---|
| 131 | });
|
|---|
| 132 | hooks.chunkHash.tap(
|
|---|
| 133 | PLUGIN_NAME,
|
|---|
| 134 | (chunk, hash, { chunkGraph, runtimeTemplate }) => {
|
|---|
| 135 | if (chunk.hasRuntime()) return;
|
|---|
| 136 | hash.update(
|
|---|
| 137 | `${PLUGIN_NAME}1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}`
|
|---|
| 138 | );
|
|---|
| 139 | /** @type {EntryModuleWithChunkGroup[]} */
|
|---|
| 140 | const entries = [
|
|---|
| 141 | ...chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
|---|
| 142 | ];
|
|---|
| 143 | updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
|
|---|
| 144 | }
|
|---|
| 145 | );
|
|---|
| 146 | });
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | module.exports = ArrayPushCallbackChunkFormatPlugin;
|
|---|