NormalModule.js 50 KB

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