webpack.js 6.6 KB

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