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