UNPKG

3.99 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const memoize = require("./util/memoize");
10
11/** @typedef {import("../declarations/WebpackOptions").Output} OutputOptions */
12/** @typedef {import("./Compilation")} Compilation */
13
14const getJavascriptModulesPlugin = memoize(() =>
15 require("./javascript/JavascriptModulesPlugin")
16);
17
18// TODO webpack 6 remove this class
19class ChunkTemplate {
20 /**
21 * @param {OutputOptions} outputOptions output options
22 * @param {Compilation} compilation the compilation
23 */
24 constructor(outputOptions, compilation) {
25 this._outputOptions = outputOptions || {};
26 this.hooks = Object.freeze({
27 renderManifest: {
28 tap: util.deprecate(
29 (options, fn) => {
30 compilation.hooks.renderManifest.tap(
31 options,
32 (entries, options) => {
33 if (options.chunk.hasRuntime()) return entries;
34 return fn(entries, options);
35 }
36 );
37 },
38 "ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)",
39 "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST"
40 )
41 },
42 modules: {
43 tap: util.deprecate(
44 (options, fn) => {
45 getJavascriptModulesPlugin()
46 .getCompilationHooks(compilation)
47 .renderChunk.tap(options, (source, renderContext) =>
48 fn(
49 source,
50 compilation.moduleTemplates.javascript,
51 renderContext
52 )
53 );
54 },
55 "ChunkTemplate.hooks.modules is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
56 "DEP_WEBPACK_CHUNK_TEMPLATE_MODULES"
57 )
58 },
59 render: {
60 tap: util.deprecate(
61 (options, fn) => {
62 getJavascriptModulesPlugin()
63 .getCompilationHooks(compilation)
64 .renderChunk.tap(options, (source, renderContext) =>
65 fn(
66 source,
67 compilation.moduleTemplates.javascript,
68 renderContext
69 )
70 );
71 },
72 "ChunkTemplate.hooks.render is deprecated (use JavascriptModulesPlugin.getCompilationHooks().renderChunk instead)",
73 "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER"
74 )
75 },
76 renderWithEntry: {
77 tap: util.deprecate(
78 (options, fn) => {
79 getJavascriptModulesPlugin()
80 .getCompilationHooks(compilation)
81 .render.tap(options, (source, renderContext) => {
82 if (
83 renderContext.chunkGraph.getNumberOfEntryModules(
84 renderContext.chunk
85 ) === 0 ||
86 renderContext.chunk.hasRuntime()
87 ) {
88 return source;
89 }
90 return fn(source, renderContext.chunk);
91 });
92 },
93 "ChunkTemplate.hooks.renderWithEntry is deprecated (use JavascriptModulesPlugin.getCompilationHooks().render instead)",
94 "DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_WITH_ENTRY"
95 )
96 },
97 hash: {
98 tap: util.deprecate(
99 (options, fn) => {
100 compilation.hooks.fullHash.tap(options, fn);
101 },
102 "ChunkTemplate.hooks.hash is deprecated (use Compilation.hooks.fullHash instead)",
103 "DEP_WEBPACK_CHUNK_TEMPLATE_HASH"
104 )
105 },
106 hashForChunk: {
107 tap: util.deprecate(
108 (options, fn) => {
109 getJavascriptModulesPlugin()
110 .getCompilationHooks(compilation)
111 .chunkHash.tap(options, (chunk, hash, context) => {
112 if (chunk.hasRuntime()) return;
113 fn(hash, chunk, context);
114 });
115 },
116 "ChunkTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHash instead)",
117 "DEP_WEBPACK_CHUNK_TEMPLATE_HASH_FOR_CHUNK"
118 )
119 }
120 });
121 }
122}
123
124Object.defineProperty(ChunkTemplate.prototype, "outputOptions", {
125 get: util.deprecate(
126 /**
127 * @this {ChunkTemplate}
128 * @returns {OutputOptions} output options
129 */
130 function () {
131 return this._outputOptions;
132 },
133 "ChunkTemplate.outputOptions is deprecated (use Compilation.outputOptions instead)",
134 "DEP_WEBPACK_CHUNK_TEMPLATE_OUTPUT_OPTIONS"
135 )
136});
137
138module.exports = ChunkTemplate;