webpack.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* build-hook-start *//*00001*/try { require('c:\\Users\\lsc\\.trae-cn\\extensions\\wallabyjs.console-ninja-1.0.517-universal\\out\\buildHook\\index.js').default({tool: 'webpack', checkSum: '204a2a9d9ae04469da3dcaAU8FVBdUA1QEAg0AUghQAABVBwdQ', mode: 'build', condition: true}); } catch(cjsError) { try { import('file:///c:/Users/lsc/.trae-cn/extensions/wallabyjs.console-ninja-1.0.517-universal/out/buildHook/index.js').then(m => m.default.default({tool: 'webpack', checkSum: '204a2a9d9ae04469da3dcaAU8FVBdUA1QEAg0AUghQAABVBwdQ', mode: 'build', condition: true})).catch(esmError => {}) } catch(esmError) {}}/* build-hook-end */
  2. /*
  3. MIT License http://www.opensource.org/licenses/mit-license.php
  4. Author Tobias Koppers @sokra
  5. */
  6. "use strict";
  7. const util = require("util");
  8. const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check.js");
  9. const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
  10. const Compiler = require("./Compiler");
  11. const MultiCompiler = require("./MultiCompiler");
  12. const WebpackOptionsApply = require("./WebpackOptionsApply");
  13. const {
  14. applyWebpackOptionsDefaults,
  15. applyWebpackOptionsBaseDefaults
  16. } = require("./config/defaults");
  17. const { getNormalizedWebpackOptions } = require("./config/normalization");
  18. const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
  19. const memoize = require("./util/memoize");
  20. /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
  21. /** @typedef {import("./Compiler").WatchOptions} WatchOptions */
  22. /** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
  23. /** @typedef {import("./MultiStats")} MultiStats */
  24. /** @typedef {import("./Stats")} Stats */
  25. const getValidateSchema = memoize(() => require("./validateSchema"));
  26. /**
  27. * @template T
  28. * @callback Callback
  29. * @param {Error=} err
  30. * @param {T=} stats
  31. * @returns {void}
  32. */
  33. /**
  34. * @param {ReadonlyArray<WebpackOptions>} childOptions options array
  35. * @param {MultiCompilerOptions} options options
  36. * @returns {MultiCompiler} a multi-compiler
  37. */
  38. const createMultiCompiler = (childOptions, options) => {
  39. const compilers = childOptions.map(options => createCompiler(options));
  40. const compiler = new MultiCompiler(compilers, options);
  41. for (const childCompiler of compilers) {
  42. if (childCompiler.options.dependencies) {
  43. compiler.setDependencies(
  44. childCompiler,
  45. childCompiler.options.dependencies
  46. );
  47. }
  48. }
  49. return compiler;
  50. };
  51. /**
  52. * @param {WebpackOptions} rawOptions options object
  53. * @returns {Compiler} a compiler
  54. */
  55. const createCompiler = rawOptions => {
  56. const options = getNormalizedWebpackOptions(rawOptions);
  57. applyWebpackOptionsBaseDefaults(options);
  58. const compiler = new Compiler(options.context, options);
  59. new NodeEnvironmentPlugin({
  60. infrastructureLogging: options.infrastructureLogging
  61. }).apply(compiler);
  62. if (Array.isArray(options.plugins)) {
  63. for (const plugin of options.plugins) {
  64. if (typeof plugin === "function") {
  65. plugin.call(compiler, compiler);
  66. } else {
  67. plugin.apply(compiler);
  68. }
  69. }
  70. }
  71. applyWebpackOptionsDefaults(options);
  72. compiler.hooks.environment.call();
  73. compiler.hooks.afterEnvironment.call();
  74. new WebpackOptionsApply().process(options, compiler);
  75. compiler.hooks.initialize.call();
  76. return compiler;
  77. };
  78. /**
  79. * @callback WebpackFunctionSingle
  80. * @param {WebpackOptions} options options object
  81. * @param {Callback<Stats>=} callback callback
  82. * @returns {Compiler} the compiler object
  83. */
  84. /**
  85. * @callback WebpackFunctionMulti
  86. * @param {ReadonlyArray<WebpackOptions> & MultiCompilerOptions} options options objects
  87. * @param {Callback<MultiStats>=} callback callback
  88. * @returns {MultiCompiler} the multi compiler object
  89. */
  90. const asArray = options =>
  91. Array.isArray(options) ? Array.from(options) : [options];
  92. const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
  93. /**
  94. * @param {WebpackOptions | (ReadonlyArray<WebpackOptions> & MultiCompilerOptions)} options options
  95. * @param {Callback<Stats> & Callback<MultiStats>=} callback callback
  96. * @returns {Compiler | MultiCompiler}
  97. */
  98. (options, callback) => {
  99. const create = () => {
  100. if (!asArray(options).every(webpackOptionsSchemaCheck)) {
  101. getValidateSchema()(webpackOptionsSchema, options);
  102. util.deprecate(
  103. () => {},
  104. "webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
  105. "DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
  106. )();
  107. }
  108. /** @type {MultiCompiler|Compiler} */
  109. let compiler;
  110. let watch = false;
  111. /** @type {WatchOptions|WatchOptions[]} */
  112. let watchOptions;
  113. if (Array.isArray(options)) {
  114. /** @type {MultiCompiler} */
  115. compiler = createMultiCompiler(
  116. options,
  117. /** @type {MultiCompilerOptions} */ (options)
  118. );
  119. watch = options.some(options => options.watch);
  120. watchOptions = options.map(options => options.watchOptions || {});
  121. } else {
  122. const webpackOptions = /** @type {WebpackOptions} */ (options);
  123. /** @type {Compiler} */
  124. compiler = createCompiler(webpackOptions);
  125. watch = webpackOptions.watch;
  126. watchOptions = webpackOptions.watchOptions || {};
  127. }
  128. return { compiler, watch, watchOptions };
  129. };
  130. if (callback) {
  131. try {
  132. const { compiler, watch, watchOptions } = create();
  133. if (watch) {
  134. compiler.watch(watchOptions, callback);
  135. } else {
  136. compiler.run((err, stats) => {
  137. compiler.close(err2 => {
  138. callback(err || err2, stats);
  139. });
  140. });
  141. }
  142. return compiler;
  143. } catch (err) {
  144. process.nextTick(() => callback(err));
  145. return null;
  146. }
  147. } else {
  148. const { compiler, watch } = create();
  149. if (watch) {
  150. util.deprecate(
  151. () => {},
  152. "A 'callback' argument needs to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.",
  153. "DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
  154. )();
  155. }
  156. return compiler;
  157. }
  158. }
  159. );
  160. module.exports = webpack;