webpack.js 6.5 KB

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