NormalModule.js 51 KB

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