UNPKG

38 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 parseJson = require("json-parse-better-errors");
9const { getContext, runLoaders } = require("loader-runner");
10const querystring = require("querystring");
11const { validate } = require("schema-utils");
12const { HookMap, SyncHook, AsyncSeriesBailHook } = require("tapable");
13const {
14 CachedSource,
15 OriginalSource,
16 RawSource,
17 SourceMapSource
18} = require("webpack-sources");
19const Compilation = require("./Compilation");
20const Module = require("./Module");
21const ModuleBuildError = require("./ModuleBuildError");
22const ModuleError = require("./ModuleError");
23const ModuleGraphConnection = require("./ModuleGraphConnection");
24const ModuleParseError = require("./ModuleParseError");
25const ModuleWarning = require("./ModuleWarning");
26const RuntimeGlobals = require("./RuntimeGlobals");
27const UnhandledSchemeError = require("./UnhandledSchemeError");
28const WebpackError = require("./WebpackError");
29const formatLocation = require("./formatLocation");
30const LazySet = require("./util/LazySet");
31const { isSubset } = require("./util/SetHelpers");
32const { getScheme } = require("./util/URLAbsoluteSpecifier");
33const {
34 compareLocations,
35 concatComparators,
36 compareSelect,
37 keepOriginalOrder
38} = require("./util/comparators");
39const createHash = require("./util/createHash");
40const { join } = require("./util/fs");
41const { contextify, absolutify } = require("./util/identifier");
42const makeSerializable = require("./util/makeSerializable");
43const memoize = require("./util/memoize");
44
45/** @typedef {import("source-map").RawSourceMap} SourceMap */
46/** @typedef {import("webpack-sources").Source} Source */
47/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
48/** @typedef {import("./ChunkGraph")} ChunkGraph */
49/** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
50/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
51/** @typedef {import("./Generator")} Generator */
52/** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
53/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
54/** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
55/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
56/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
57/** @typedef {import("./ModuleGraph")} ModuleGraph */
58/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
59/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
60/** @typedef {import("./Parser")} Parser */
61/** @typedef {import("./RequestShortener")} RequestShortener */
62/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
63/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
64/** @typedef {import("./util/Hash")} Hash */
65/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
66/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
67
68const getInvalidDependenciesModuleWarning = memoize(() =>
69 require("./InvalidDependenciesModuleWarning")
70);
71
72const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/;
73
74/**
75 * @typedef {Object} LoaderItem
76 * @property {string} loader
77 * @property {any} options
78 * @property {string?} ident
79 * @property {string?} type
80 */
81
82/**
83 * @param {string} context absolute context path
84 * @param {string} source a source path
85 * @param {Object=} associatedObjectForCache an object to which the cache will be attached
86 * @returns {string} new source path
87 */
88const contextifySourceUrl = (context, source, associatedObjectForCache) => {
89 if (source.startsWith("webpack://")) return source;
90 return `webpack://${contextify(context, source, associatedObjectForCache)}`;
91};
92
93/**
94 * @param {string} context absolute context path
95 * @param {SourceMap} sourceMap a source map
96 * @param {Object=} associatedObjectForCache an object to which the cache will be attached
97 * @returns {SourceMap} new source map
98 */
99const contextifySourceMap = (context, sourceMap, associatedObjectForCache) => {
100 if (!Array.isArray(sourceMap.sources)) return sourceMap;
101 const { sourceRoot } = sourceMap;
102 /** @type {function(string): string} */
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 * @param {string | Buffer} input the input
127 * @returns {string} the converted string
128 */
129const asString = input => {
130 if (Buffer.isBuffer(input)) {
131 return input.toString("utf-8");
132 }
133 return input;
134};
135
136/**
137 * @param {string | Buffer} input the input
138 * @returns {Buffer} the converted buffer
139 */
140const asBuffer = input => {
141 if (!Buffer.isBuffer(input)) {
142 return Buffer.from(input, "utf-8");
143 }
144 return input;
145};
146
147class 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
158makeSerializable(
159 NonErrorEmittedError,
160 "webpack/lib/NormalModule",
161 "NonErrorEmittedError"
162);
163
164/**
165 * @typedef {Object} NormalModuleCompilationHooks
166 * @property {SyncHook<[object, NormalModule]>} loader
167 * @property {SyncHook<[LoaderItem[], NormalModule, object]>} beforeLoaders
168 * @property {HookMap<AsyncSeriesBailHook<[string, NormalModule], string | Buffer>>} readResourceForScheme
169 */
170
171/** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
172const compilationHooksMap = new WeakMap();
173
174class NormalModule extends Module {
175 /**
176 * @param {Compilation} compilation the compilation
177 * @returns {NormalModuleCompilationHooks} the attached hooks
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 * @param {Object} options options object
201 * @param {string=} options.layer an optional layer in which the module is
202 * @param {string} options.type module type
203 * @param {string} options.request request string
204 * @param {string} options.userRequest request intended by user (without loaders from config)
205 * @param {string} options.rawRequest request without resolving
206 * @param {LoaderItem[]} options.loaders list of loaders
207 * @param {string} options.resource path + query of the real resource
208 * @param {string | undefined} options.matchResource path + query of the matched resource (virtual)
209 * @param {Parser} options.parser the parser used
210 * @param {object} options.parserOptions the options of the parser used
211 * @param {Generator} options.generator the generator used
212 * @param {object} options.generatorOptions the options of the generator used
213 * @param {Object} options.resolveOptions options used for resolving requests from this module
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 // Info from Factory
233 /** @type {string} */
234 this.request = request;
235 /** @type {string} */
236 this.userRequest = userRequest;
237 /** @type {string} */
238 this.rawRequest = rawRequest;
239 /** @type {boolean} */
240 this.binary = /^(asset|webassembly)\b/.test(type);
241 /** @type {Parser} */
242 this.parser = parser;
243 this.parserOptions = parserOptions;
244 /** @type {Generator} */
245 this.generator = generator;
246 this.generatorOptions = generatorOptions;
247 /** @type {string} */
248 this.resource = resource;
249 /** @type {string | undefined} */
250 this.matchResource = matchResource;
251 /** @type {LoaderItem[]} */
252 this.loaders = loaders;
253 if (resolveOptions !== undefined) {
254 // already declared in super class
255 this.resolveOptions = resolveOptions;
256 }
257
258 // Info from Build
259 /** @type {WebpackError=} */
260 this.error = null;
261 /** @private @type {Source=} */
262 this._source = null;
263 /** @private @type {Map<string, number> | undefined} **/
264 this._sourceSizes = undefined;
265
266 // Cache
267 this._lastSuccessfulBuildMeta = {};
268 this._forceBuild = true;
269 this._isEvaluatingSideEffects = false;
270 /** @type {WeakSet<ModuleGraph> | undefined} */
271 this._addedSideEffectsBailout = undefined;
272 }
273
274 /**
275 * @returns {string} a unique identifier of the module
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 * @param {RequestShortener} requestShortener the request shortener
287 * @returns {string} a user readable identifier of the module
288 */
289 readableIdentifier(requestShortener) {
290 return requestShortener.shorten(this.userRequest);
291 }
292
293 /**
294 * @param {LibIdentOptions} options options
295 * @returns {string | null} an identifier for library inclusion
296 */
297 libIdent(options) {
298 return contextify(
299 options.context,
300 this.userRequest,
301 options.associatedObjectForCache
302 );
303 }
304
305 /**
306 * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
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 * Assuming this module is in the cache. Update the (cached) module with
317 * the fresh module from the factory. Usually updates internal references
318 * and properties.
319 * @param {Module} module fresh module
320 * @returns {void}
321 */
322 updateCacheModule(module) {
323 super.updateCacheModule(module);
324 const m = /** @type {NormalModule} */ (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 * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
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 * Module should be unsafe cached. Get data that's needed for that.
351 * This data will be passed to restoreFromUnsafeCache later.
352 * @returns {object} cached data
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 * restore unsafe cache data
367 * @param {object} unsafeCacheData data from getUnsafeCacheData
368 * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
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 * @param {string} context the compilation context
383 * @param {string} name the asset name
384 * @param {string} content the content
385 * @param {string | TODO} sourceMap an optional source map
386 * @param {Object=} associatedObjectForCache object for caching
387 * @returns {Source} the created source
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 * @param {ResolverWithOptions} resolver a resolver
421 * @param {WebpackOptions} options webpack options
422 * @param {Compilation} compilation the compilation
423 * @param {InputFileSystem} fs file system from reading
424 * @returns {any} loader context
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 * @param {string} context the compilation context
618 * @param {string | Buffer} content the content
619 * @param {string | TODO} sourceMap an optional source map
620 * @param {Object=} associatedObjectForCache object for caching
621 * @returns {Source} the created source
622 */
623 createSource(context, content, sourceMap, associatedObjectForCache) {
624 if (Buffer.isBuffer(content)) {
625 return new RawSource(content);
626 }
627
628 // if there is no identifier return raw source
629 if (!this.identifier) {
630 return new RawSource(content);
631 }
632
633 // from here on we assume we have an identifier
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 * @param {WebpackOptions} options webpack options
656 * @param {Compilation} compilation the compilation
657 * @param {ResolverWithOptions} resolver the resolver
658 * @param {InputFileSystem} fs the file system
659 * @param {function(WebpackError=): void} callback callback function
660 * @returns {void}
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 // Cleanup loaderContext to avoid leaking memory in ICs
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 * @param {WebpackError} error the error
785 * @returns {void}
786 */
787 markModuleAsErrored(error) {
788 // Restore build meta from successful build to keep importing state
789 this.buildMeta = { ...this._lastSuccessfulBuildMeta };
790 this.error = error;
791 this.addError(error);
792 }
793
794 applyNoParseRule(rule, content) {
795 // must start with "rule" if rule is a string
796 if (typeof rule === "string") {
797 return content.startsWith(rule);
798 }
799
800 if (typeof rule === "function") {
801 return rule(content);
802 }
803 // we assume rule is a regexp
804 return rule.test(content);
805 }
806
807 // check if module should not be parsed
808 // returns "true" if the module should !not! be parsed
809 // returns "false" if the module !must! be parsed
810 shouldPreventParsing(noParseRule, request) {
811 // if no noParseRule exists, return false
812 // the module !must! be parsed.
813 if (!noParseRule) {
814 return false;
815 }
816
817 // we only have one rule to check
818 if (!Array.isArray(noParseRule)) {
819 // returns "true" if the module is !not! to be parsed
820 return this.applyNoParseRule(noParseRule, request);
821 }
822
823 for (let i = 0; i < noParseRule.length; i++) {
824 const rule = noParseRule[i];
825 // early exit on first truthy match
826 // this module is !not! to be parsed
827 if (this.applyNoParseRule(rule, request)) {
828 return true;
829 }
830 }
831 // no match found, so this module !should! be parsed
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 = /** @type {string} */ (hash.digest("hex"));
844 }
845
846 /**
847 * @param {WebpackOptions} options webpack options
848 * @param {Compilation} compilation the compilation
849 * @param {ResolverWithOptions} resolver the resolver
850 * @param {InputFileSystem} fs the file system
851 * @param {function(WebpackError=): void} callback callback function
852 * @returns {void}
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 // if we have an error mark module as failed and exit
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 // add warning for all non-absolute paths in fileDependencies, etc
915 // This makes it easier to find problems with watching and/or caching
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 // ignore
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 // convert file/context/missingDependencies into filesystem snapshot
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 // check if this module should !not! be parsed.
974 // if so, exit here;
975 const noParseRule = options.module && options.module.noParse;
976 if (this.shouldPreventParsing(noParseRule, this.request)) {
977 // We assume that we need module and exports
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 * @param {ConcatenationBailoutReasonContext} context context
1001 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
1002 */
1003 getConcatenationBailoutReason(context) {
1004 return this.generator.getConcatenationBailoutReason(this, context);
1005 }
1006
1007 /**
1008 * @param {ModuleGraph} moduleGraph the module graph
1009 * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
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 /** @type {ConnectionState} */
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 // When caching is implemented here, make sure to not cache when
1048 // at least one circular connection was in the loop above
1049 return current;
1050 } else {
1051 return true;
1052 }
1053 }
1054
1055 /**
1056 * @returns {Set<string>} types available (do not mutate)
1057 */
1058 getSourceTypes() {
1059 return this.generator.getTypes(this);
1060 }
1061
1062 /**
1063 * @param {CodeGenerationContext} context context for code generation
1064 * @returns {CodeGenerationResult} result
1065 */
1066 codeGeneration({
1067 dependencyTemplates,
1068 runtimeTemplate,
1069 moduleGraph,
1070 chunkGraph,
1071 runtime,
1072 concatenationScope
1073 }) {
1074 /** @type {Set<string>} */
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 /** @type {Map<string, any>} */
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 /** @type {CodeGenerationResult} */
1114 const resultEntry = {
1115 sources,
1116 runtimeRequirements,
1117 data
1118 };
1119 return resultEntry;
1120 }
1121
1122 /**
1123 * @returns {Source | null} the original source for the module before webpack transformation
1124 */
1125 originalSource() {
1126 return this._source;
1127 }
1128
1129 /**
1130 * @returns {void}
1131 */
1132 invalidateBuild() {
1133 this._forceBuild = true;
1134 }
1135
1136 /**
1137 * @param {NeedBuildContext} context context info
1138 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
1139 * @returns {void}
1140 */
1141 needBuild({ fileSystemInfo, valueCacheVersions }, callback) {
1142 // build if enforced
1143 if (this._forceBuild) return callback(null, true);
1144
1145 // always try to build in case of an error
1146 if (this.error) return callback(null, true);
1147
1148 // always build when module is not cacheable
1149 if (!this.buildInfo.cacheable) return callback(null, true);
1150
1151 // build when there is no snapshot to check
1152 if (!this.buildInfo.snapshot) return callback(null, true);
1153
1154 // build when valueDependencies have changed
1155 /** @type {Map<string, string | Set<string>>} */
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 // check snapshot for validity
1175 fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
1176 callback(err, !valid);
1177 });
1178 }
1179
1180 /**
1181 * @param {string=} type the source type for which the size should be estimated
1182 * @returns {number} the estimated size of the module (must be non-zero)
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 * @param {LazySet<string>} fileDependencies set where file dependencies are added to
1200 * @param {LazySet<string>} contextDependencies set where context dependencies are added to
1201 * @param {LazySet<string>} missingDependencies set where missing dependencies are added to
1202 * @param {LazySet<string>} buildDependencies set where build dependencies are added to
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 * @param {Hash} hash the hash used to track dependencies
1232 * @param {UpdateHashContext} context context
1233 * @returns {void}
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 // deserialize
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 // will be deserialized by Module
1258 layer: null,
1259 type: "",
1260 // will be filled by updateCacheModule
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
1288makeSerializable(NormalModule, "webpack/lib/NormalModule");
1289
1290module.exports = NormalModule;