NormalModule.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const parseJson = require("json-parse-even-better-errors");
  7. const { getContext, runLoaders } = require("loader-runner");
  8. const querystring = require("querystring");
  9. const { HookMap, SyncHook, AsyncSeriesBailHook } = require("tapable");
  10. const {
  11. CachedSource,
  12. OriginalSource,
  13. RawSource,
  14. SourceMapSource
  15. } = require("webpack-sources");
  16. const Compilation = require("./Compilation");
  17. const HookWebpackError = require("./HookWebpackError");
  18. const Module = require("./Module");
  19. const ModuleBuildError = require("./ModuleBuildError");
  20. const ModuleError = require("./ModuleError");
  21. const ModuleGraphConnection = require("./ModuleGraphConnection");
  22. const ModuleParseError = require("./ModuleParseError");
  23. const { JAVASCRIPT_MODULE_TYPE_AUTO } = require("./ModuleTypeConstants");
  24. const ModuleWarning = require("./ModuleWarning");
  25. const RuntimeGlobals = require("./RuntimeGlobals");
  26. const UnhandledSchemeError = require("./UnhandledSchemeError");
  27. const WebpackError = require("./WebpackError");
  28. const formatLocation = require("./formatLocation");
  29. const LazySet = require("./util/LazySet");
  30. const { isSubset } = require("./util/SetHelpers");
  31. const { getScheme } = require("./util/URLAbsoluteSpecifier");
  32. const {
  33. compareLocations,
  34. concatComparators,
  35. compareSelect,
  36. keepOriginalOrder
  37. } = require("./util/comparators");
  38. const createHash = require("./util/createHash");
  39. const { createFakeHook } = require("./util/deprecation");
  40. const { join } = require("./util/fs");
  41. const {
  42. contextify,
  43. absolutify,
  44. makePathsRelative
  45. } = require("./util/identifier");
  46. const makeSerializable = require("./util/makeSerializable");
  47. const memoize = require("./util/memoize");
  48. /** @typedef {import("webpack-sources").Source} Source */
  49. /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */
  50. /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
  51. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  52. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  53. /** @typedef {import("./Compiler")} Compiler */
  54. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  55. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  56. /** @typedef {import("./Generator")} Generator */
  57. /** @typedef {import("./Module").BuildInfo} BuildInfo */
  58. /** @typedef {import("./Module").BuildMeta} BuildMeta */
  59. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  60. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  61. /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  62. /** @typedef {import("./Module").KnownBuildInfo} KnownBuildInfo */
  63. /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
  64. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  65. /** @typedef {import("./Generator").SourceTypes} SourceTypes */
  66. /** @typedef {import("./Module").UnsafeCacheData} UnsafeCacheData */
  67. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  68. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  69. /** @typedef {import("./ModuleTypeConstants").JavaScriptModuleTypes} JavaScriptModuleTypes */
  70. /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
  71. /** @typedef {import("./NormalModuleFactory").ResourceDataWithData} ResourceDataWithData */
  72. /** @typedef {import("./Parser")} Parser */
  73. /** @typedef {import("./RequestShortener")} RequestShortener */
  74. /** @typedef {import("./ResolverFactory").ResolveContext} ResolveContext */
  75. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  76. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  77. /** @typedef {import("./logging/Logger").Logger} WebpackLogger */
  78. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  79. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  80. /** @typedef {import("./util/Hash")} Hash */
  81. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  82. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  83. /** @typedef {import("./util/createHash").Algorithm} Algorithm */
  84. /**
  85. * @template T
  86. * @typedef {import("./util/deprecation").FakeHook<T>} FakeHook
  87. */
  88. /** @typedef {{[k: string]: any}} ParserOptions */
  89. /** @typedef {{[k: string]: any}} GeneratorOptions */
  90. /** @typedef {UnsafeCacheData & { parser: undefined | Parser, parserOptions: undefined | ParserOptions, generator: undefined | Generator, generatorOptions: undefined | GeneratorOptions }} NormalModuleUnsafeCacheData */
  91. /**
  92. * @template T
  93. * @typedef {import("../declarations/LoaderContext").LoaderContext<T>} LoaderContext
  94. */
  95. /**
  96. * @template T
  97. * @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} NormalModuleLoaderContext
  98. */
  99. /**
  100. * @typedef {object} SourceMap
  101. * @property {number} version
  102. * @property {string[]} sources
  103. * @property {string} mappings
  104. * @property {string=} file
  105. * @property {string=} sourceRoot
  106. * @property {string[]=} sourcesContent
  107. * @property {string[]=} names
  108. * @property {string=} debugId
  109. */
  110. const getInvalidDependenciesModuleWarning = memoize(() =>
  111. require("./InvalidDependenciesModuleWarning")
  112. );
  113. const getValidate = memoize(() => require("schema-utils").validate);
  114. const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/;
  115. /**
  116. * @typedef {object} LoaderItem
  117. * @property {string} loader
  118. * @property {any} options
  119. * @property {string?} ident
  120. * @property {string?} type
  121. */
  122. /**
  123. * @param {string} context absolute context path
  124. * @param {string} source a source path
  125. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  126. * @returns {string} new source path
  127. */
  128. const contextifySourceUrl = (context, source, associatedObjectForCache) => {
  129. if (source.startsWith("webpack://")) return source;
  130. return `webpack://${makePathsRelative(
  131. context,
  132. source,
  133. associatedObjectForCache
  134. )}`;
  135. };
  136. /**
  137. * @param {string} context absolute context path
  138. * @param {SourceMap} sourceMap a source map
  139. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  140. * @returns {SourceMap} new source map
  141. */
  142. const contextifySourceMap = (context, sourceMap, associatedObjectForCache) => {
  143. if (!Array.isArray(sourceMap.sources)) return sourceMap;
  144. const { sourceRoot } = sourceMap;
  145. /** @type {function(string): string} */
  146. const mapper = !sourceRoot
  147. ? source => source
  148. : sourceRoot.endsWith("/")
  149. ? source =>
  150. source.startsWith("/")
  151. ? `${sourceRoot.slice(0, -1)}${source}`
  152. : `${sourceRoot}${source}`
  153. : source =>
  154. source.startsWith("/")
  155. ? `${sourceRoot}${source}`
  156. : `${sourceRoot}/${source}`;
  157. const newSources = sourceMap.sources.map(source =>
  158. contextifySourceUrl(context, mapper(source), associatedObjectForCache)
  159. );
  160. return {
  161. ...sourceMap,
  162. file: "x",
  163. sourceRoot: undefined,
  164. sources: newSources
  165. };
  166. };
  167. /**
  168. * @param {string | Buffer} input the input
  169. * @returns {string} the converted string
  170. */
  171. const asString = input => {
  172. if (Buffer.isBuffer(input)) {
  173. return input.toString("utf-8");
  174. }
  175. return input;
  176. };
  177. /**
  178. * @param {string | Buffer} input the input
  179. * @returns {Buffer} the converted buffer
  180. */
  181. const asBuffer = input => {
  182. if (!Buffer.isBuffer(input)) {
  183. return Buffer.from(input, "utf-8");
  184. }
  185. return input;
  186. };
  187. class NonErrorEmittedError extends WebpackError {
  188. /**
  189. * @param {any} error value which is not an instance of Error
  190. */
  191. constructor(error) {
  192. super();
  193. this.name = "NonErrorEmittedError";
  194. this.message = `(Emitted value instead of an instance of Error) ${error}`;
  195. }
  196. }
  197. makeSerializable(
  198. NonErrorEmittedError,
  199. "webpack/lib/NormalModule",
  200. "NonErrorEmittedError"
  201. );
  202. /**
  203. * @typedef {object} NormalModuleCompilationHooks
  204. * @property {SyncHook<[LoaderContext<any>, NormalModule]>} loader
  205. * @property {SyncHook<[LoaderItem[], NormalModule, LoaderContext<any>]>} beforeLoaders
  206. * @property {SyncHook<[NormalModule]>} beforeParse
  207. * @property {SyncHook<[NormalModule]>} beforeSnapshot
  208. * @property {HookMap<FakeHook<AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>>>} readResourceForScheme
  209. * @property {HookMap<AsyncSeriesBailHook<[LoaderContext<any>], string | Buffer | null>>} readResource
  210. * @property {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>} needBuild
  211. */
  212. /**
  213. * @typedef {object} NormalModuleCreateData
  214. * @property {string=} layer an optional layer in which the module is
  215. * @property {JavaScriptModuleTypes | ""} type module type. When deserializing, this is set to an empty string "".
  216. * @property {string} request request string
  217. * @property {string} userRequest request intended by user (without loaders from config)
  218. * @property {string} rawRequest request without resolving
  219. * @property {LoaderItem[]} loaders list of loaders
  220. * @property {string} resource path + query of the real resource
  221. * @property {Record<string, any>=} resourceResolveData resource resolve data
  222. * @property {string} context context directory for resolving
  223. * @property {string=} matchResource path + query of the matched resource (virtual)
  224. * @property {Parser} parser the parser used
  225. * @property {ParserOptions=} parserOptions the options of the parser used
  226. * @property {Generator} generator the generator used
  227. * @property {GeneratorOptions=} generatorOptions the options of the generator used
  228. * @property {ResolveOptions=} resolveOptions options used for resolving requests from this module
  229. */
  230. /** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
  231. const compilationHooksMap = new WeakMap();
  232. class NormalModule extends Module {
  233. /**
  234. * @param {Compilation} compilation the compilation
  235. * @returns {NormalModuleCompilationHooks} the attached hooks
  236. */
  237. static getCompilationHooks(compilation) {
  238. if (!(compilation instanceof Compilation)) {
  239. throw new TypeError(
  240. "The 'compilation' argument must be an instance of Compilation"
  241. );
  242. }
  243. let hooks = compilationHooksMap.get(compilation);
  244. if (hooks === undefined) {
  245. hooks = {
  246. loader: new SyncHook(["loaderContext", "module"]),
  247. beforeLoaders: new SyncHook(["loaders", "module", "loaderContext"]),
  248. beforeParse: new SyncHook(["module"]),
  249. beforeSnapshot: new SyncHook(["module"]),
  250. // TODO webpack 6 deprecate
  251. readResourceForScheme: new HookMap(scheme => {
  252. const hook =
  253. /** @type {NormalModuleCompilationHooks} */
  254. (hooks).readResource.for(scheme);
  255. return createFakeHook(
  256. /** @type {AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>} */ ({
  257. tap: (options, fn) =>
  258. hook.tap(options, loaderContext =>
  259. fn(
  260. loaderContext.resource,
  261. /** @type {NormalModule} */ (loaderContext._module)
  262. )
  263. ),
  264. tapAsync: (options, fn) =>
  265. hook.tapAsync(options, (loaderContext, callback) =>
  266. fn(
  267. loaderContext.resource,
  268. /** @type {NormalModule} */ (loaderContext._module),
  269. callback
  270. )
  271. ),
  272. tapPromise: (options, fn) =>
  273. hook.tapPromise(options, loaderContext =>
  274. fn(
  275. loaderContext.resource,
  276. /** @type {NormalModule} */ (loaderContext._module)
  277. )
  278. )
  279. })
  280. );
  281. }),
  282. readResource: new HookMap(
  283. () => new AsyncSeriesBailHook(["loaderContext"])
  284. ),
  285. needBuild: new AsyncSeriesBailHook(["module", "context"])
  286. };
  287. compilationHooksMap.set(
  288. compilation,
  289. /** @type {NormalModuleCompilationHooks} */ (hooks)
  290. );
  291. }
  292. return /** @type {NormalModuleCompilationHooks} */ (hooks);
  293. }
  294. /**
  295. * @param {NormalModuleCreateData} options options object
  296. */
  297. constructor({
  298. layer,
  299. type,
  300. request,
  301. userRequest,
  302. rawRequest,
  303. loaders,
  304. resource,
  305. resourceResolveData,
  306. context,
  307. matchResource,
  308. parser,
  309. parserOptions,
  310. generator,
  311. generatorOptions,
  312. resolveOptions
  313. }) {
  314. super(type, context || getContext(resource), layer);
  315. // Info from Factory
  316. /** @type {string} */
  317. this.request = request;
  318. /** @type {string} */
  319. this.userRequest = userRequest;
  320. /** @type {string} */
  321. this.rawRequest = rawRequest;
  322. /** @type {boolean} */
  323. this.binary = /^(asset|webassembly)\b/.test(type);
  324. /** @type {undefined | Parser} */
  325. this.parser = parser;
  326. /** @type {undefined | ParserOptions} */
  327. this.parserOptions = parserOptions;
  328. /** @type {undefined | Generator} */
  329. this.generator = generator;
  330. /** @type {undefined | GeneratorOptions} */
  331. this.generatorOptions = generatorOptions;
  332. /** @type {string} */
  333. this.resource = resource;
  334. this.resourceResolveData = resourceResolveData;
  335. /** @type {string | undefined} */
  336. this.matchResource = matchResource;
  337. /** @type {LoaderItem[]} */
  338. this.loaders = loaders;
  339. if (resolveOptions !== undefined) {
  340. // already declared in super class
  341. this.resolveOptions = resolveOptions;
  342. }
  343. // Info from Build
  344. /** @type {WebpackError | null} */
  345. this.error = null;
  346. /**
  347. * @private
  348. * @type {Source | null}
  349. */
  350. this._source = null;
  351. /**
  352. * @private
  353. * @type {Map<string | undefined, number> | undefined}
  354. */
  355. this._sourceSizes = undefined;
  356. /**
  357. * @private
  358. * @type {undefined | SourceTypes}
  359. */
  360. this._sourceTypes = undefined;
  361. // Cache
  362. this._lastSuccessfulBuildMeta = {};
  363. this._forceBuild = true;
  364. this._isEvaluatingSideEffects = false;
  365. /** @type {WeakSet<ModuleGraph> | undefined} */
  366. this._addedSideEffectsBailout = undefined;
  367. /** @type {Map<string, any>} */
  368. this._codeGeneratorData = new Map();
  369. }
  370. /**
  371. * @returns {string} a unique identifier of the module
  372. */
  373. identifier() {
  374. if (this.layer === null) {
  375. if (this.type === JAVASCRIPT_MODULE_TYPE_AUTO) {
  376. return this.request;
  377. }
  378. return `${this.type}|${this.request}`;
  379. }
  380. return `${this.type}|${this.request}|${this.layer}`;
  381. }
  382. /**
  383. * @param {RequestShortener} requestShortener the request shortener
  384. * @returns {string} a user readable identifier of the module
  385. */
  386. readableIdentifier(requestShortener) {
  387. return /** @type {string} */ (requestShortener.shorten(this.userRequest));
  388. }
  389. /**
  390. * @param {LibIdentOptions} options options
  391. * @returns {string | null} an identifier for library inclusion
  392. */
  393. libIdent(options) {
  394. let ident = contextify(
  395. options.context,
  396. this.userRequest,
  397. options.associatedObjectForCache
  398. );
  399. if (this.layer) ident = `(${this.layer})/${ident}`;
  400. return ident;
  401. }
  402. /**
  403. * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
  404. */
  405. nameForCondition() {
  406. const resource = this.matchResource || this.resource;
  407. const idx = resource.indexOf("?");
  408. if (idx >= 0) return resource.slice(0, idx);
  409. return resource;
  410. }
  411. /**
  412. * Assuming this module is in the cache. Update the (cached) module with
  413. * the fresh module from the factory. Usually updates internal references
  414. * and properties.
  415. * @param {Module} module fresh module
  416. * @returns {void}
  417. */
  418. updateCacheModule(module) {
  419. super.updateCacheModule(module);
  420. const m = /** @type {NormalModule} */ (module);
  421. this.binary = m.binary;
  422. this.request = m.request;
  423. this.userRequest = m.userRequest;
  424. this.rawRequest = m.rawRequest;
  425. this.parser = m.parser;
  426. this.parserOptions = m.parserOptions;
  427. this.generator = m.generator;
  428. this.generatorOptions = m.generatorOptions;
  429. this.resource = m.resource;
  430. this.resourceResolveData = m.resourceResolveData;
  431. this.context = m.context;
  432. this.matchResource = m.matchResource;
  433. this.loaders = m.loaders;
  434. }
  435. /**
  436. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  437. */
  438. cleanupForCache() {
  439. // Make sure to cache types and sizes before cleanup when this module has been built
  440. // They are accessed by the stats and we don't want them to crash after cleanup
  441. // TODO reconsider this for webpack 6
  442. if (this.buildInfo) {
  443. if (this._sourceTypes === undefined) this.getSourceTypes();
  444. for (const type of /** @type {SourceTypes} */ (this._sourceTypes)) {
  445. this.size(type);
  446. }
  447. }
  448. super.cleanupForCache();
  449. this.parser = undefined;
  450. this.parserOptions = undefined;
  451. this.generator = undefined;
  452. this.generatorOptions = undefined;
  453. }
  454. /**
  455. * Module should be unsafe cached. Get data that's needed for that.
  456. * This data will be passed to restoreFromUnsafeCache later.
  457. * @returns {UnsafeCacheData} cached data
  458. */
  459. getUnsafeCacheData() {
  460. const data =
  461. /** @type {NormalModuleUnsafeCacheData} */
  462. (super.getUnsafeCacheData());
  463. data.parserOptions = this.parserOptions;
  464. data.generatorOptions = this.generatorOptions;
  465. return data;
  466. }
  467. /**
  468. * restore unsafe cache data
  469. * @param {NormalModuleUnsafeCacheData} unsafeCacheData data from getUnsafeCacheData
  470. * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
  471. */
  472. restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
  473. this._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
  474. }
  475. /**
  476. * restore unsafe cache data
  477. * @param {object} unsafeCacheData data from getUnsafeCacheData
  478. * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
  479. */
  480. _restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
  481. super._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
  482. this.parserOptions = unsafeCacheData.parserOptions;
  483. this.parser = normalModuleFactory.getParser(this.type, this.parserOptions);
  484. this.generatorOptions = unsafeCacheData.generatorOptions;
  485. this.generator = normalModuleFactory.getGenerator(
  486. this.type,
  487. this.generatorOptions
  488. );
  489. // we assume the generator behaves identically and keep cached sourceTypes/Sizes
  490. }
  491. /**
  492. * @param {string} context the compilation context
  493. * @param {string} name the asset name
  494. * @param {string | Buffer} content the content
  495. * @param {(string | SourceMap)=} sourceMap an optional source map
  496. * @param {object=} associatedObjectForCache object for caching
  497. * @returns {Source} the created source
  498. */
  499. createSourceForAsset(
  500. context,
  501. name,
  502. content,
  503. sourceMap,
  504. associatedObjectForCache
  505. ) {
  506. if (sourceMap) {
  507. if (
  508. typeof sourceMap === "string" &&
  509. (this.useSourceMap || this.useSimpleSourceMap)
  510. ) {
  511. return new OriginalSource(
  512. content,
  513. contextifySourceUrl(context, sourceMap, associatedObjectForCache)
  514. );
  515. }
  516. if (this.useSourceMap) {
  517. return new SourceMapSource(
  518. content,
  519. name,
  520. contextifySourceMap(
  521. context,
  522. /** @type {SourceMap} */ (sourceMap),
  523. associatedObjectForCache
  524. )
  525. );
  526. }
  527. }
  528. return new RawSource(content);
  529. }
  530. /**
  531. * @private
  532. * @template T
  533. * @param {ResolverWithOptions} resolver a resolver
  534. * @param {WebpackOptions} options webpack options
  535. * @param {Compilation} compilation the compilation
  536. * @param {InputFileSystem} fs file system from reading
  537. * @param {NormalModuleCompilationHooks} hooks the hooks
  538. * @returns {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} loader context
  539. */
  540. _createLoaderContext(resolver, options, compilation, fs, hooks) {
  541. const { requestShortener } = compilation.runtimeTemplate;
  542. const getCurrentLoaderName = () => {
  543. const currentLoader = this.getCurrentLoader(loaderContext);
  544. if (!currentLoader) return "(not in loader scope)";
  545. return requestShortener.shorten(currentLoader.loader);
  546. };
  547. /**
  548. * @returns {ResolveContext} resolve context
  549. */
  550. const getResolveContext = () => ({
  551. fileDependencies: {
  552. add: d => /** @type {TODO} */ (loaderContext).addDependency(d)
  553. },
  554. contextDependencies: {
  555. add: d => /** @type {TODO} */ (loaderContext).addContextDependency(d)
  556. },
  557. missingDependencies: {
  558. add: d => /** @type {TODO} */ (loaderContext).addMissingDependency(d)
  559. }
  560. });
  561. const getAbsolutify = memoize(() =>
  562. absolutify.bindCache(compilation.compiler.root)
  563. );
  564. const getAbsolutifyInContext = memoize(() =>
  565. absolutify.bindContextCache(
  566. /** @type {string} */
  567. (this.context),
  568. compilation.compiler.root
  569. )
  570. );
  571. const getContextify = memoize(() =>
  572. contextify.bindCache(compilation.compiler.root)
  573. );
  574. const getContextifyInContext = memoize(() =>
  575. contextify.bindContextCache(
  576. /** @type {string} */
  577. (this.context),
  578. compilation.compiler.root
  579. )
  580. );
  581. const utils = {
  582. /**
  583. * @param {string} context context
  584. * @param {string} request request
  585. * @returns {string} result
  586. */
  587. absolutify: (context, request) =>
  588. context === this.context
  589. ? getAbsolutifyInContext()(request)
  590. : getAbsolutify()(context, request),
  591. /**
  592. * @param {string} context context
  593. * @param {string} request request
  594. * @returns {string} result
  595. */
  596. contextify: (context, request) =>
  597. context === this.context
  598. ? getContextifyInContext()(request)
  599. : getContextify()(context, request),
  600. /**
  601. * @param {(string | typeof import("./util/Hash"))=} type type
  602. * @returns {Hash} hash
  603. */
  604. createHash: type =>
  605. createHash(
  606. type ||
  607. /** @type {Algorithm} */
  608. (compilation.outputOptions.hashFunction)
  609. )
  610. };
  611. /** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
  612. const loaderContext = {
  613. version: 2,
  614. getOptions: schema => {
  615. const loader = this.getCurrentLoader(loaderContext);
  616. let { options } = /** @type {LoaderItem} */ (loader);
  617. if (typeof options === "string") {
  618. if (options.startsWith("{") && options.endsWith("}")) {
  619. try {
  620. options = parseJson(options);
  621. } catch (err) {
  622. throw new Error(
  623. `Cannot parse string options: ${/** @type {Error} */ (err).message}`
  624. );
  625. }
  626. } else {
  627. options = querystring.parse(options, "&", "=", {
  628. maxKeys: 0
  629. });
  630. }
  631. }
  632. if (options === null || options === undefined) {
  633. options = {};
  634. }
  635. if (schema) {
  636. let name = "Loader";
  637. let baseDataPath = "options";
  638. let match;
  639. if (schema.title && (match = /^(.+) (.+)$/.exec(schema.title))) {
  640. [, name, baseDataPath] = match;
  641. }
  642. getValidate()(schema, options, {
  643. name,
  644. baseDataPath
  645. });
  646. }
  647. return options;
  648. },
  649. emitWarning: warning => {
  650. if (!(warning instanceof Error)) {
  651. warning = new NonErrorEmittedError(warning);
  652. }
  653. this.addWarning(
  654. new ModuleWarning(warning, {
  655. from: getCurrentLoaderName()
  656. })
  657. );
  658. },
  659. emitError: error => {
  660. if (!(error instanceof Error)) {
  661. error = new NonErrorEmittedError(error);
  662. }
  663. this.addError(
  664. new ModuleError(error, {
  665. from: getCurrentLoaderName()
  666. })
  667. );
  668. },
  669. getLogger: name => {
  670. const currentLoader = this.getCurrentLoader(loaderContext);
  671. return compilation.getLogger(() =>
  672. [currentLoader && currentLoader.loader, name, this.identifier()]
  673. .filter(Boolean)
  674. .join("|")
  675. );
  676. },
  677. resolve(context, request, callback) {
  678. resolver.resolve({}, context, request, getResolveContext(), callback);
  679. },
  680. getResolve(options) {
  681. const child = options ? resolver.withOptions(options) : resolver;
  682. return (context, request, callback) => {
  683. if (callback) {
  684. child.resolve({}, context, request, getResolveContext(), callback);
  685. } else {
  686. return new Promise((resolve, reject) => {
  687. child.resolve(
  688. {},
  689. context,
  690. request,
  691. getResolveContext(),
  692. (err, result) => {
  693. if (err) reject(err);
  694. else resolve(result);
  695. }
  696. );
  697. });
  698. }
  699. };
  700. },
  701. emitFile: (name, content, sourceMap, assetInfo) => {
  702. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  703. if (!buildInfo.assets) {
  704. buildInfo.assets = Object.create(null);
  705. buildInfo.assetsInfo = new Map();
  706. }
  707. const assets =
  708. /** @type {NonNullable<KnownBuildInfo["assets"]>} */
  709. (buildInfo.assets);
  710. const assetsInfo =
  711. /** @type {NonNullable<KnownBuildInfo["assetsInfo"]>} */
  712. (buildInfo.assetsInfo);
  713. assets[name] = this.createSourceForAsset(
  714. /** @type {string} */ (options.context),
  715. name,
  716. content,
  717. sourceMap,
  718. compilation.compiler.root
  719. );
  720. assetsInfo.set(name, assetInfo);
  721. },
  722. addBuildDependency: dep => {
  723. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  724. if (buildInfo.buildDependencies === undefined) {
  725. buildInfo.buildDependencies = new LazySet();
  726. }
  727. buildInfo.buildDependencies.add(dep);
  728. },
  729. utils,
  730. rootContext: /** @type {string} */ (options.context),
  731. webpack: true,
  732. sourceMap: Boolean(this.useSourceMap),
  733. mode: options.mode || "production",
  734. hashFunction: /** @type {TODO} */ (options.output.hashFunction),
  735. hashDigest: /** @type {string} */ (options.output.hashDigest),
  736. hashDigestLength: /** @type {number} */ (options.output.hashDigestLength),
  737. hashSalt: /** @type {string} */ (options.output.hashSalt),
  738. _module: this,
  739. _compilation: compilation,
  740. _compiler: compilation.compiler,
  741. fs
  742. };
  743. Object.assign(loaderContext, options.loader);
  744. hooks.loader.call(/** @type {LoaderContext<any>} */ (loaderContext), this);
  745. return loaderContext;
  746. }
  747. // TODO remove `loaderContext` in webpack@6
  748. /**
  749. * @param {TODO} loaderContext loader context
  750. * @param {number} index index
  751. * @returns {LoaderItem | null} loader
  752. */
  753. getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
  754. if (
  755. this.loaders &&
  756. this.loaders.length &&
  757. index < this.loaders.length &&
  758. index >= 0 &&
  759. this.loaders[index]
  760. ) {
  761. return this.loaders[index];
  762. }
  763. return null;
  764. }
  765. /**
  766. * @param {string} context the compilation context
  767. * @param {string | Buffer} content the content
  768. * @param {(string | SourceMapSource | null)=} sourceMap an optional source map
  769. * @param {object=} associatedObjectForCache object for caching
  770. * @returns {Source} the created source
  771. */
  772. createSource(context, content, sourceMap, associatedObjectForCache) {
  773. if (Buffer.isBuffer(content)) {
  774. return new RawSource(content);
  775. }
  776. // if there is no identifier return raw source
  777. if (!this.identifier) {
  778. return new RawSource(content);
  779. }
  780. // from here on we assume we have an identifier
  781. const identifier = this.identifier();
  782. if (this.useSourceMap && sourceMap) {
  783. return new SourceMapSource(
  784. content,
  785. contextifySourceUrl(context, identifier, associatedObjectForCache),
  786. contextifySourceMap(
  787. context,
  788. /** @type {TODO} */ (sourceMap),
  789. associatedObjectForCache
  790. )
  791. );
  792. }
  793. if (this.useSourceMap || this.useSimpleSourceMap) {
  794. return new OriginalSource(
  795. content,
  796. contextifySourceUrl(context, identifier, associatedObjectForCache)
  797. );
  798. }
  799. return new RawSource(content);
  800. }
  801. /**
  802. * @param {WebpackOptions} options webpack options
  803. * @param {Compilation} compilation the compilation
  804. * @param {ResolverWithOptions} resolver the resolver
  805. * @param {InputFileSystem} fs the file system
  806. * @param {NormalModuleCompilationHooks} hooks the hooks
  807. * @param {function((WebpackError | null)=): void} callback callback function
  808. * @returns {void}
  809. */
  810. _doBuild(options, compilation, resolver, fs, hooks, callback) {
  811. const loaderContext = this._createLoaderContext(
  812. resolver,
  813. options,
  814. compilation,
  815. fs,
  816. hooks
  817. );
  818. /** @typedef {[string | Buffer, string | SourceMapSource, Record<string, any>]} Result */
  819. /**
  820. * @param {Error | null} err err
  821. * @param {(Result | null)=} _result result
  822. * @returns {void}
  823. */
  824. const processResult = (err, _result) => {
  825. if (err) {
  826. if (!(err instanceof Error)) {
  827. err = new NonErrorEmittedError(err);
  828. }
  829. const currentLoader = this.getCurrentLoader(loaderContext);
  830. const error = new ModuleBuildError(err, {
  831. from:
  832. currentLoader &&
  833. compilation.runtimeTemplate.requestShortener.shorten(
  834. currentLoader.loader
  835. )
  836. });
  837. return callback(error);
  838. }
  839. const result = /** @type {Result} */ (_result);
  840. const source = result[0];
  841. const sourceMap = result.length >= 1 ? result[1] : null;
  842. const extraInfo = result.length >= 2 ? result[2] : null;
  843. if (!Buffer.isBuffer(source) && typeof source !== "string") {
  844. const currentLoader = this.getCurrentLoader(loaderContext, 0);
  845. const err = new Error(
  846. `Final loader (${
  847. currentLoader
  848. ? compilation.runtimeTemplate.requestShortener.shorten(
  849. currentLoader.loader
  850. )
  851. : "unknown"
  852. }) didn't return a Buffer or String`
  853. );
  854. const error = new ModuleBuildError(err);
  855. return callback(error);
  856. }
  857. const isBinaryModule =
  858. this.generatorOptions && this.generatorOptions.binary !== undefined
  859. ? this.generatorOptions.binary
  860. : this.binary;
  861. this._source = this.createSource(
  862. /** @type {string} */ (options.context),
  863. isBinaryModule ? asBuffer(source) : asString(source),
  864. sourceMap,
  865. compilation.compiler.root
  866. );
  867. if (this._sourceSizes !== undefined) this._sourceSizes.clear();
  868. this._ast =
  869. typeof extraInfo === "object" &&
  870. extraInfo !== null &&
  871. extraInfo.webpackAST !== undefined
  872. ? extraInfo.webpackAST
  873. : null;
  874. return callback();
  875. };
  876. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  877. buildInfo.fileDependencies = new LazySet();
  878. buildInfo.contextDependencies = new LazySet();
  879. buildInfo.missingDependencies = new LazySet();
  880. buildInfo.cacheable = true;
  881. try {
  882. hooks.beforeLoaders.call(
  883. this.loaders,
  884. this,
  885. /** @type {LoaderContext<any>} */ (loaderContext)
  886. );
  887. } catch (err) {
  888. processResult(/** @type {Error} */ (err));
  889. return;
  890. }
  891. if (this.loaders.length > 0) {
  892. /** @type {BuildInfo} */
  893. (this.buildInfo).buildDependencies = new LazySet();
  894. }
  895. runLoaders(
  896. {
  897. resource: this.resource,
  898. loaders: this.loaders,
  899. context: loaderContext,
  900. /**
  901. * @param {LoaderContext<TODO>} loaderContext the loader context
  902. * @param {string} resourcePath the resource Path
  903. * @param {(err: Error | null, result?: string | Buffer) => void} callback callback
  904. */
  905. processResource: (loaderContext, resourcePath, callback) => {
  906. const resource = loaderContext.resource;
  907. const scheme = getScheme(resource);
  908. hooks.readResource
  909. .for(scheme)
  910. .callAsync(loaderContext, (err, result) => {
  911. if (err) return callback(err);
  912. if (typeof result !== "string" && !result) {
  913. return callback(
  914. new UnhandledSchemeError(
  915. /** @type {string} */
  916. (scheme),
  917. resource
  918. )
  919. );
  920. }
  921. return callback(null, result);
  922. });
  923. }
  924. },
  925. (err, result) => {
  926. // Cleanup loaderContext to avoid leaking memory in ICs
  927. loaderContext._compilation =
  928. loaderContext._compiler =
  929. loaderContext._module =
  930. // eslint-disable-next-line no-warning-comments
  931. // @ts-ignore
  932. loaderContext.fs =
  933. undefined;
  934. if (!result) {
  935. /** @type {BuildInfo} */
  936. (this.buildInfo).cacheable = false;
  937. return processResult(
  938. err || new Error("No result from loader-runner processing"),
  939. null
  940. );
  941. }
  942. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  943. const fileDependencies =
  944. /** @type {NonNullable<KnownBuildInfo["fileDependencies"]>} */
  945. (buildInfo.fileDependencies);
  946. const contextDependencies =
  947. /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
  948. (buildInfo.contextDependencies);
  949. const missingDependencies =
  950. /** @type {NonNullable<KnownBuildInfo["missingDependencies"]>} */
  951. (buildInfo.missingDependencies);
  952. fileDependencies.addAll(result.fileDependencies);
  953. contextDependencies.addAll(result.contextDependencies);
  954. missingDependencies.addAll(result.missingDependencies);
  955. for (const loader of this.loaders) {
  956. const buildDependencies =
  957. /** @type {NonNullable<KnownBuildInfo["buildDependencies"]>} */
  958. (buildInfo.buildDependencies);
  959. buildDependencies.add(loader.loader);
  960. }
  961. buildInfo.cacheable = buildInfo.cacheable && result.cacheable;
  962. processResult(err, result.result);
  963. }
  964. );
  965. }
  966. /**
  967. * @param {WebpackError} error the error
  968. * @returns {void}
  969. */
  970. markModuleAsErrored(error) {
  971. // Restore build meta from successful build to keep importing state
  972. this.buildMeta = { ...this._lastSuccessfulBuildMeta };
  973. this.error = error;
  974. this.addError(error);
  975. }
  976. /**
  977. * @param {TODO} rule rule
  978. * @param {string} content content
  979. * @returns {boolean} result
  980. */
  981. applyNoParseRule(rule, content) {
  982. // must start with "rule" if rule is a string
  983. if (typeof rule === "string") {
  984. return content.startsWith(rule);
  985. }
  986. if (typeof rule === "function") {
  987. return rule(content);
  988. }
  989. // we assume rule is a regexp
  990. return rule.test(content);
  991. }
  992. /**
  993. * @param {TODO} noParseRule no parse rule
  994. * @param {string} request request
  995. * @returns {boolean} check if module should not be parsed, returns "true" if the module should !not! be parsed, returns "false" if the module !must! be parsed
  996. */
  997. shouldPreventParsing(noParseRule, request) {
  998. // if no noParseRule exists, return false
  999. // the module !must! be parsed.
  1000. if (!noParseRule) {
  1001. return false;
  1002. }
  1003. // we only have one rule to check
  1004. if (!Array.isArray(noParseRule)) {
  1005. // returns "true" if the module is !not! to be parsed
  1006. return this.applyNoParseRule(noParseRule, request);
  1007. }
  1008. for (let i = 0; i < noParseRule.length; i++) {
  1009. const rule = noParseRule[i];
  1010. // early exit on first truthy match
  1011. // this module is !not! to be parsed
  1012. if (this.applyNoParseRule(rule, request)) {
  1013. return true;
  1014. }
  1015. }
  1016. // no match found, so this module !should! be parsed
  1017. return false;
  1018. }
  1019. /**
  1020. * @param {Compilation} compilation compilation
  1021. * @private
  1022. */
  1023. _initBuildHash(compilation) {
  1024. const hash = createHash(
  1025. /** @type {Algorithm} */
  1026. (compilation.outputOptions.hashFunction)
  1027. );
  1028. if (this._source) {
  1029. hash.update("source");
  1030. this._source.updateHash(hash);
  1031. }
  1032. hash.update("meta");
  1033. hash.update(JSON.stringify(this.buildMeta));
  1034. /** @type {BuildInfo} */
  1035. (this.buildInfo).hash = /** @type {string} */ (hash.digest("hex"));
  1036. }
  1037. /**
  1038. * @param {WebpackOptions} options webpack options
  1039. * @param {Compilation} compilation the compilation
  1040. * @param {ResolverWithOptions} resolver the resolver
  1041. * @param {InputFileSystem} fs the file system
  1042. * @param {function(WebpackError=): void} callback callback function
  1043. * @returns {void}
  1044. */
  1045. build(options, compilation, resolver, fs, callback) {
  1046. this._forceBuild = false;
  1047. this._source = null;
  1048. if (this._sourceSizes !== undefined) this._sourceSizes.clear();
  1049. this._sourceTypes = undefined;
  1050. this._ast = null;
  1051. this.error = null;
  1052. this.clearWarningsAndErrors();
  1053. this.clearDependenciesAndBlocks();
  1054. this.buildMeta = {};
  1055. this.buildInfo = {
  1056. cacheable: false,
  1057. parsed: true,
  1058. fileDependencies: undefined,
  1059. contextDependencies: undefined,
  1060. missingDependencies: undefined,
  1061. buildDependencies: undefined,
  1062. valueDependencies: undefined,
  1063. hash: undefined,
  1064. assets: undefined,
  1065. assetsInfo: undefined
  1066. };
  1067. const startTime = compilation.compiler.fsStartTime || Date.now();
  1068. const hooks = NormalModule.getCompilationHooks(compilation);
  1069. return this._doBuild(options, compilation, resolver, fs, hooks, err => {
  1070. // if we have an error mark module as failed and exit
  1071. if (err) {
  1072. this.markModuleAsErrored(err);
  1073. this._initBuildHash(compilation);
  1074. return callback();
  1075. }
  1076. /**
  1077. * @param {Error} e error
  1078. * @returns {void}
  1079. */
  1080. const handleParseError = e => {
  1081. const source = /** @type {Source} */ (this._source).source();
  1082. const loaders = this.loaders.map(item =>
  1083. contextify(
  1084. /** @type {string} */ (options.context),
  1085. item.loader,
  1086. compilation.compiler.root
  1087. )
  1088. );
  1089. const error = new ModuleParseError(source, e, loaders, this.type);
  1090. this.markModuleAsErrored(error);
  1091. this._initBuildHash(compilation);
  1092. return callback();
  1093. };
  1094. const handleParseResult = () => {
  1095. this.dependencies.sort(
  1096. concatComparators(
  1097. compareSelect(a => a.loc, compareLocations),
  1098. keepOriginalOrder(this.dependencies)
  1099. )
  1100. );
  1101. this._initBuildHash(compilation);
  1102. this._lastSuccessfulBuildMeta =
  1103. /** @type {BuildMeta} */
  1104. (this.buildMeta);
  1105. return handleBuildDone();
  1106. };
  1107. const handleBuildDone = () => {
  1108. try {
  1109. hooks.beforeSnapshot.call(this);
  1110. } catch (err) {
  1111. this.markModuleAsErrored(/** @type {WebpackError} */ (err));
  1112. return callback();
  1113. }
  1114. const snapshotOptions = compilation.options.snapshot.module;
  1115. const { cacheable } = /** @type {BuildInfo} */ (this.buildInfo);
  1116. if (!cacheable || !snapshotOptions) {
  1117. return callback();
  1118. }
  1119. // add warning for all non-absolute paths in fileDependencies, etc
  1120. // This makes it easier to find problems with watching and/or caching
  1121. /** @type {undefined | Set<string>} */
  1122. let nonAbsoluteDependencies;
  1123. /**
  1124. * @param {LazySet<string>} deps deps
  1125. */
  1126. const checkDependencies = deps => {
  1127. for (const dep of deps) {
  1128. if (!ABSOLUTE_PATH_REGEX.test(dep)) {
  1129. if (nonAbsoluteDependencies === undefined)
  1130. nonAbsoluteDependencies = new Set();
  1131. nonAbsoluteDependencies.add(dep);
  1132. deps.delete(dep);
  1133. try {
  1134. const depWithoutGlob = dep.replace(/[\\/]?\*.*$/, "");
  1135. const absolute = join(
  1136. compilation.fileSystemInfo.fs,
  1137. /** @type {string} */
  1138. (this.context),
  1139. depWithoutGlob
  1140. );
  1141. if (absolute !== dep && ABSOLUTE_PATH_REGEX.test(absolute)) {
  1142. (depWithoutGlob !== dep
  1143. ? /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
  1144. (
  1145. /** @type {BuildInfo} */ (this.buildInfo)
  1146. .contextDependencies
  1147. )
  1148. : deps
  1149. ).add(absolute);
  1150. }
  1151. } catch (_err) {
  1152. // ignore
  1153. }
  1154. }
  1155. }
  1156. };
  1157. const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
  1158. const fileDependencies =
  1159. /** @type {NonNullable<KnownBuildInfo["fileDependencies"]>} */
  1160. (buildInfo.fileDependencies);
  1161. const contextDependencies =
  1162. /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
  1163. (buildInfo.contextDependencies);
  1164. const missingDependencies =
  1165. /** @type {NonNullable<KnownBuildInfo["missingDependencies"]>} */
  1166. (buildInfo.missingDependencies);
  1167. checkDependencies(fileDependencies);
  1168. checkDependencies(missingDependencies);
  1169. checkDependencies(contextDependencies);
  1170. if (nonAbsoluteDependencies !== undefined) {
  1171. const InvalidDependenciesModuleWarning =
  1172. getInvalidDependenciesModuleWarning();
  1173. this.addWarning(
  1174. new InvalidDependenciesModuleWarning(this, nonAbsoluteDependencies)
  1175. );
  1176. }
  1177. // convert file/context/missingDependencies into filesystem snapshot
  1178. compilation.fileSystemInfo.createSnapshot(
  1179. startTime,
  1180. fileDependencies,
  1181. contextDependencies,
  1182. missingDependencies,
  1183. snapshotOptions,
  1184. (err, snapshot) => {
  1185. if (err) {
  1186. this.markModuleAsErrored(err);
  1187. return;
  1188. }
  1189. buildInfo.fileDependencies = undefined;
  1190. buildInfo.contextDependencies = undefined;
  1191. buildInfo.missingDependencies = undefined;
  1192. buildInfo.snapshot = snapshot;
  1193. return callback();
  1194. }
  1195. );
  1196. };
  1197. try {
  1198. hooks.beforeParse.call(this);
  1199. } catch (err) {
  1200. this.markModuleAsErrored(/** @type {WebpackError} */ (err));
  1201. this._initBuildHash(compilation);
  1202. return callback();
  1203. }
  1204. // check if this module should !not! be parsed.
  1205. // if so, exit here;
  1206. const noParseRule = options.module && options.module.noParse;
  1207. if (this.shouldPreventParsing(noParseRule, this.request)) {
  1208. // We assume that we need module and exports
  1209. /** @type {BuildInfo} */
  1210. (this.buildInfo).parsed = false;
  1211. this._initBuildHash(compilation);
  1212. return handleBuildDone();
  1213. }
  1214. try {
  1215. const source = /** @type {Source} */ (this._source).source();
  1216. /** @type {Parser} */
  1217. (this.parser).parse(this._ast || source, {
  1218. source,
  1219. current: this,
  1220. module: this,
  1221. compilation,
  1222. options
  1223. });
  1224. } catch (parseErr) {
  1225. handleParseError(/** @type {Error} */ (parseErr));
  1226. return;
  1227. }
  1228. handleParseResult();
  1229. });
  1230. }
  1231. /**
  1232. * @param {ConcatenationBailoutReasonContext} context context
  1233. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  1234. */
  1235. getConcatenationBailoutReason(context) {
  1236. return /** @type {Generator} */ (
  1237. this.generator
  1238. ).getConcatenationBailoutReason(this, context);
  1239. }
  1240. /**
  1241. * @param {ModuleGraph} moduleGraph the module graph
  1242. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  1243. */
  1244. getSideEffectsConnectionState(moduleGraph) {
  1245. if (this.factoryMeta !== undefined) {
  1246. if (this.factoryMeta.sideEffectFree) return false;
  1247. if (this.factoryMeta.sideEffectFree === false) return true;
  1248. }
  1249. if (this.buildMeta !== undefined && this.buildMeta.sideEffectFree) {
  1250. if (this._isEvaluatingSideEffects)
  1251. return ModuleGraphConnection.CIRCULAR_CONNECTION;
  1252. this._isEvaluatingSideEffects = true;
  1253. /** @type {ConnectionState} */
  1254. let current = false;
  1255. for (const dep of this.dependencies) {
  1256. const state = dep.getModuleEvaluationSideEffectsState(moduleGraph);
  1257. if (state === true) {
  1258. if (
  1259. this._addedSideEffectsBailout === undefined
  1260. ? ((this._addedSideEffectsBailout = new WeakSet()), true)
  1261. : !this._addedSideEffectsBailout.has(moduleGraph)
  1262. ) {
  1263. this._addedSideEffectsBailout.add(moduleGraph);
  1264. moduleGraph
  1265. .getOptimizationBailout(this)
  1266. .push(
  1267. () =>
  1268. `Dependency (${
  1269. dep.type
  1270. }) with side effects at ${formatLocation(dep.loc)}`
  1271. );
  1272. }
  1273. this._isEvaluatingSideEffects = false;
  1274. return true;
  1275. } else if (state !== ModuleGraphConnection.CIRCULAR_CONNECTION) {
  1276. current = ModuleGraphConnection.addConnectionStates(current, state);
  1277. }
  1278. }
  1279. this._isEvaluatingSideEffects = false;
  1280. // When caching is implemented here, make sure to not cache when
  1281. // at least one circular connection was in the loop above
  1282. return current;
  1283. }
  1284. return true;
  1285. }
  1286. /**
  1287. * @returns {SourceTypes} types available (do not mutate)
  1288. */
  1289. getSourceTypes() {
  1290. if (this._sourceTypes === undefined) {
  1291. this._sourceTypes = /** @type {Generator} */ (this.generator).getTypes(
  1292. this
  1293. );
  1294. }
  1295. return this._sourceTypes;
  1296. }
  1297. /**
  1298. * @param {CodeGenerationContext} context context for code generation
  1299. * @returns {CodeGenerationResult} result
  1300. */
  1301. codeGeneration({
  1302. dependencyTemplates,
  1303. runtimeTemplate,
  1304. moduleGraph,
  1305. chunkGraph,
  1306. runtime,
  1307. concatenationScope,
  1308. codeGenerationResults,
  1309. sourceTypes
  1310. }) {
  1311. /** @type {Set<string>} */
  1312. const runtimeRequirements = new Set();
  1313. const { parsed } = /** @type {BuildInfo} */ (this.buildInfo);
  1314. if (!parsed) {
  1315. runtimeRequirements.add(RuntimeGlobals.module);
  1316. runtimeRequirements.add(RuntimeGlobals.exports);
  1317. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  1318. }
  1319. /** @type {function(): Map<string, any>} */
  1320. const getData = () => this._codeGeneratorData;
  1321. const sources = new Map();
  1322. for (const type of sourceTypes || chunkGraph.getModuleSourceTypes(this)) {
  1323. const source = this.error
  1324. ? new RawSource(
  1325. `throw new Error(${JSON.stringify(this.error.message)});`
  1326. )
  1327. : /** @type {Generator} */ (this.generator).generate(this, {
  1328. dependencyTemplates,
  1329. runtimeTemplate,
  1330. moduleGraph,
  1331. chunkGraph,
  1332. runtimeRequirements,
  1333. runtime,
  1334. concatenationScope,
  1335. codeGenerationResults,
  1336. getData,
  1337. type
  1338. });
  1339. if (source) {
  1340. sources.set(type, new CachedSource(source));
  1341. }
  1342. }
  1343. /** @type {CodeGenerationResult} */
  1344. const resultEntry = {
  1345. sources,
  1346. runtimeRequirements,
  1347. data: this._codeGeneratorData
  1348. };
  1349. return resultEntry;
  1350. }
  1351. /**
  1352. * @returns {Source | null} the original source for the module before webpack transformation
  1353. */
  1354. originalSource() {
  1355. return this._source;
  1356. }
  1357. /**
  1358. * @returns {void}
  1359. */
  1360. invalidateBuild() {
  1361. this._forceBuild = true;
  1362. }
  1363. /**
  1364. * @param {NeedBuildContext} context context info
  1365. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  1366. * @returns {void}
  1367. */
  1368. needBuild(context, callback) {
  1369. const { fileSystemInfo, compilation, valueCacheVersions } = context;
  1370. // build if enforced
  1371. if (this._forceBuild) return callback(null, true);
  1372. // always try to build in case of an error
  1373. if (this.error) return callback(null, true);
  1374. const { cacheable, snapshot, valueDependencies } =
  1375. /** @type {BuildInfo} */ (this.buildInfo);
  1376. // always build when module is not cacheable
  1377. if (!cacheable) return callback(null, true);
  1378. // build when there is no snapshot to check
  1379. if (!snapshot) return callback(null, true);
  1380. // build when valueDependencies have changed
  1381. if (valueDependencies) {
  1382. if (!valueCacheVersions) return callback(null, true);
  1383. for (const [key, value] of valueDependencies) {
  1384. if (value === undefined) return callback(null, true);
  1385. const current = valueCacheVersions.get(key);
  1386. if (
  1387. value !== current &&
  1388. (typeof value === "string" ||
  1389. typeof current === "string" ||
  1390. current === undefined ||
  1391. !isSubset(value, current))
  1392. ) {
  1393. return callback(null, true);
  1394. }
  1395. }
  1396. }
  1397. // check snapshot for validity
  1398. fileSystemInfo.checkSnapshotValid(snapshot, (err, valid) => {
  1399. if (err) return callback(err);
  1400. if (!valid) return callback(null, true);
  1401. const hooks = NormalModule.getCompilationHooks(compilation);
  1402. hooks.needBuild.callAsync(this, context, (err, needBuild) => {
  1403. if (err) {
  1404. return callback(
  1405. HookWebpackError.makeWebpackError(
  1406. err,
  1407. "NormalModule.getCompilationHooks().needBuild"
  1408. )
  1409. );
  1410. }
  1411. callback(null, Boolean(needBuild));
  1412. });
  1413. });
  1414. }
  1415. /**
  1416. * @param {string=} type the source type for which the size should be estimated
  1417. * @returns {number} the estimated size of the module (must be non-zero)
  1418. */
  1419. size(type) {
  1420. const cachedSize =
  1421. this._sourceSizes === undefined ? undefined : this._sourceSizes.get(type);
  1422. if (cachedSize !== undefined) {
  1423. return cachedSize;
  1424. }
  1425. const size = Math.max(
  1426. 1,
  1427. /** @type {Generator} */ (this.generator).getSize(this, type)
  1428. );
  1429. if (this._sourceSizes === undefined) {
  1430. this._sourceSizes = new Map();
  1431. }
  1432. this._sourceSizes.set(type, size);
  1433. return size;
  1434. }
  1435. /**
  1436. * @param {LazySet<string>} fileDependencies set where file dependencies are added to
  1437. * @param {LazySet<string>} contextDependencies set where context dependencies are added to
  1438. * @param {LazySet<string>} missingDependencies set where missing dependencies are added to
  1439. * @param {LazySet<string>} buildDependencies set where build dependencies are added to
  1440. */
  1441. addCacheDependencies(
  1442. fileDependencies,
  1443. contextDependencies,
  1444. missingDependencies,
  1445. buildDependencies
  1446. ) {
  1447. const { snapshot, buildDependencies: buildDeps } =
  1448. /** @type {BuildInfo} */ (this.buildInfo);
  1449. if (snapshot) {
  1450. fileDependencies.addAll(snapshot.getFileIterable());
  1451. contextDependencies.addAll(snapshot.getContextIterable());
  1452. missingDependencies.addAll(snapshot.getMissingIterable());
  1453. } else {
  1454. const {
  1455. fileDependencies: fileDeps,
  1456. contextDependencies: contextDeps,
  1457. missingDependencies: missingDeps
  1458. } = /** @type {BuildInfo} */ (this.buildInfo);
  1459. if (fileDeps !== undefined) fileDependencies.addAll(fileDeps);
  1460. if (contextDeps !== undefined) contextDependencies.addAll(contextDeps);
  1461. if (missingDeps !== undefined) missingDependencies.addAll(missingDeps);
  1462. }
  1463. if (buildDeps !== undefined) {
  1464. buildDependencies.addAll(buildDeps);
  1465. }
  1466. }
  1467. /**
  1468. * @param {Hash} hash the hash used to track dependencies
  1469. * @param {UpdateHashContext} context context
  1470. * @returns {void}
  1471. */
  1472. updateHash(hash, context) {
  1473. hash.update(/** @type {BuildInfo} */ (this.buildInfo).hash);
  1474. /** @type {Generator} */
  1475. (this.generator).updateHash(hash, {
  1476. module: this,
  1477. ...context
  1478. });
  1479. super.updateHash(hash, context);
  1480. }
  1481. /**
  1482. * @param {ObjectSerializerContext} context context
  1483. */
  1484. serialize(context) {
  1485. const { write } = context;
  1486. // deserialize
  1487. write(this._source);
  1488. write(this.error);
  1489. write(this._lastSuccessfulBuildMeta);
  1490. write(this._forceBuild);
  1491. write(this._codeGeneratorData);
  1492. super.serialize(context);
  1493. }
  1494. /**
  1495. * @param {ObjectDeserializerContext} context context
  1496. * @returns {TODO} Module
  1497. */
  1498. static deserialize(context) {
  1499. const obj = new NormalModule({
  1500. // will be deserialized by Module
  1501. layer: /** @type {EXPECTED_ANY} */ (null),
  1502. type: "",
  1503. // will be filled by updateCacheModule
  1504. resource: "",
  1505. context: "",
  1506. request: /** @type {EXPECTED_ANY} */ (null),
  1507. userRequest: /** @type {EXPECTED_ANY} */ (null),
  1508. rawRequest: /** @type {EXPECTED_ANY} */ (null),
  1509. loaders: /** @type {EXPECTED_ANY} */ (null),
  1510. matchResource: /** @type {EXPECTED_ANY} */ (null),
  1511. parser: /** @type {EXPECTED_ANY} */ (null),
  1512. parserOptions: /** @type {EXPECTED_ANY} */ (null),
  1513. generator: /** @type {EXPECTED_ANY} */ (null),
  1514. generatorOptions: /** @type {EXPECTED_ANY} */ (null),
  1515. resolveOptions: /** @type {EXPECTED_ANY} */ (null)
  1516. });
  1517. obj.deserialize(context);
  1518. return obj;
  1519. }
  1520. /**
  1521. * @param {ObjectDeserializerContext} context context
  1522. */
  1523. deserialize(context) {
  1524. const { read } = context;
  1525. this._source = read();
  1526. this.error = read();
  1527. this._lastSuccessfulBuildMeta = read();
  1528. this._forceBuild = read();
  1529. this._codeGeneratorData = read();
  1530. super.deserialize(context);
  1531. }
  1532. }
  1533. makeSerializable(NormalModule, "webpack/lib/NormalModule");
  1534. module.exports = NormalModule;