index.js 16 KB

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