NormalModule.js 55 KB

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