| 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 util = require("util");
|
|---|
| 9 | const memoize = require("./util/memoize");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("tapable").Tap} Tap */
|
|---|
| 12 | /** @typedef {import("./config/defaults").OutputNormalizedWithDefaults} OutputOptions */
|
|---|
| 13 | /** @typedef {import("./Chunk")} Chunk */
|
|---|
| 14 | /** @typedef {import("./Compilation")} Compilation */
|
|---|
| 15 | /** @typedef {import("./Compilation").ChunkHashContext} ChunkHashContext */
|
|---|
| 16 | /** @typedef {import("./Compilation").Hash} Hash */
|
|---|
| 17 | /** @typedef {import("./Compilation").RenderManifestEntry} RenderManifestEntry */
|
|---|
| 18 | /** @typedef {import("./Compilation").RenderManifestOptions} RenderManifestOptions */
|
|---|
| 19 | /** @typedef {import("./Compilation").Source} Source */
|
|---|
| 20 | /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
|
|---|
| 21 | /** @typedef {import("./javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
|
|---|
| 22 | /**
|
|---|
| 23 | * Defines the if set type used by this module.
|
|---|
| 24 | * @template T
|
|---|
| 25 | * @typedef {import("tapable").IfSet<T>} IfSet
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | const getJavascriptModulesPlugin = memoize(() =>
|
|---|
| 29 | require("./javascript/JavascriptModulesPlugin")
|
|---|
| 30 | );
|
|---|
| 31 |
|
|---|
| 32 | // TODO webpack 6 remove this class
|
|---|
| 33 | class ChunkTemplate {
|
|---|
| 34 | /**
|
|---|
| 35 | * Creates an instance of ChunkTemplate.
|
|---|
| 36 | * @param {OutputOptions} outputOptions output options
|
|---|
| 37 | * @param {Compilation} compilation the compilation
|
|---|
| 38 | */
|
|---|
| 39 | constructor(outputOptions, compilation) {
|
|---|
| 40 | this._outputOptions = outputOptions || {};
|
|---|
| 41 | this.hooks = Object.freeze({
|
|---|
| 42 | renderManifest: {
|
|---|
| 43 | tap: util.deprecate(
|
|---|
| 44 | /**
|
|---|
| 45 | * Handles the callback logic for this hook.
|
|---|
| 46 | * @template AdditionalOptions
|
|---|
| 47 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 48 | * @param {(renderManifestEntries: RenderManifestEntry[], renderManifestOptions: RenderManifestOptions) => RenderManifestEntry[]} fn function
|
|---|
| 49 | */
|
|---|
| 50 | (options, fn) => {
|
|---|
| 51 | compilation.hooks.renderManifest.tap(
|
|---|
| 52 | options,
|
|---|
| 53 | (entries, options) => {
|
|---|
| 54 | if (options.chunk.hasRuntime()) return entries;
|
|---|
| 55 | return fn(entries, options);
|
|---|
| 56 | }
|
|---|
| 57 | );
|
|---|
| 58 | },
|
|---|
| 59 | "ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
|
|---|
| 60 | "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST"
|
|---|
| 61 | )
|
|---|
| 62 | },
|
|---|
| 63 | modules: {
|
|---|
| 64 | tap: util.deprecate(
|
|---|
| 65 | /**
|
|---|
| 66 | * Handles the callback logic for this hook.
|
|---|
| 67 | * @template AdditionalOptions
|
|---|
| 68 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 69 | * @param {(source: Source, moduleTemplate: ModuleTemplate, renderContext: RenderContext) => Source} fn function
|
|---|
| 70 | */
|
|---|
| 71 | (options, fn) => {
|
|---|
| 72 | getJavascriptModulesPlugin()
|
|---|
| 73 | .getCompilationHooks(compilation)
|
|---|
| 74 | .renderChunk.tap(options, (source, renderContext) =>
|
|---|
| 75 | fn(
|
|---|
| 76 | source,
|
|---|
| 77 | compilation.moduleTemplates.javascript,
|
|---|
| 78 | renderContext
|
|---|
| 79 | )
|
|---|
| 80 | );
|
|---|
| 81 | },
|
|---|
| 82 | "ChunkTemplate.hooks.modules is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
|
|---|
| 83 | "DEP_WEBPACK_CHUNK_TEMPLATE_MODULES"
|
|---|
| 84 | )
|
|---|
| 85 | },
|
|---|
| 86 | render: {
|
|---|
| 87 | tap: util.deprecate(
|
|---|
| 88 | /**
|
|---|
| 89 | * Handles the callback logic for this hook.
|
|---|
| 90 | * @template AdditionalOptions
|
|---|
| 91 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 92 | * @param {(source: Source, moduleTemplate: ModuleTemplate, renderContext: RenderContext) => Source} fn function
|
|---|
| 93 | */
|
|---|
| 94 | (options, fn) => {
|
|---|
| 95 | getJavascriptModulesPlugin()
|
|---|
| 96 | .getCompilationHooks(compilation)
|
|---|
| 97 | .renderChunk.tap(options, (source, renderContext) =>
|
|---|
| 98 | fn(
|
|---|
| 99 | source,
|
|---|
| 100 | compilation.moduleTemplates.javascript,
|
|---|
| 101 | renderContext
|
|---|
| 102 | )
|
|---|
| 103 | );
|
|---|
| 104 | },
|
|---|
| 105 | "ChunkTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
|
|---|
| 106 | "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER"
|
|---|
| 107 | )
|
|---|
| 108 | },
|
|---|
| 109 | renderWithEntry: {
|
|---|
| 110 | tap: util.deprecate(
|
|---|
| 111 | /**
|
|---|
| 112 | * Handles the callback logic for this hook.
|
|---|
| 113 | * @template AdditionalOptions
|
|---|
| 114 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 115 | * @param {(source: Source, chunk: Chunk) => Source} fn function
|
|---|
| 116 | */
|
|---|
| 117 | (options, fn) => {
|
|---|
| 118 | getJavascriptModulesPlugin()
|
|---|
| 119 | .getCompilationHooks(compilation)
|
|---|
| 120 | .render.tap(options, (source, renderContext) => {
|
|---|
| 121 | if (
|
|---|
| 122 | renderContext.chunkGraph.getNumberOfEntryModules(
|
|---|
| 123 | renderContext.chunk
|
|---|
| 124 | ) === 0 ||
|
|---|
| 125 | renderContext.chunk.hasRuntime()
|
|---|
| 126 | ) {
|
|---|
| 127 | return source;
|
|---|
| 128 | }
|
|---|
| 129 | return fn(source, renderContext.chunk);
|
|---|
| 130 | });
|
|---|
| 131 | },
|
|---|
| 132 | "ChunkTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
|
|---|
| 133 | "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_WITH_ENTRY"
|
|---|
| 134 | )
|
|---|
| 135 | },
|
|---|
| 136 | hash: {
|
|---|
| 137 | tap: util.deprecate(
|
|---|
| 138 | /**
|
|---|
| 139 | * Handles the callback logic for this hook.
|
|---|
| 140 | * @template AdditionalOptions
|
|---|
| 141 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 142 | * @param {(hash: Hash) => void} fn function
|
|---|
| 143 | */
|
|---|
| 144 | (options, fn) => {
|
|---|
| 145 | compilation.hooks.fullHash.tap(options, fn);
|
|---|
| 146 | },
|
|---|
| 147 | "ChunkTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
|
|---|
| 148 | "DEP_WEBPACK_CHUNK_TEMPLATE_HASH"
|
|---|
| 149 | )
|
|---|
| 150 | },
|
|---|
| 151 | hashForChunk: {
|
|---|
| 152 | tap: util.deprecate(
|
|---|
| 153 | /**
|
|---|
| 154 | * Handles the callback logic for this hook.
|
|---|
| 155 | * @template AdditionalOptions
|
|---|
| 156 | * @param {string | Tap & IfSet<AdditionalOptions>} options options
|
|---|
| 157 | * @param {(hash: Hash, chunk: Chunk, chunkHashContext: ChunkHashContext) => void} fn function
|
|---|
| 158 | */
|
|---|
| 159 | (options, fn) => {
|
|---|
| 160 | getJavascriptModulesPlugin()
|
|---|
| 161 | .getCompilationHooks(compilation)
|
|---|
| 162 | .chunkHash.tap(options, (chunk, hash, context) => {
|
|---|
| 163 | if (chunk.hasRuntime()) return;
|
|---|
| 164 | fn(hash, chunk, context);
|
|---|
| 165 | });
|
|---|
| 166 | },
|
|---|
| 167 | "ChunkTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
|
|---|
| 168 | "DEP_WEBPACK_CHUNK_TEMPLATE_HASH_FOR_CHUNK"
|
|---|
| 169 | )
|
|---|
| 170 | }
|
|---|
| 171 | });
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | Object.defineProperty(ChunkTemplate.prototype, "outputOptions", {
|
|---|
| 176 | get: util.deprecate(
|
|---|
| 177 | /**
|
|---|
| 178 | * Returns output options.
|
|---|
| 179 | * @this {ChunkTemplate}
|
|---|
| 180 | * @returns {OutputOptions} output options
|
|---|
| 181 | */
|
|---|
| 182 | function outputOptions() {
|
|---|
| 183 | return this._outputOptions;
|
|---|
| 184 | },
|
|---|
| 185 | "ChunkTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
|
|---|
| 186 | "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
|
|---|
| 187 | )
|
|---|
| 188 | });
|
|---|
| 189 |
|
|---|
| 190 | module.exports = ChunkTemplate;
|
|---|