index.js 16 KB

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