webpack.js 5.9 KB

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