1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const parseJson = require("json-parse-better-errors");
|
9 | const { getContext, runLoaders } = require("loader-runner");
|
10 | const querystring = require("querystring");
|
11 | const { validate } = require("schema-utils");
|
12 | const { HookMap, SyncHook, AsyncSeriesBailHook } = require("tapable");
|
13 | const {
|
14 | CachedSource,
|
15 | OriginalSource,
|
16 | RawSource,
|
17 | SourceMapSource
|
18 | } = require("webpack-sources");
|
19 | const Compilation = require("./Compilation");
|
20 | const Module = require("./Module");
|
21 | const ModuleBuildError = require("./ModuleBuildError");
|
22 | const ModuleError = require("./ModuleError");
|
23 | const ModuleGraphConnection = require("./ModuleGraphConnection");
|
24 | const ModuleParseError = require("./ModuleParseError");
|
25 | const ModuleWarning = require("./ModuleWarning");
|
26 | const RuntimeGlobals = require("./RuntimeGlobals");
|
27 | const UnhandledSchemeError = require("./UnhandledSchemeError");
|
28 | const WebpackError = require("./WebpackError");
|
29 | const formatLocation = require("./formatLocation");
|
30 | const LazySet = require("./util/LazySet");
|
31 | const { isSubset } = require("./util/SetHelpers");
|
32 | const { getScheme } = require("./util/URLAbsoluteSpecifier");
|
33 | const {
|
34 | compareLocations,
|
35 | concatComparators,
|
36 | compareSelect,
|
37 | keepOriginalOrder
|
38 | } = require("./util/comparators");
|
39 | const createHash = require("./util/createHash");
|
40 | const { join } = require("./util/fs");
|
41 | const { contextify, absolutify } = require("./util/identifier");
|
42 | const makeSerializable = require("./util/makeSerializable");
|
43 | const memoize = require("./util/memoize");
|
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 | const getInvalidDependenciesModuleWarning = memoize(() =>
|
69 | require("./InvalidDependenciesModuleWarning")
|
70 | );
|
71 |
|
72 | const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/;
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | const contextifySourceUrl = (context, source, associatedObjectForCache) => {
|
89 | if (source.startsWith("webpack://")) return source;
|
90 | return `webpack://${contextify(context, source, associatedObjectForCache)}`;
|
91 | };
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | const contextifySourceMap = (context, sourceMap, associatedObjectForCache) => {
|
100 | if (!Array.isArray(sourceMap.sources)) return sourceMap;
|
101 | const { sourceRoot } = sourceMap;
|
102 |
|
103 | const mapper = !sourceRoot
|
104 | ? source => source
|
105 | : sourceRoot.endsWith("/")
|
106 | ? source =>
|
107 | source.startsWith("/")
|
108 | ? `${sourceRoot.slice(0, -1)}${source}`
|
109 | : `${sourceRoot}${source}`
|
110 | : source =>
|
111 | source.startsWith("/")
|
112 | ? `${sourceRoot}${source}`
|
113 | : `${sourceRoot}/${source}`;
|
114 | const newSources = sourceMap.sources.map(source =>
|
115 | contextifySourceUrl(context, mapper(source), associatedObjectForCache)
|
116 | );
|
117 | return {
|
118 | ...sourceMap,
|
119 | file: "x",
|
120 | sourceRoot: undefined,
|
121 | sources: newSources
|
122 | };
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | const asString = input => {
|
130 | if (Buffer.isBuffer(input)) {
|
131 | return input.toString("utf-8");
|
132 | }
|
133 | return input;
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | const asBuffer = input => {
|
141 | if (!Buffer.isBuffer(input)) {
|
142 | return Buffer.from(input, "utf-8");
|
143 | }
|
144 | return input;
|
145 | };
|
146 |
|
147 | class NonErrorEmittedError extends WebpackError {
|
148 | constructor(error) {
|
149 | super();
|
150 |
|
151 | this.name = "NonErrorEmittedError";
|
152 | this.message = "(Emitted value instead of an instance of Error) " + error;
|
153 |
|
154 | Error.captureStackTrace(this, this.constructor);
|
155 | }
|
156 | }
|
157 |
|
158 | makeSerializable(
|
159 | NonErrorEmittedError,
|
160 | "webpack/lib/NormalModule",
|
161 | "NonErrorEmittedError"
|
162 | );
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | const compilationHooksMap = new WeakMap();
|
173 |
|
174 | class NormalModule extends Module {
|
175 | |
176 |
|
177 |
|
178 |
|
179 | static getCompilationHooks(compilation) {
|
180 | if (!(compilation instanceof Compilation)) {
|
181 | throw new TypeError(
|
182 | "The 'compilation' argument must be an instance of Compilation"
|
183 | );
|
184 | }
|
185 | let hooks = compilationHooksMap.get(compilation);
|
186 | if (hooks === undefined) {
|
187 | hooks = {
|
188 | loader: new SyncHook(["loaderContext", "module"]),
|
189 | beforeLoaders: new SyncHook(["loaders", "module", "loaderContext"]),
|
190 | readResourceForScheme: new HookMap(
|
191 | () => new AsyncSeriesBailHook(["resource", "module"])
|
192 | )
|
193 | };
|
194 | compilationHooksMap.set(compilation, hooks);
|
195 | }
|
196 | return hooks;
|
197 | }
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | constructor({
|
216 | layer,
|
217 | type,
|
218 | request,
|
219 | userRequest,
|
220 | rawRequest,
|
221 | loaders,
|
222 | resource,
|
223 | matchResource,
|
224 | parser,
|
225 | parserOptions,
|
226 | generator,
|
227 | generatorOptions,
|
228 | resolveOptions
|
229 | }) {
|
230 | super(type, getContext(resource), layer);
|
231 |
|
232 |
|
233 |
|
234 | this.request = request;
|
235 |
|
236 | this.userRequest = userRequest;
|
237 |
|
238 | this.rawRequest = rawRequest;
|
239 |
|
240 | this.binary = /^(asset|webassembly)\b/.test(type);
|
241 |
|
242 | this.parser = parser;
|
243 | this.parserOptions = parserOptions;
|
244 |
|
245 | this.generator = generator;
|
246 | this.generatorOptions = generatorOptions;
|
247 |
|
248 | this.resource = resource;
|
249 |
|
250 | this.matchResource = matchResource;
|
251 |
|
252 | this.loaders = loaders;
|
253 | if (resolveOptions !== undefined) {
|
254 |
|
255 | this.resolveOptions = resolveOptions;
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 | this.error = null;
|
261 |
|
262 | this._source = null;
|
263 |
|
264 | this._sourceSizes = undefined;
|
265 |
|
266 |
|
267 | this._lastSuccessfulBuildMeta = {};
|
268 | this._forceBuild = true;
|
269 | this._isEvaluatingSideEffects = false;
|
270 |
|
271 | this._addedSideEffectsBailout = undefined;
|
272 | }
|
273 |
|
274 | |
275 |
|
276 |
|
277 | identifier() {
|
278 | if (this.layer === null) {
|
279 | return this.request;
|
280 | } else {
|
281 | return `${this.request}|${this.layer}`;
|
282 | }
|
283 | }
|
284 |
|
285 | |
286 |
|
287 |
|
288 |
|
289 | readableIdentifier(requestShortener) {
|
290 | return requestShortener.shorten(this.userRequest);
|
291 | }
|
292 |
|
293 | |
294 |
|
295 |
|
296 |
|
297 | libIdent(options) {
|
298 | return contextify(
|
299 | options.context,
|
300 | this.userRequest,
|
301 | options.associatedObjectForCache
|
302 | );
|
303 | }
|
304 |
|
305 | |
306 |
|
307 |
|
308 | nameForCondition() {
|
309 | const resource = this.matchResource || this.resource;
|
310 | const idx = resource.indexOf("?");
|
311 | if (idx >= 0) return resource.substr(0, idx);
|
312 | return resource;
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | updateCacheModule(module) {
|
323 | super.updateCacheModule(module);
|
324 | const m = (module);
|
325 | this.binary = m.binary;
|
326 | this.request = m.request;
|
327 | this.userRequest = m.userRequest;
|
328 | this.rawRequest = m.rawRequest;
|
329 | this.parser = m.parser;
|
330 | this.parserOptions = m.parserOptions;
|
331 | this.generator = m.generator;
|
332 | this.generatorOptions = m.generatorOptions;
|
333 | this.resource = m.resource;
|
334 | this.matchResource = m.matchResource;
|
335 | this.loaders = m.loaders;
|
336 | }
|
337 |
|
338 | |
339 |
|
340 |
|
341 | cleanupForCache() {
|
342 | super.cleanupForCache();
|
343 | this.parser = undefined;
|
344 | this.parserOptions = undefined;
|
345 | this.generator = undefined;
|
346 | this.generatorOptions = undefined;
|
347 | }
|
348 |
|
349 | |
350 |
|
351 |
|
352 |
|
353 |
|
354 | getUnsafeCacheData() {
|
355 | const data = super.getUnsafeCacheData();
|
356 | data.parserOptions = this.parserOptions;
|
357 | data.generatorOptions = this.generatorOptions;
|
358 | return data;
|
359 | }
|
360 |
|
361 | restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
|
362 | this._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
|
363 | }
|
364 |
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 | _restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
|
371 | super._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
|
372 | this.parserOptions = unsafeCacheData.parserOptions;
|
373 | this.parser = normalModuleFactory.getParser(this.type, this.parserOptions);
|
374 | this.generatorOptions = unsafeCacheData.generatorOptions;
|
375 | this.generator = normalModuleFactory.getGenerator(
|
376 | this.type,
|
377 | this.generatorOptions
|
378 | );
|
379 | }
|
380 |
|
381 | |
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | createSourceForAsset(
|
390 | context,
|
391 | name,
|
392 | content,
|
393 | sourceMap,
|
394 | associatedObjectForCache
|
395 | ) {
|
396 | if (sourceMap) {
|
397 | if (
|
398 | typeof sourceMap === "string" &&
|
399 | (this.useSourceMap || this.useSimpleSourceMap)
|
400 | ) {
|
401 | return new OriginalSource(
|
402 | content,
|
403 | contextifySourceUrl(context, sourceMap, associatedObjectForCache)
|
404 | );
|
405 | }
|
406 |
|
407 | if (this.useSourceMap) {
|
408 | return new SourceMapSource(
|
409 | content,
|
410 | name,
|
411 | contextifySourceMap(context, sourceMap, associatedObjectForCache)
|
412 | );
|
413 | }
|
414 | }
|
415 |
|
416 | return new RawSource(content);
|
417 | }
|
418 |
|
419 | |
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | createLoaderContext(resolver, options, compilation, fs) {
|
427 | const { requestShortener } = compilation.runtimeTemplate;
|
428 | const getCurrentLoaderName = () => {
|
429 | const currentLoader = this.getCurrentLoader(loaderContext);
|
430 | if (!currentLoader) return "(not in loader scope)";
|
431 | return requestShortener.shorten(currentLoader.loader);
|
432 | };
|
433 | const getResolveContext = () => {
|
434 | return {
|
435 | fileDependencies: {
|
436 | add: d => loaderContext.addDependency(d)
|
437 | },
|
438 | contextDependencies: {
|
439 | add: d => loaderContext.addContextDependency(d)
|
440 | },
|
441 | missingDependencies: {
|
442 | add: d => loaderContext.addMissingDependency(d)
|
443 | }
|
444 | };
|
445 | };
|
446 | const getAbsolutify = memoize(() =>
|
447 | absolutify.bindCache(compilation.compiler.root)
|
448 | );
|
449 | const getAbsolutifyInContext = memoize(() =>
|
450 | absolutify.bindContextCache(this.context, compilation.compiler.root)
|
451 | );
|
452 | const getContextify = memoize(() =>
|
453 | contextify.bindCache(compilation.compiler.root)
|
454 | );
|
455 | const getContextifyInContext = memoize(() =>
|
456 | contextify.bindContextCache(this.context, compilation.compiler.root)
|
457 | );
|
458 | const utils = {
|
459 | absolutify: (context, request) => {
|
460 | return context === this.context
|
461 | ? getAbsolutifyInContext()(request)
|
462 | : getAbsolutify()(context, request);
|
463 | },
|
464 | contextify: (context, request) => {
|
465 | return context === this.context
|
466 | ? getContextifyInContext()(request)
|
467 | : getContextify()(context, request);
|
468 | }
|
469 | };
|
470 | const loaderContext = {
|
471 | version: 2,
|
472 | getOptions: schema => {
|
473 | const loader = this.getCurrentLoader(loaderContext);
|
474 |
|
475 | let { options } = loader;
|
476 |
|
477 | if (typeof options === "string") {
|
478 | if (options.substr(0, 1) === "{" && options.substr(-1) === "}") {
|
479 | try {
|
480 | options = parseJson(options);
|
481 | } catch (e) {
|
482 | throw new Error(`Cannot parse string options: ${e.message}`);
|
483 | }
|
484 | } else {
|
485 | options = querystring.parse(options, "&", "=", {
|
486 | maxKeys: 0
|
487 | });
|
488 | }
|
489 | }
|
490 |
|
491 | if (options === null || options === undefined) {
|
492 | options = {};
|
493 | }
|
494 |
|
495 | if (schema) {
|
496 | let name = "Loader";
|
497 | let baseDataPath = "options";
|
498 | let match;
|
499 | if (schema.title && (match = /^(.+) (.+)$/.exec(schema.title))) {
|
500 | [, name, baseDataPath] = match;
|
501 | }
|
502 | validate(schema, options, {
|
503 | name,
|
504 | baseDataPath
|
505 | });
|
506 | }
|
507 |
|
508 | return options;
|
509 | },
|
510 | emitWarning: warning => {
|
511 | if (!(warning instanceof Error)) {
|
512 | warning = new NonErrorEmittedError(warning);
|
513 | }
|
514 | this.addWarning(
|
515 | new ModuleWarning(warning, {
|
516 | from: getCurrentLoaderName()
|
517 | })
|
518 | );
|
519 | },
|
520 | emitError: error => {
|
521 | if (!(error instanceof Error)) {
|
522 | error = new NonErrorEmittedError(error);
|
523 | }
|
524 | this.addError(
|
525 | new ModuleError(error, {
|
526 | from: getCurrentLoaderName()
|
527 | })
|
528 | );
|
529 | },
|
530 | getLogger: name => {
|
531 | const currentLoader = this.getCurrentLoader(loaderContext);
|
532 | return compilation.getLogger(() =>
|
533 | [currentLoader && currentLoader.loader, name, this.identifier()]
|
534 | .filter(Boolean)
|
535 | .join("|")
|
536 | );
|
537 | },
|
538 | resolve(context, request, callback) {
|
539 | resolver.resolve({}, context, request, getResolveContext(), callback);
|
540 | },
|
541 | getResolve(options) {
|
542 | const child = options ? resolver.withOptions(options) : resolver;
|
543 | return (context, request, callback) => {
|
544 | if (callback) {
|
545 | child.resolve({}, context, request, getResolveContext(), callback);
|
546 | } else {
|
547 | return new Promise((resolve, reject) => {
|
548 | child.resolve(
|
549 | {},
|
550 | context,
|
551 | request,
|
552 | getResolveContext(),
|
553 | (err, result) => {
|
554 | if (err) reject(err);
|
555 | else resolve(result);
|
556 | }
|
557 | );
|
558 | });
|
559 | }
|
560 | };
|
561 | },
|
562 | emitFile: (name, content, sourceMap, assetInfo) => {
|
563 | if (!this.buildInfo.assets) {
|
564 | this.buildInfo.assets = Object.create(null);
|
565 | this.buildInfo.assetsInfo = new Map();
|
566 | }
|
567 | this.buildInfo.assets[name] = this.createSourceForAsset(
|
568 | options.context,
|
569 | name,
|
570 | content,
|
571 | sourceMap,
|
572 | compilation.compiler.root
|
573 | );
|
574 | this.buildInfo.assetsInfo.set(name, assetInfo);
|
575 | },
|
576 | addBuildDependency: dep => {
|
577 | if (this.buildInfo.buildDependencies === undefined) {
|
578 | this.buildInfo.buildDependencies = new LazySet();
|
579 | }
|
580 | this.buildInfo.buildDependencies.add(dep);
|
581 | },
|
582 | utils,
|
583 | rootContext: options.context,
|
584 | webpack: true,
|
585 | sourceMap: !!this.useSourceMap,
|
586 | mode: options.mode || "production",
|
587 | _module: this,
|
588 | _compilation: compilation,
|
589 | _compiler: compilation.compiler,
|
590 | fs: fs
|
591 | };
|
592 |
|
593 | Object.assign(loaderContext, options.loader);
|
594 |
|
595 | NormalModule.getCompilationHooks(compilation).loader.call(
|
596 | loaderContext,
|
597 | this
|
598 | );
|
599 |
|
600 | return loaderContext;
|
601 | }
|
602 |
|
603 | getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
|
604 | if (
|
605 | this.loaders &&
|
606 | this.loaders.length &&
|
607 | index < this.loaders.length &&
|
608 | index >= 0 &&
|
609 | this.loaders[index]
|
610 | ) {
|
611 | return this.loaders[index];
|
612 | }
|
613 | return null;
|
614 | }
|
615 |
|
616 | |
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 | createSource(context, content, sourceMap, associatedObjectForCache) {
|
624 | if (Buffer.isBuffer(content)) {
|
625 | return new RawSource(content);
|
626 | }
|
627 |
|
628 |
|
629 | if (!this.identifier) {
|
630 | return new RawSource(content);
|
631 | }
|
632 |
|
633 |
|
634 | const identifier = this.identifier();
|
635 |
|
636 | if (this.useSourceMap && sourceMap) {
|
637 | return new SourceMapSource(
|
638 | content,
|
639 | contextifySourceUrl(context, identifier, associatedObjectForCache),
|
640 | contextifySourceMap(context, sourceMap, associatedObjectForCache)
|
641 | );
|
642 | }
|
643 |
|
644 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
645 | return new OriginalSource(
|
646 | content,
|
647 | contextifySourceUrl(context, identifier, associatedObjectForCache)
|
648 | );
|
649 | }
|
650 |
|
651 | return new RawSource(content);
|
652 | }
|
653 |
|
654 | |
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 | doBuild(options, compilation, resolver, fs, callback) {
|
663 | const loaderContext = this.createLoaderContext(
|
664 | resolver,
|
665 | options,
|
666 | compilation,
|
667 | fs
|
668 | );
|
669 |
|
670 | const processResult = (err, result) => {
|
671 | if (err) {
|
672 | if (!(err instanceof Error)) {
|
673 | err = new NonErrorEmittedError(err);
|
674 | }
|
675 | const currentLoader = this.getCurrentLoader(loaderContext);
|
676 | const error = new ModuleBuildError(err, {
|
677 | from:
|
678 | currentLoader &&
|
679 | compilation.runtimeTemplate.requestShortener.shorten(
|
680 | currentLoader.loader
|
681 | )
|
682 | });
|
683 | return callback(error);
|
684 | }
|
685 |
|
686 | const source = result[0];
|
687 | const sourceMap = result.length >= 1 ? result[1] : null;
|
688 | const extraInfo = result.length >= 2 ? result[2] : null;
|
689 |
|
690 | if (!Buffer.isBuffer(source) && typeof source !== "string") {
|
691 | const currentLoader = this.getCurrentLoader(loaderContext, 0);
|
692 | const err = new Error(
|
693 | `Final loader (${
|
694 | currentLoader
|
695 | ? compilation.runtimeTemplate.requestShortener.shorten(
|
696 | currentLoader.loader
|
697 | )
|
698 | : "unknown"
|
699 | }) didn't return a Buffer or String`
|
700 | );
|
701 | const error = new ModuleBuildError(err);
|
702 | return callback(error);
|
703 | }
|
704 |
|
705 | this._source = this.createSource(
|
706 | options.context,
|
707 | this.binary ? asBuffer(source) : asString(source),
|
708 | sourceMap,
|
709 | compilation.compiler.root
|
710 | );
|
711 | if (this._sourceSizes !== undefined) this._sourceSizes.clear();
|
712 | this._ast =
|
713 | typeof extraInfo === "object" &&
|
714 | extraInfo !== null &&
|
715 | extraInfo.webpackAST !== undefined
|
716 | ? extraInfo.webpackAST
|
717 | : null;
|
718 | return callback();
|
719 | };
|
720 |
|
721 | const hooks = NormalModule.getCompilationHooks(compilation);
|
722 |
|
723 | try {
|
724 | hooks.beforeLoaders.call(this.loaders, this, loaderContext);
|
725 | } catch (err) {
|
726 | processResult(err);
|
727 | return;
|
728 | }
|
729 | runLoaders(
|
730 | {
|
731 | resource: this.resource,
|
732 | loaders: this.loaders,
|
733 | context: loaderContext,
|
734 | processResource: (loaderContext, resource, callback) => {
|
735 | const scheme = getScheme(resource);
|
736 | if (scheme) {
|
737 | hooks.readResourceForScheme
|
738 | .for(scheme)
|
739 | .callAsync(resource, this, (err, result) => {
|
740 | if (err) return callback(err);
|
741 | if (typeof result !== "string" && !result) {
|
742 | return callback(new UnhandledSchemeError(scheme, resource));
|
743 | }
|
744 | return callback(null, result);
|
745 | });
|
746 | } else {
|
747 | loaderContext.addDependency(resource);
|
748 | fs.readFile(resource, callback);
|
749 | }
|
750 | }
|
751 | },
|
752 | (err, result) => {
|
753 |
|
754 | loaderContext._compilation = loaderContext._compiler = loaderContext._module = loaderContext.fs = undefined;
|
755 |
|
756 | if (!result) {
|
757 | return processResult(
|
758 | err || new Error("No result from loader-runner processing"),
|
759 | null
|
760 | );
|
761 | }
|
762 | this.buildInfo.fileDependencies = new LazySet();
|
763 | this.buildInfo.fileDependencies.addAll(result.fileDependencies);
|
764 | this.buildInfo.contextDependencies = new LazySet();
|
765 | this.buildInfo.contextDependencies.addAll(result.contextDependencies);
|
766 | this.buildInfo.missingDependencies = new LazySet();
|
767 | this.buildInfo.missingDependencies.addAll(result.missingDependencies);
|
768 | if (
|
769 | this.loaders.length > 0 &&
|
770 | this.buildInfo.buildDependencies === undefined
|
771 | ) {
|
772 | this.buildInfo.buildDependencies = new LazySet();
|
773 | }
|
774 | for (const loader of this.loaders) {
|
775 | this.buildInfo.buildDependencies.add(loader.loader);
|
776 | }
|
777 | this.buildInfo.cacheable = result.cacheable;
|
778 | processResult(err, result.result);
|
779 | }
|
780 | );
|
781 | }
|
782 |
|
783 | |
784 |
|
785 |
|
786 |
|
787 | markModuleAsErrored(error) {
|
788 |
|
789 | this.buildMeta = { ...this._lastSuccessfulBuildMeta };
|
790 | this.error = error;
|
791 | this.addError(error);
|
792 | }
|
793 |
|
794 | applyNoParseRule(rule, content) {
|
795 |
|
796 | if (typeof rule === "string") {
|
797 | return content.startsWith(rule);
|
798 | }
|
799 |
|
800 | if (typeof rule === "function") {
|
801 | return rule(content);
|
802 | }
|
803 |
|
804 | return rule.test(content);
|
805 | }
|
806 |
|
807 |
|
808 |
|
809 |
|
810 | shouldPreventParsing(noParseRule, request) {
|
811 |
|
812 |
|
813 | if (!noParseRule) {
|
814 | return false;
|
815 | }
|
816 |
|
817 |
|
818 | if (!Array.isArray(noParseRule)) {
|
819 |
|
820 | return this.applyNoParseRule(noParseRule, request);
|
821 | }
|
822 |
|
823 | for (let i = 0; i < noParseRule.length; i++) {
|
824 | const rule = noParseRule[i];
|
825 |
|
826 |
|
827 | if (this.applyNoParseRule(rule, request)) {
|
828 | return true;
|
829 | }
|
830 | }
|
831 |
|
832 | return false;
|
833 | }
|
834 |
|
835 | _initBuildHash(compilation) {
|
836 | const hash = createHash(compilation.outputOptions.hashFunction);
|
837 | if (this._source) {
|
838 | hash.update("source");
|
839 | this._source.updateHash(hash);
|
840 | }
|
841 | hash.update("meta");
|
842 | hash.update(JSON.stringify(this.buildMeta));
|
843 | this.buildInfo.hash = (hash.digest("hex"));
|
844 | }
|
845 |
|
846 | |
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 | build(options, compilation, resolver, fs, callback) {
|
855 | this._forceBuild = false;
|
856 | this._source = null;
|
857 | if (this._sourceSizes !== undefined) this._sourceSizes.clear();
|
858 | this._ast = null;
|
859 | this.error = null;
|
860 | this.clearWarningsAndErrors();
|
861 | this.clearDependenciesAndBlocks();
|
862 | this.buildMeta = {};
|
863 | this.buildInfo = {
|
864 | cacheable: false,
|
865 | parsed: true,
|
866 | fileDependencies: undefined,
|
867 | contextDependencies: undefined,
|
868 | missingDependencies: undefined,
|
869 | buildDependencies: undefined,
|
870 | valueDependencies: undefined,
|
871 | hash: undefined,
|
872 | assets: undefined,
|
873 | assetsInfo: undefined
|
874 | };
|
875 |
|
876 | const startTime = Date.now();
|
877 |
|
878 | return this.doBuild(options, compilation, resolver, fs, err => {
|
879 |
|
880 | if (err) {
|
881 | this.markModuleAsErrored(err);
|
882 | this._initBuildHash(compilation);
|
883 | return callback();
|
884 | }
|
885 |
|
886 | const handleParseError = e => {
|
887 | const source = this._source.source();
|
888 | const loaders = this.loaders.map(item =>
|
889 | contextify(options.context, item.loader, compilation.compiler.root)
|
890 | );
|
891 | const error = new ModuleParseError(source, e, loaders, this.type);
|
892 | this.markModuleAsErrored(error);
|
893 | this._initBuildHash(compilation);
|
894 | return callback();
|
895 | };
|
896 |
|
897 | const handleParseResult = result => {
|
898 | this.dependencies.sort(
|
899 | concatComparators(
|
900 | compareSelect(a => a.loc, compareLocations),
|
901 | keepOriginalOrder(this.dependencies)
|
902 | )
|
903 | );
|
904 | this._initBuildHash(compilation);
|
905 | this._lastSuccessfulBuildMeta = this.buildMeta;
|
906 | return handleBuildDone();
|
907 | };
|
908 |
|
909 | const handleBuildDone = () => {
|
910 | const snapshotOptions = compilation.options.snapshot.module;
|
911 | if (!this.buildInfo.cacheable || !snapshotOptions) {
|
912 | return callback();
|
913 | }
|
914 |
|
915 |
|
916 | let nonAbsoluteDependencies = undefined;
|
917 | const checkDependencies = deps => {
|
918 | for (const dep of deps) {
|
919 | if (!ABSOLUTE_PATH_REGEX.test(dep)) {
|
920 | if (nonAbsoluteDependencies === undefined)
|
921 | nonAbsoluteDependencies = new Set();
|
922 | nonAbsoluteDependencies.add(dep);
|
923 | deps.delete(dep);
|
924 | try {
|
925 | const depWithoutGlob = dep.replace(/[\\/]?\*.*$/, "");
|
926 | const absolute = join(
|
927 | compilation.fileSystemInfo.fs,
|
928 | this.context,
|
929 | depWithoutGlob
|
930 | );
|
931 | if (absolute !== dep && ABSOLUTE_PATH_REGEX.test(absolute)) {
|
932 | (depWithoutGlob !== dep
|
933 | ? this.buildInfo.contextDependencies
|
934 | : deps
|
935 | ).add(absolute);
|
936 | }
|
937 | } catch (e) {
|
938 |
|
939 | }
|
940 | }
|
941 | }
|
942 | };
|
943 | checkDependencies(this.buildInfo.fileDependencies);
|
944 | checkDependencies(this.buildInfo.missingDependencies);
|
945 | checkDependencies(this.buildInfo.contextDependencies);
|
946 | if (nonAbsoluteDependencies !== undefined) {
|
947 | const InvalidDependenciesModuleWarning = getInvalidDependenciesModuleWarning();
|
948 | this.addWarning(
|
949 | new InvalidDependenciesModuleWarning(this, nonAbsoluteDependencies)
|
950 | );
|
951 | }
|
952 |
|
953 | compilation.fileSystemInfo.createSnapshot(
|
954 | startTime,
|
955 | this.buildInfo.fileDependencies,
|
956 | this.buildInfo.contextDependencies,
|
957 | this.buildInfo.missingDependencies,
|
958 | snapshotOptions,
|
959 | (err, snapshot) => {
|
960 | if (err) {
|
961 | this.markModuleAsErrored(err);
|
962 | return;
|
963 | }
|
964 | this.buildInfo.fileDependencies = undefined;
|
965 | this.buildInfo.contextDependencies = undefined;
|
966 | this.buildInfo.missingDependencies = undefined;
|
967 | this.buildInfo.snapshot = snapshot;
|
968 | return callback();
|
969 | }
|
970 | );
|
971 | };
|
972 |
|
973 |
|
974 |
|
975 | const noParseRule = options.module && options.module.noParse;
|
976 | if (this.shouldPreventParsing(noParseRule, this.request)) {
|
977 |
|
978 | this.buildInfo.parsed = false;
|
979 | this._initBuildHash(compilation);
|
980 | return handleBuildDone();
|
981 | }
|
982 |
|
983 | let result;
|
984 | try {
|
985 | result = this.parser.parse(this._ast || this._source.source(), {
|
986 | current: this,
|
987 | module: this,
|
988 | compilation: compilation,
|
989 | options: options
|
990 | });
|
991 | } catch (e) {
|
992 | handleParseError(e);
|
993 | return;
|
994 | }
|
995 | handleParseResult(result);
|
996 | });
|
997 | }
|
998 |
|
999 | |
1000 |
|
1001 |
|
1002 |
|
1003 | getConcatenationBailoutReason(context) {
|
1004 | return this.generator.getConcatenationBailoutReason(this, context);
|
1005 | }
|
1006 |
|
1007 | |
1008 |
|
1009 |
|
1010 |
|
1011 | getSideEffectsConnectionState(moduleGraph) {
|
1012 | if (this.factoryMeta !== undefined) {
|
1013 | if (this.factoryMeta.sideEffectFree) return false;
|
1014 | if (this.factoryMeta.sideEffectFree === false) return true;
|
1015 | }
|
1016 | if (this.buildMeta !== undefined && this.buildMeta.sideEffectFree) {
|
1017 | if (this._isEvaluatingSideEffects)
|
1018 | return ModuleGraphConnection.CIRCULAR_CONNECTION;
|
1019 | this._isEvaluatingSideEffects = true;
|
1020 |
|
1021 | let current = false;
|
1022 | for (const dep of this.dependencies) {
|
1023 | const state = dep.getModuleEvaluationSideEffectsState(moduleGraph);
|
1024 | if (state === true) {
|
1025 | if (
|
1026 | this._addedSideEffectsBailout === undefined
|
1027 | ? ((this._addedSideEffectsBailout = new WeakSet()), true)
|
1028 | : !this._addedSideEffectsBailout.has(moduleGraph)
|
1029 | ) {
|
1030 | this._addedSideEffectsBailout.add(moduleGraph);
|
1031 | moduleGraph
|
1032 | .getOptimizationBailout(this)
|
1033 | .push(
|
1034 | () =>
|
1035 | `Dependency (${
|
1036 | dep.type
|
1037 | }) with side effects at ${formatLocation(dep.loc)}`
|
1038 | );
|
1039 | }
|
1040 | this._isEvaluatingSideEffects = false;
|
1041 | return true;
|
1042 | } else if (state !== ModuleGraphConnection.CIRCULAR_CONNECTION) {
|
1043 | current = ModuleGraphConnection.addConnectionStates(current, state);
|
1044 | }
|
1045 | }
|
1046 | this._isEvaluatingSideEffects = false;
|
1047 |
|
1048 |
|
1049 | return current;
|
1050 | } else {
|
1051 | return true;
|
1052 | }
|
1053 | }
|
1054 |
|
1055 | |
1056 |
|
1057 |
|
1058 | getSourceTypes() {
|
1059 | return this.generator.getTypes(this);
|
1060 | }
|
1061 |
|
1062 | |
1063 |
|
1064 |
|
1065 |
|
1066 | codeGeneration({
|
1067 | dependencyTemplates,
|
1068 | runtimeTemplate,
|
1069 | moduleGraph,
|
1070 | chunkGraph,
|
1071 | runtime,
|
1072 | concatenationScope
|
1073 | }) {
|
1074 |
|
1075 | const runtimeRequirements = new Set();
|
1076 |
|
1077 | if (!this.buildInfo.parsed) {
|
1078 | runtimeRequirements.add(RuntimeGlobals.module);
|
1079 | runtimeRequirements.add(RuntimeGlobals.exports);
|
1080 | runtimeRequirements.add(RuntimeGlobals.thisAsExports);
|
1081 | }
|
1082 |
|
1083 |
|
1084 | let data;
|
1085 | const getData = () => {
|
1086 | if (data === undefined) data = new Map();
|
1087 | return data;
|
1088 | };
|
1089 |
|
1090 | const sources = new Map();
|
1091 | for (const type of this.generator.getTypes(this)) {
|
1092 | const source = this.error
|
1093 | ? new RawSource(
|
1094 | "throw new Error(" + JSON.stringify(this.error.message) + ");"
|
1095 | )
|
1096 | : this.generator.generate(this, {
|
1097 | dependencyTemplates,
|
1098 | runtimeTemplate,
|
1099 | moduleGraph,
|
1100 | chunkGraph,
|
1101 | runtimeRequirements,
|
1102 | runtime,
|
1103 | concatenationScope,
|
1104 | getData,
|
1105 | type
|
1106 | });
|
1107 |
|
1108 | if (source) {
|
1109 | sources.set(type, new CachedSource(source));
|
1110 | }
|
1111 | }
|
1112 |
|
1113 |
|
1114 | const resultEntry = {
|
1115 | sources,
|
1116 | runtimeRequirements,
|
1117 | data
|
1118 | };
|
1119 | return resultEntry;
|
1120 | }
|
1121 |
|
1122 | |
1123 |
|
1124 |
|
1125 | originalSource() {
|
1126 | return this._source;
|
1127 | }
|
1128 |
|
1129 | |
1130 |
|
1131 |
|
1132 | invalidateBuild() {
|
1133 | this._forceBuild = true;
|
1134 | }
|
1135 |
|
1136 | |
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 | needBuild({ fileSystemInfo, valueCacheVersions }, callback) {
|
1142 |
|
1143 | if (this._forceBuild) return callback(null, true);
|
1144 |
|
1145 |
|
1146 | if (this.error) return callback(null, true);
|
1147 |
|
1148 |
|
1149 | if (!this.buildInfo.cacheable) return callback(null, true);
|
1150 |
|
1151 |
|
1152 | if (!this.buildInfo.snapshot) return callback(null, true);
|
1153 |
|
1154 |
|
1155 |
|
1156 | const valueDependencies = this.buildInfo.valueDependencies;
|
1157 | if (valueDependencies) {
|
1158 | if (!valueCacheVersions) return callback(null, true);
|
1159 | for (const [key, value] of valueDependencies) {
|
1160 | if (value === undefined) return callback(null, true);
|
1161 | const current = valueCacheVersions.get(key);
|
1162 | if (
|
1163 | value !== current &&
|
1164 | (typeof value === "string" ||
|
1165 | typeof current === "string" ||
|
1166 | current === undefined ||
|
1167 | !isSubset(value, current))
|
1168 | ) {
|
1169 | return callback(null, true);
|
1170 | }
|
1171 | }
|
1172 | }
|
1173 |
|
1174 |
|
1175 | fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
|
1176 | callback(err, !valid);
|
1177 | });
|
1178 | }
|
1179 |
|
1180 | |
1181 |
|
1182 |
|
1183 |
|
1184 | size(type) {
|
1185 | const cachedSize =
|
1186 | this._sourceSizes === undefined ? undefined : this._sourceSizes.get(type);
|
1187 | if (cachedSize !== undefined) {
|
1188 | return cachedSize;
|
1189 | }
|
1190 | const size = Math.max(1, this.generator.getSize(this, type));
|
1191 | if (this._sourceSizes === undefined) {
|
1192 | this._sourceSizes = new Map();
|
1193 | }
|
1194 | this._sourceSizes.set(type, size);
|
1195 | return size;
|
1196 | }
|
1197 |
|
1198 | |
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 | addCacheDependencies(
|
1205 | fileDependencies,
|
1206 | contextDependencies,
|
1207 | missingDependencies,
|
1208 | buildDependencies
|
1209 | ) {
|
1210 | const { snapshot, buildDependencies: buildDeps } = this.buildInfo;
|
1211 | if (snapshot) {
|
1212 | fileDependencies.addAll(snapshot.getFileIterable());
|
1213 | contextDependencies.addAll(snapshot.getContextIterable());
|
1214 | missingDependencies.addAll(snapshot.getMissingIterable());
|
1215 | } else {
|
1216 | const {
|
1217 | fileDependencies: fileDeps,
|
1218 | contextDependencies: contextDeps,
|
1219 | missingDependencies: missingDeps
|
1220 | } = this.buildInfo;
|
1221 | if (fileDeps !== undefined) fileDependencies.addAll(fileDeps);
|
1222 | if (contextDeps !== undefined) contextDependencies.addAll(contextDeps);
|
1223 | if (missingDeps !== undefined) missingDependencies.addAll(missingDeps);
|
1224 | }
|
1225 | if (buildDeps !== undefined) {
|
1226 | buildDependencies.addAll(buildDeps);
|
1227 | }
|
1228 | }
|
1229 |
|
1230 | |
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 | updateHash(hash, context) {
|
1236 | hash.update(this.buildInfo.hash);
|
1237 | this.generator.updateHash(hash, {
|
1238 | module: this,
|
1239 | ...context
|
1240 | });
|
1241 | super.updateHash(hash, context);
|
1242 | }
|
1243 |
|
1244 | serialize(context) {
|
1245 | const { write } = context;
|
1246 |
|
1247 | write(this._source);
|
1248 | write(this._sourceSizes);
|
1249 | write(this.error);
|
1250 | write(this._lastSuccessfulBuildMeta);
|
1251 | write(this._forceBuild);
|
1252 | super.serialize(context);
|
1253 | }
|
1254 |
|
1255 | static deserialize(context) {
|
1256 | const obj = new NormalModule({
|
1257 |
|
1258 | layer: null,
|
1259 | type: "",
|
1260 |
|
1261 | resource: "",
|
1262 | request: null,
|
1263 | userRequest: null,
|
1264 | rawRequest: null,
|
1265 | loaders: null,
|
1266 | matchResource: null,
|
1267 | parser: null,
|
1268 | parserOptions: null,
|
1269 | generator: null,
|
1270 | generatorOptions: null,
|
1271 | resolveOptions: null
|
1272 | });
|
1273 | obj.deserialize(context);
|
1274 | return obj;
|
1275 | }
|
1276 |
|
1277 | deserialize(context) {
|
1278 | const { read } = context;
|
1279 | this._source = read();
|
1280 | this._sourceSizes = read();
|
1281 | this.error = read();
|
1282 | this._lastSuccessfulBuildMeta = read();
|
1283 | this._forceBuild = read();
|
1284 | super.deserialize(context);
|
1285 | }
|
1286 | }
|
1287 |
|
1288 | makeSerializable(NormalModule, "webpack/lib/NormalModule");
|
1289 |
|
1290 | module.exports = NormalModule;
|