NormalModule.js 53 KB

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