1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Yuta Hiroto @hiroppy
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const {
|
---|
9 | ASSET_MODULE_TYPE_RESOURCE,
|
---|
10 | ASSET_MODULE_TYPE_INLINE,
|
---|
11 | ASSET_MODULE_TYPE,
|
---|
12 | ASSET_MODULE_TYPE_SOURCE
|
---|
13 | } = require("../ModuleTypeConstants");
|
---|
14 | const { cleverMerge } = require("../util/cleverMerge");
|
---|
15 | const { compareModulesByIdentifier } = require("../util/comparators");
|
---|
16 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
17 | const memoize = require("../util/memoize");
|
---|
18 |
|
---|
19 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
20 | /** @typedef {import("../../declarations/WebpackOptions").AssetParserOptions} AssetParserOptions */
|
---|
21 | /** @typedef {import("../Chunk")} Chunk */
|
---|
22 | /** @typedef {import("../Compiler")} Compiler */
|
---|
23 | /** @typedef {import("../Module")} Module */
|
---|
24 | /** @typedef {import("../Module").BuildInfo} BuildInfo */
|
---|
25 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @param {string} name name of definitions
|
---|
29 | * @returns {TODO} definition
|
---|
30 | */
|
---|
31 | const getSchema = name => {
|
---|
32 | const { definitions } = require("../../schemas/WebpackOptions.json");
|
---|
33 | return {
|
---|
34 | definitions,
|
---|
35 | oneOf: [{ $ref: `#/definitions/${name}` }]
|
---|
36 | };
|
---|
37 | };
|
---|
38 |
|
---|
39 | const generatorValidationOptions = {
|
---|
40 | name: "Asset Modules Plugin",
|
---|
41 | baseDataPath: "generator"
|
---|
42 | };
|
---|
43 | const validateGeneratorOptions = {
|
---|
44 | asset: createSchemaValidation(
|
---|
45 | require("../../schemas/plugins/asset/AssetGeneratorOptions.check.js"),
|
---|
46 | () => getSchema("AssetGeneratorOptions"),
|
---|
47 | generatorValidationOptions
|
---|
48 | ),
|
---|
49 | "asset/resource": createSchemaValidation(
|
---|
50 | require("../../schemas/plugins/asset/AssetResourceGeneratorOptions.check.js"),
|
---|
51 | () => getSchema("AssetResourceGeneratorOptions"),
|
---|
52 | generatorValidationOptions
|
---|
53 | ),
|
---|
54 | "asset/inline": createSchemaValidation(
|
---|
55 | require("../../schemas/plugins/asset/AssetInlineGeneratorOptions.check.js"),
|
---|
56 | () => getSchema("AssetInlineGeneratorOptions"),
|
---|
57 | generatorValidationOptions
|
---|
58 | )
|
---|
59 | };
|
---|
60 |
|
---|
61 | const validateParserOptions = createSchemaValidation(
|
---|
62 | require("../../schemas/plugins/asset/AssetParserOptions.check.js"),
|
---|
63 | () => getSchema("AssetParserOptions"),
|
---|
64 | {
|
---|
65 | name: "Asset Modules Plugin",
|
---|
66 | baseDataPath: "parser"
|
---|
67 | }
|
---|
68 | );
|
---|
69 |
|
---|
70 | const getAssetGenerator = memoize(() => require("./AssetGenerator"));
|
---|
71 | const getAssetParser = memoize(() => require("./AssetParser"));
|
---|
72 | const getAssetSourceParser = memoize(() => require("./AssetSourceParser"));
|
---|
73 | const getAssetSourceGenerator = memoize(() =>
|
---|
74 | require("./AssetSourceGenerator")
|
---|
75 | );
|
---|
76 |
|
---|
77 | const type = ASSET_MODULE_TYPE;
|
---|
78 | const plugin = "AssetModulesPlugin";
|
---|
79 |
|
---|
80 | class AssetModulesPlugin {
|
---|
81 | /**
|
---|
82 | * Apply the plugin
|
---|
83 | * @param {Compiler} compiler the compiler instance
|
---|
84 | * @returns {void}
|
---|
85 | */
|
---|
86 | apply(compiler) {
|
---|
87 | compiler.hooks.compilation.tap(
|
---|
88 | plugin,
|
---|
89 | (compilation, { normalModuleFactory }) => {
|
---|
90 | normalModuleFactory.hooks.createParser
|
---|
91 | .for(ASSET_MODULE_TYPE)
|
---|
92 | .tap(plugin, parserOptions => {
|
---|
93 | validateParserOptions(parserOptions);
|
---|
94 | parserOptions = cleverMerge(
|
---|
95 | /** @type {AssetParserOptions} */
|
---|
96 | (compiler.options.module.parser.asset),
|
---|
97 | parserOptions
|
---|
98 | );
|
---|
99 |
|
---|
100 | let dataUrlCondition = parserOptions.dataUrlCondition;
|
---|
101 | if (!dataUrlCondition || typeof dataUrlCondition === "object") {
|
---|
102 | dataUrlCondition = {
|
---|
103 | maxSize: 8096,
|
---|
104 | ...dataUrlCondition
|
---|
105 | };
|
---|
106 | }
|
---|
107 |
|
---|
108 | const AssetParser = getAssetParser();
|
---|
109 |
|
---|
110 | return new AssetParser(dataUrlCondition);
|
---|
111 | });
|
---|
112 | normalModuleFactory.hooks.createParser
|
---|
113 | .for(ASSET_MODULE_TYPE_INLINE)
|
---|
114 | .tap(plugin, _parserOptions => {
|
---|
115 | const AssetParser = getAssetParser();
|
---|
116 |
|
---|
117 | return new AssetParser(true);
|
---|
118 | });
|
---|
119 | normalModuleFactory.hooks.createParser
|
---|
120 | .for(ASSET_MODULE_TYPE_RESOURCE)
|
---|
121 | .tap(plugin, _parserOptions => {
|
---|
122 | const AssetParser = getAssetParser();
|
---|
123 |
|
---|
124 | return new AssetParser(false);
|
---|
125 | });
|
---|
126 | normalModuleFactory.hooks.createParser
|
---|
127 | .for(ASSET_MODULE_TYPE_SOURCE)
|
---|
128 | .tap(plugin, _parserOptions => {
|
---|
129 | const AssetSourceParser = getAssetSourceParser();
|
---|
130 |
|
---|
131 | return new AssetSourceParser();
|
---|
132 | });
|
---|
133 |
|
---|
134 | for (const type of [
|
---|
135 | ASSET_MODULE_TYPE,
|
---|
136 | ASSET_MODULE_TYPE_INLINE,
|
---|
137 | ASSET_MODULE_TYPE_RESOURCE
|
---|
138 | ]) {
|
---|
139 | normalModuleFactory.hooks.createGenerator
|
---|
140 | .for(type)
|
---|
141 | .tap(plugin, generatorOptions => {
|
---|
142 | validateGeneratorOptions[type](generatorOptions);
|
---|
143 |
|
---|
144 | let dataUrl;
|
---|
145 | if (type !== ASSET_MODULE_TYPE_RESOURCE) {
|
---|
146 | dataUrl = generatorOptions.dataUrl;
|
---|
147 | if (!dataUrl || typeof dataUrl === "object") {
|
---|
148 | dataUrl = {
|
---|
149 | encoding: undefined,
|
---|
150 | mimetype: undefined,
|
---|
151 | ...dataUrl
|
---|
152 | };
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | let filename;
|
---|
157 | let publicPath;
|
---|
158 | let outputPath;
|
---|
159 | if (type !== ASSET_MODULE_TYPE_INLINE) {
|
---|
160 | filename = generatorOptions.filename;
|
---|
161 | publicPath = generatorOptions.publicPath;
|
---|
162 | outputPath = generatorOptions.outputPath;
|
---|
163 | }
|
---|
164 |
|
---|
165 | const AssetGenerator = getAssetGenerator();
|
---|
166 |
|
---|
167 | return new AssetGenerator(
|
---|
168 | compilation.moduleGraph,
|
---|
169 | dataUrl,
|
---|
170 | filename,
|
---|
171 | publicPath,
|
---|
172 | outputPath,
|
---|
173 | generatorOptions.emit !== false
|
---|
174 | );
|
---|
175 | });
|
---|
176 | }
|
---|
177 | normalModuleFactory.hooks.createGenerator
|
---|
178 | .for(ASSET_MODULE_TYPE_SOURCE)
|
---|
179 | .tap(plugin, () => {
|
---|
180 | const AssetSourceGenerator = getAssetSourceGenerator();
|
---|
181 |
|
---|
182 | return new AssetSourceGenerator(compilation.moduleGraph);
|
---|
183 | });
|
---|
184 |
|
---|
185 | compilation.hooks.renderManifest.tap(plugin, (result, options) => {
|
---|
186 | const { chunkGraph } = compilation;
|
---|
187 | const { chunk, codeGenerationResults } = options;
|
---|
188 |
|
---|
189 | const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
|
---|
190 | chunk,
|
---|
191 | ASSET_MODULE_TYPE,
|
---|
192 | compareModulesByIdentifier
|
---|
193 | );
|
---|
194 | if (modules) {
|
---|
195 | for (const module of modules) {
|
---|
196 | try {
|
---|
197 | const codeGenResult = codeGenerationResults.get(
|
---|
198 | module,
|
---|
199 | chunk.runtime
|
---|
200 | );
|
---|
201 | const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
|
---|
202 | const data =
|
---|
203 | /** @type {NonNullable<CodeGenerationResult["data"]>} */
|
---|
204 | (codeGenResult.data);
|
---|
205 | const errored = module.getNumberOfErrors() > 0;
|
---|
206 | result.push({
|
---|
207 | render: () =>
|
---|
208 | /** @type {Source} */ (codeGenResult.sources.get(type)),
|
---|
209 | filename: errored
|
---|
210 | ? module.nameForCondition()
|
---|
211 | : buildInfo.filename || data.get("filename"),
|
---|
212 | info: buildInfo.assetInfo || data.get("assetInfo"),
|
---|
213 | auxiliary: true,
|
---|
214 | identifier: `assetModule${chunkGraph.getModuleId(module)}`,
|
---|
215 | hash: errored
|
---|
216 | ? chunkGraph.getModuleHash(module, chunk.runtime)
|
---|
217 | : buildInfo.fullContentHash || data.get("fullContentHash")
|
---|
218 | });
|
---|
219 | } catch (err) {
|
---|
220 | /** @type {Error} */ (err).message +=
|
---|
221 | `\nduring rendering of asset ${module.identifier()}`;
|
---|
222 | throw err;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | return result;
|
---|
228 | });
|
---|
229 |
|
---|
230 | compilation.hooks.prepareModuleExecution.tap(
|
---|
231 | "AssetModulesPlugin",
|
---|
232 | (options, context) => {
|
---|
233 | const { codeGenerationResult } = options;
|
---|
234 | const source = codeGenerationResult.sources.get(ASSET_MODULE_TYPE);
|
---|
235 | if (source === undefined) return;
|
---|
236 | const data =
|
---|
237 | /** @type {NonNullable<CodeGenerationResult["data"]>} */
|
---|
238 | (codeGenerationResult.data);
|
---|
239 | context.assets.set(data.get("filename"), {
|
---|
240 | source,
|
---|
241 | info: data.get("assetInfo")
|
---|
242 | });
|
---|
243 | }
|
---|
244 | );
|
---|
245 | }
|
---|
246 | );
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | module.exports = AssetModulesPlugin;
|
---|