[79a0317] | 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 |
|
---|
| 20 | class 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.exports);
|
---|
| 37 | set.add(RuntimeGlobals.require);
|
---|
| 38 | }
|
---|
| 39 | set.add(RuntimeGlobals.chunkCallback);
|
---|
| 40 | }
|
---|
| 41 | );
|
---|
| 42 | const hooks = getCompilationHooks(compilation);
|
---|
| 43 | hooks.renderChunk.tap(
|
---|
| 44 | "ArrayPushCallbackChunkFormatPlugin",
|
---|
| 45 | (modules, renderContext) => {
|
---|
| 46 | const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
---|
| 47 | const hotUpdateChunk =
|
---|
| 48 | chunk instanceof HotUpdateChunk ? chunk : null;
|
---|
| 49 | const globalObject = runtimeTemplate.globalObject;
|
---|
| 50 | const source = new ConcatSource();
|
---|
| 51 | const runtimeModules =
|
---|
| 52 | chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
---|
| 53 | if (hotUpdateChunk) {
|
---|
| 54 | const hotUpdateGlobal =
|
---|
| 55 | runtimeTemplate.outputOptions.hotUpdateGlobal;
|
---|
| 56 | source.add(
|
---|
| 57 | `${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
|
---|
| 58 | );
|
---|
| 59 | source.add(`${JSON.stringify(chunk.id)},`);
|
---|
| 60 | source.add(modules);
|
---|
| 61 | if (runtimeModules.length > 0) {
|
---|
| 62 | source.add(",\n");
|
---|
| 63 | const runtimePart = Template.renderChunkRuntimeModules(
|
---|
| 64 | runtimeModules,
|
---|
| 65 | renderContext
|
---|
| 66 | );
|
---|
| 67 | source.add(runtimePart);
|
---|
| 68 | }
|
---|
| 69 | source.add(")");
|
---|
| 70 | } else {
|
---|
| 71 | const chunkLoadingGlobal =
|
---|
| 72 | runtimeTemplate.outputOptions.chunkLoadingGlobal;
|
---|
| 73 | source.add(
|
---|
| 74 | `(${globalObject}[${JSON.stringify(
|
---|
| 75 | chunkLoadingGlobal
|
---|
| 76 | )}] = ${globalObject}[${JSON.stringify(
|
---|
| 77 | chunkLoadingGlobal
|
---|
| 78 | )}] || []).push([`
|
---|
| 79 | );
|
---|
| 80 | source.add(`${JSON.stringify(chunk.ids)},`);
|
---|
| 81 | source.add(modules);
|
---|
| 82 | const entries = Array.from(
|
---|
| 83 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
---|
| 84 | );
|
---|
| 85 | if (runtimeModules.length > 0 || entries.length > 0) {
|
---|
| 86 | const runtime = new ConcatSource(
|
---|
| 87 | `${
|
---|
| 88 | runtimeTemplate.supportsArrowFunction()
|
---|
| 89 | ? `${RuntimeGlobals.require} =>`
|
---|
| 90 | : `function(${RuntimeGlobals.require})`
|
---|
| 91 | } { // webpackRuntimeModules\n`
|
---|
| 92 | );
|
---|
| 93 | if (runtimeModules.length > 0) {
|
---|
| 94 | runtime.add(
|
---|
| 95 | Template.renderRuntimeModules(runtimeModules, {
|
---|
| 96 | ...renderContext,
|
---|
| 97 | codeGenerationResults: compilation.codeGenerationResults
|
---|
| 98 | })
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 | if (entries.length > 0) {
|
---|
| 102 | const startupSource = new RawSource(
|
---|
| 103 | generateEntryStartup(
|
---|
| 104 | chunkGraph,
|
---|
| 105 | runtimeTemplate,
|
---|
| 106 | entries,
|
---|
| 107 | chunk,
|
---|
| 108 | true
|
---|
| 109 | )
|
---|
| 110 | );
|
---|
| 111 | runtime.add(
|
---|
| 112 | hooks.renderStartup.call(
|
---|
| 113 | startupSource,
|
---|
| 114 | entries[entries.length - 1][0],
|
---|
| 115 | {
|
---|
| 116 | ...renderContext,
|
---|
| 117 | inlined: false
|
---|
| 118 | }
|
---|
| 119 | )
|
---|
| 120 | );
|
---|
| 121 | if (
|
---|
| 122 | chunkGraph
|
---|
| 123 | .getChunkRuntimeRequirements(chunk)
|
---|
| 124 | .has(RuntimeGlobals.returnExportsFromRuntime)
|
---|
| 125 | ) {
|
---|
| 126 | runtime.add(`return ${RuntimeGlobals.exports};\n`);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | runtime.add("}\n");
|
---|
| 130 | source.add(",\n");
|
---|
| 131 | source.add(new PrefixSource("/******/ ", runtime));
|
---|
| 132 | }
|
---|
| 133 | source.add("])");
|
---|
| 134 | }
|
---|
| 135 | return source;
|
---|
| 136 | }
|
---|
| 137 | );
|
---|
| 138 | hooks.chunkHash.tap(
|
---|
| 139 | "ArrayPushCallbackChunkFormatPlugin",
|
---|
| 140 | (chunk, hash, { chunkGraph, runtimeTemplate }) => {
|
---|
| 141 | if (chunk.hasRuntime()) return;
|
---|
| 142 | hash.update(
|
---|
| 143 | `ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}`
|
---|
| 144 | );
|
---|
| 145 | const entries = Array.from(
|
---|
| 146 | chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
|
---|
| 147 | );
|
---|
| 148 | updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
|
---|
| 149 | }
|
---|
| 150 | );
|
---|
| 151 | }
|
---|
| 152 | );
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | module.exports = ArrayPushCallbackChunkFormatPlugin;
|
---|