NormalModule.js 51 KB

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