index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const memoize = require("./util/memoize");
  8. /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
  9. /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */
  10. /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */
  11. /** @typedef {import("../declarations/WebpackOptions").ExternalItemFunctionData} ExternalItemFunctionData */
  12. /** @typedef {import("../declarations/WebpackOptions").ExternalItemObjectKnown} ExternalItemObjectKnown */
  13. /** @typedef {import("../declarations/WebpackOptions").ExternalItemObjectUnknown} ExternalItemObjectUnknown */
  14. /** @typedef {import("../declarations/WebpackOptions").ExternalItemValue} ExternalItemValue */
  15. /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
  16. /** @typedef {import("../declarations/WebpackOptions").FileCacheOptions} FileCacheOptions */
  17. /** @typedef {import("../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  18. /** @typedef {import("../declarations/WebpackOptions").MemoryCacheOptions} MemoryCacheOptions */
  19. /** @typedef {import("../declarations/WebpackOptions").ModuleOptions} ModuleOptions */
  20. /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
  21. /** @typedef {import("../declarations/WebpackOptions").RuleSetCondition} RuleSetCondition */
  22. /** @typedef {import("../declarations/WebpackOptions").RuleSetConditionAbsolute} RuleSetConditionAbsolute */
  23. /** @typedef {import("../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
  24. /** @typedef {import("../declarations/WebpackOptions").RuleSetUse} RuleSetUse */
  25. /** @typedef {import("../declarations/WebpackOptions").RuleSetUseItem} RuleSetUseItem */
  26. /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
  27. /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} Configuration */
  28. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
  29. /** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
  30. /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
  31. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  32. /** @typedef {import("./Compilation").Asset} Asset */
  33. /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
  34. /** @typedef {import("./Compilation").EntryOptions} EntryOptions */
  35. /** @typedef {import("./Compilation").PathData} PathData */
  36. /** @typedef {import("./Compiler").AssetEmittedInfo} AssetEmittedInfo */
  37. /** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
  38. /** @typedef {import("./MultiStats")} MultiStats */
  39. /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
  40. /** @typedef {import("./Parser").ParserState} ParserState */
  41. /** @typedef {import("./ResolverFactory").ResolvePluginInstance} ResolvePluginInstance */
  42. /** @typedef {import("./ResolverFactory").Resolver} Resolver */
  43. /** @typedef {import("./Watching")} Watching */
  44. /** @typedef {import("./cli").Argument} Argument */
  45. /** @typedef {import("./cli").Problem} Problem */
  46. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
  47. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
  48. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */
  49. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkOrigin} StatsChunkOrigin */
  50. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
  51. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsError} StatsError */
  52. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsLogging} StatsLogging */
  53. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsLoggingEntry} StatsLoggingEntry */
  54. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModule} StatsModule */
  55. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleIssuer} StatsModuleIssuer */
  56. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleReason} StatsModuleReason */
  57. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleTraceDependency} StatsModuleTraceDependency */
  58. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleTraceItem} StatsModuleTraceItem */
  59. /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsProfile} StatsProfile */
  60. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  61. /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
  62. /**
  63. * @template {Function} T
  64. * @param {function(): T} factory factory function
  65. * @returns {T} function
  66. */
  67. const lazyFunction = factory => {
  68. const fac = memoize(factory);
  69. const f = /** @type {any} */ (
  70. (...args) => {
  71. return fac()(...args);
  72. }
  73. );
  74. return /** @type {T} */ (f);
  75. };
  76. /**
  77. * @template A
  78. * @template B
  79. * @param {A} obj input a
  80. * @param {B} exports input b
  81. * @returns {A & B} merged
  82. */
  83. const mergeExports = (obj, exports) => {
  84. const descriptors = Object.getOwnPropertyDescriptors(exports);
  85. for (const name of Object.keys(descriptors)) {
  86. const descriptor = descriptors[name];
  87. if (descriptor.get) {
  88. const fn = descriptor.get;
  89. Object.defineProperty(obj, name, {
  90. configurable: false,
  91. enumerable: true,
  92. get: memoize(fn)
  93. });
  94. } else if (typeof descriptor.value === "object") {
  95. Object.defineProperty(obj, name, {
  96. configurable: false,
  97. enumerable: true,
  98. writable: false,
  99. value: mergeExports({}, descriptor.value)
  100. });
  101. } else {
  102. throw new Error(
  103. "Exposed values must be either a getter or an nested object"
  104. );
  105. }
  106. }
  107. return /** @type {A & B} */ (Object.freeze(obj));
  108. };
  109. const fn = lazyFunction(() => require("./webpack"));
  110. module.exports = mergeExports(fn, {
  111. get webpack() {
  112. return require("./webpack");
  113. },
  114. get validate() {
  115. const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check.js");
  116. const getRealValidate = memoize(() => {
  117. const validateSchema = require("./validateSchema");
  118. const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
  119. return options => validateSchema(webpackOptionsSchema, options);
  120. });
  121. return options => {
  122. if (!webpackOptionsSchemaCheck(options)) getRealValidate()(options);
  123. };
  124. },
  125. get validateSchema() {
  126. const validateSchema = require("./validateSchema");
  127. return validateSchema;
  128. },
  129. get version() {
  130. return /** @type {string} */ (require("../package.json").version);
  131. },
  132. get cli() {
  133. return require("./cli");
  134. },
  135. get AutomaticPrefetchPlugin() {
  136. return require("./AutomaticPrefetchPlugin");
  137. },
  138. get AsyncDependenciesBlock() {
  139. return require("./AsyncDependenciesBlock");
  140. },
  141. get BannerPlugin() {
  142. return require("./BannerPlugin");
  143. },
  144. get Cache() {
  145. return require("./Cache");
  146. },
  147. get Chunk() {
  148. return require("./Chunk");
  149. },
  150. get ChunkGraph() {
  151. return require("./ChunkGraph");
  152. },
  153. get CleanPlugin() {
  154. return require("./CleanPlugin");
  155. },
  156. get Compilation() {
  157. return require("./Compilation");
  158. },
  159. get Compiler() {
  160. return require("./Compiler");
  161. },
  162. get ConcatenationScope() {
  163. return require("./ConcatenationScope");
  164. },
  165. get ContextExclusionPlugin() {
  166. return require("./ContextExclusionPlugin");
  167. },
  168. get ContextReplacementPlugin() {
  169. return require("./ContextReplacementPlugin");
  170. },
  171. get DefinePlugin() {
  172. return require("./DefinePlugin");
  173. },
  174. get DelegatedPlugin() {
  175. return require("./DelegatedPlugin");
  176. },
  177. get Dependency() {
  178. return require("./Dependency");
  179. },
  180. get DllPlugin() {
  181. return require("./DllPlugin");
  182. },
  183. get DllReferencePlugin() {
  184. return require("./DllReferencePlugin");
  185. },
  186. get DynamicEntryPlugin() {
  187. return require("./DynamicEntryPlugin");
  188. },
  189. get EntryOptionPlugin() {
  190. return require("./EntryOptionPlugin");
  191. },
  192. get EntryPlugin() {
  193. return require("./EntryPlugin");
  194. },
  195. get EnvironmentPlugin() {
  196. return require("./EnvironmentPlugin");
  197. },
  198. get EvalDevToolModulePlugin() {
  199. return require("./EvalDevToolModulePlugin");
  200. },
  201. get EvalSourceMapDevToolPlugin() {
  202. return require("./EvalSourceMapDevToolPlugin");
  203. },
  204. get ExternalModule() {
  205. return require("./ExternalModule");
  206. },
  207. get ExternalsPlugin() {
  208. return require("./ExternalsPlugin");
  209. },
  210. get Generator() {
  211. return require("./Generator");
  212. },
  213. get HotUpdateChunk() {
  214. return require("./HotUpdateChunk");
  215. },
  216. get HotModuleReplacementPlugin() {
  217. return require("./HotModuleReplacementPlugin");
  218. },
  219. get InitFragment() {
  220. return require("./InitFragment");
  221. },
  222. get IgnorePlugin() {
  223. return require("./IgnorePlugin");
  224. },
  225. get JavascriptModulesPlugin() {
  226. return util.deprecate(
  227. () => require("./javascript/JavascriptModulesPlugin"),
  228. "webpack.JavascriptModulesPlugin has moved to webpack.javascript.JavascriptModulesPlugin",
  229. "DEP_WEBPACK_JAVASCRIPT_MODULES_PLUGIN"
  230. )();
  231. },
  232. get LibManifestPlugin() {
  233. return require("./LibManifestPlugin");
  234. },
  235. get LibraryTemplatePlugin() {
  236. return util.deprecate(
  237. () => require("./LibraryTemplatePlugin"),
  238. "webpack.LibraryTemplatePlugin is deprecated and has been replaced by compilation.outputOptions.library or compilation.addEntry + passing a library option",
  239. "DEP_WEBPACK_LIBRARY_TEMPLATE_PLUGIN"
  240. )();
  241. },
  242. get LoaderOptionsPlugin() {
  243. return require("./LoaderOptionsPlugin");
  244. },
  245. get LoaderTargetPlugin() {
  246. return require("./LoaderTargetPlugin");
  247. },
  248. get Module() {
  249. return require("./Module");
  250. },
  251. get ModuleFilenameHelpers() {
  252. return require("./ModuleFilenameHelpers");
  253. },
  254. get ModuleGraph() {
  255. return require("./ModuleGraph");
  256. },
  257. get ModuleGraphConnection() {
  258. return require("./ModuleGraphConnection");
  259. },
  260. get NoEmitOnErrorsPlugin() {
  261. return require("./NoEmitOnErrorsPlugin");
  262. },
  263. get NormalModule() {
  264. return require("./NormalModule");
  265. },
  266. get NormalModuleReplacementPlugin() {
  267. return require("./NormalModuleReplacementPlugin");
  268. },
  269. get MultiCompiler() {
  270. return require("./MultiCompiler");
  271. },
  272. get OptimizationStages() {
  273. return require("./OptimizationStages");
  274. },
  275. get Parser() {
  276. return require("./Parser");
  277. },
  278. get PlatformPlugin() {
  279. return require("./PlatformPlugin");
  280. },
  281. get PrefetchPlugin() {
  282. return require("./PrefetchPlugin");
  283. },
  284. get ProgressPlugin() {
  285. return require("./ProgressPlugin");
  286. },
  287. get ProvidePlugin() {
  288. return require("./ProvidePlugin");
  289. },
  290. get RuntimeGlobals() {
  291. return require("./RuntimeGlobals");
  292. },
  293. get RuntimeModule() {
  294. return require("./RuntimeModule");
  295. },
  296. get SingleEntryPlugin() {
  297. return util.deprecate(
  298. () => require("./EntryPlugin"),
  299. "SingleEntryPlugin was renamed to EntryPlugin",
  300. "DEP_WEBPACK_SINGLE_ENTRY_PLUGIN"
  301. )();
  302. },
  303. get SourceMapDevToolPlugin() {
  304. return require("./SourceMapDevToolPlugin");
  305. },
  306. get Stats() {
  307. return require("./Stats");
  308. },
  309. get Template() {
  310. return require("./Template");
  311. },
  312. get UsageState() {
  313. return require("./ExportsInfo").UsageState;
  314. },
  315. get WatchIgnorePlugin() {
  316. return require("./WatchIgnorePlugin");
  317. },
  318. get WebpackError() {
  319. return require("./WebpackError");
  320. },
  321. get WebpackOptionsApply() {
  322. return require("./WebpackOptionsApply");
  323. },
  324. get WebpackOptionsDefaulter() {
  325. return util.deprecate(
  326. () => require("./WebpackOptionsDefaulter"),
  327. "webpack.WebpackOptionsDefaulter is deprecated and has been replaced by webpack.config.getNormalizedWebpackOptions and webpack.config.applyWebpackOptionsDefaults",
  328. "DEP_WEBPACK_OPTIONS_DEFAULTER"
  329. )();
  330. },
  331. // TODO webpack 6 deprecate
  332. get WebpackOptionsValidationError() {
  333. return require("schema-utils").ValidationError;
  334. },
  335. get ValidationError() {
  336. return require("schema-utils").ValidationError;
  337. },
  338. cache: {
  339. get MemoryCachePlugin() {
  340. return require("./cache/MemoryCachePlugin");
  341. }
  342. },
  343. config: {
  344. get getNormalizedWebpackOptions() {
  345. return require("./config/normalization").getNormalizedWebpackOptions;
  346. },
  347. get applyWebpackOptionsDefaults() {
  348. return require("./config/defaults").applyWebpackOptionsDefaults;
  349. }
  350. },
  351. dependencies: {
  352. get ModuleDependency() {
  353. return require("./dependencies/ModuleDependency");
  354. },
  355. get HarmonyImportDependency() {
  356. return require("./dependencies/HarmonyImportDependency");
  357. },
  358. get ConstDependency() {
  359. return require("./dependencies/ConstDependency");
  360. },
  361. get NullDependency() {
  362. return require("./dependencies/NullDependency");
  363. }
  364. },
  365. ids: {
  366. get ChunkModuleIdRangePlugin() {
  367. return require("./ids/ChunkModuleIdRangePlugin");
  368. },
  369. get NaturalModuleIdsPlugin() {
  370. return require("./ids/NaturalModuleIdsPlugin");
  371. },
  372. get OccurrenceModuleIdsPlugin() {
  373. return require("./ids/OccurrenceModuleIdsPlugin");
  374. },
  375. get NamedModuleIdsPlugin() {
  376. return require("./ids/NamedModuleIdsPlugin");
  377. },
  378. get DeterministicChunkIdsPlugin() {
  379. return require("./ids/DeterministicChunkIdsPlugin");
  380. },
  381. get DeterministicModuleIdsPlugin() {
  382. return require("./ids/DeterministicModuleIdsPlugin");
  383. },
  384. get NamedChunkIdsPlugin() {
  385. return require("./ids/NamedChunkIdsPlugin");
  386. },
  387. get OccurrenceChunkIdsPlugin() {
  388. return require("./ids/OccurrenceChunkIdsPlugin");
  389. },
  390. get HashedModuleIdsPlugin() {
  391. return require("./ids/HashedModuleIdsPlugin");
  392. }
  393. },
  394. javascript: {
  395. get EnableChunkLoadingPlugin() {
  396. return require("./javascript/EnableChunkLoadingPlugin");
  397. },
  398. get JavascriptModulesPlugin() {
  399. return require("./javascript/JavascriptModulesPlugin");
  400. },
  401. get JavascriptParser() {
  402. return require("./javascript/JavascriptParser");
  403. }
  404. },
  405. optimize: {
  406. get AggressiveMergingPlugin() {
  407. return require("./optimize/AggressiveMergingPlugin");
  408. },
  409. get AggressiveSplittingPlugin() {
  410. return util.deprecate(
  411. () => require("./optimize/AggressiveSplittingPlugin"),
  412. "AggressiveSplittingPlugin is deprecated in favor of SplitChunksPlugin",
  413. "DEP_WEBPACK_AGGRESSIVE_SPLITTING_PLUGIN"
  414. )();
  415. },
  416. get InnerGraph() {
  417. return require("./optimize/InnerGraph");
  418. },
  419. get LimitChunkCountPlugin() {
  420. return require("./optimize/LimitChunkCountPlugin");
  421. },
  422. get MinChunkSizePlugin() {
  423. return require("./optimize/MinChunkSizePlugin");
  424. },
  425. get ModuleConcatenationPlugin() {
  426. return require("./optimize/ModuleConcatenationPlugin");
  427. },
  428. get RealContentHashPlugin() {
  429. return require("./optimize/RealContentHashPlugin");
  430. },
  431. get RuntimeChunkPlugin() {
  432. return require("./optimize/RuntimeChunkPlugin");
  433. },
  434. get SideEffectsFlagPlugin() {
  435. return require("./optimize/SideEffectsFlagPlugin");
  436. },
  437. get SplitChunksPlugin() {
  438. return require("./optimize/SplitChunksPlugin");
  439. }
  440. },
  441. runtime: {
  442. get GetChunkFilenameRuntimeModule() {
  443. return require("./runtime/GetChunkFilenameRuntimeModule");
  444. },
  445. get LoadScriptRuntimeModule() {
  446. return require("./runtime/LoadScriptRuntimeModule");
  447. }
  448. },
  449. prefetch: {
  450. get ChunkPrefetchPreloadPlugin() {
  451. return require("./prefetch/ChunkPrefetchPreloadPlugin");
  452. }
  453. },
  454. web: {
  455. get FetchCompileAsyncWasmPlugin() {
  456. return require("./web/FetchCompileAsyncWasmPlugin");
  457. },
  458. get FetchCompileWasmPlugin() {
  459. return require("./web/FetchCompileWasmPlugin");
  460. },
  461. get JsonpChunkLoadingRuntimeModule() {
  462. return require("./web/JsonpChunkLoadingRuntimeModule");
  463. },
  464. get JsonpTemplatePlugin() {
  465. return require("./web/JsonpTemplatePlugin");
  466. }
  467. },
  468. webworker: {
  469. get WebWorkerTemplatePlugin() {
  470. return require("./webworker/WebWorkerTemplatePlugin");
  471. }
  472. },
  473. node: {
  474. get NodeEnvironmentPlugin() {
  475. return require("./node/NodeEnvironmentPlugin");
  476. },
  477. get NodeSourcePlugin() {
  478. return require("./node/NodeSourcePlugin");
  479. },
  480. get NodeTargetPlugin() {
  481. return require("./node/NodeTargetPlugin");
  482. },
  483. get NodeTemplatePlugin() {
  484. return require("./node/NodeTemplatePlugin");
  485. },
  486. get ReadFileCompileWasmPlugin() {
  487. return require("./node/ReadFileCompileWasmPlugin");
  488. }
  489. },
  490. electron: {
  491. get ElectronTargetPlugin() {
  492. return require("./electron/ElectronTargetPlugin");
  493. }
  494. },
  495. wasm: {
  496. get AsyncWebAssemblyModulesPlugin() {
  497. return require("./wasm-async/AsyncWebAssemblyModulesPlugin");
  498. },
  499. get EnableWasmLoadingPlugin() {
  500. return require("./wasm/EnableWasmLoadingPlugin");
  501. }
  502. },
  503. library: {
  504. get AbstractLibraryPlugin() {
  505. return require("./library/AbstractLibraryPlugin");
  506. },
  507. get EnableLibraryPlugin() {
  508. return require("./library/EnableLibraryPlugin");
  509. }
  510. },
  511. container: {
  512. get ContainerPlugin() {
  513. return require("./container/ContainerPlugin");
  514. },
  515. get ContainerReferencePlugin() {
  516. return require("./container/ContainerReferencePlugin");
  517. },
  518. get ModuleFederationPlugin() {
  519. return require("./container/ModuleFederationPlugin");
  520. },
  521. get scope() {
  522. return require("./container/options").scope;
  523. }
  524. },
  525. sharing: {
  526. get ConsumeSharedPlugin() {
  527. return require("./sharing/ConsumeSharedPlugin");
  528. },
  529. get ProvideSharedPlugin() {
  530. return require("./sharing/ProvideSharedPlugin");
  531. },
  532. get SharePlugin() {
  533. return require("./sharing/SharePlugin");
  534. },
  535. get scope() {
  536. return require("./container/options").scope;
  537. }
  538. },
  539. debug: {
  540. get ProfilingPlugin() {
  541. return require("./debug/ProfilingPlugin");
  542. }
  543. },
  544. util: {
  545. get createHash() {
  546. return require("./util/createHash");
  547. },
  548. get comparators() {
  549. return require("./util/comparators");
  550. },
  551. get runtime() {
  552. return require("./util/runtime");
  553. },
  554. get serialization() {
  555. return require("./util/serialization");
  556. },
  557. get cleverMerge() {
  558. return require("./util/cleverMerge").cachedCleverMerge;
  559. },
  560. get LazySet() {
  561. return require("./util/LazySet");
  562. },
  563. get compileBooleanMatcher() {
  564. return require("./util/compileBooleanMatcher");
  565. }
  566. },
  567. get sources() {
  568. return require("webpack-sources");
  569. },
  570. experiments: {
  571. schemes: {
  572. get HttpUriPlugin() {
  573. return require("./schemes/HttpUriPlugin");
  574. }
  575. },
  576. ids: {
  577. get SyncModuleIdsPlugin() {
  578. return require("./ids/SyncModuleIdsPlugin");
  579. }
  580. }
  581. }
  582. });