webpack.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Compiler = require("./Compiler");
  7. const MultiCompiler = require("./MultiCompiler");
  8. const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
  9. const WebpackOptionsApply = require("./WebpackOptionsApply");
  10. const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
  11. const validateSchema = require("./validateSchema");
  12. const WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
  13. const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
  14. const RemovedPluginError = require("./RemovedPluginError");
  15. const version = require("../package.json").version;
  16. /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
  17. /**
  18. * @param {WebpackOptions} options options object
  19. * @param {function(Error=, Stats=): void=} callback callback
  20. * @returns {Compiler | MultiCompiler} the compiler object
  21. */
  22. const webpack = (options, callback) => {
  23. const webpackOptionsValidationErrors = validateSchema(
  24. webpackOptionsSchema,
  25. options
  26. );
  27. if (webpackOptionsValidationErrors.length) {
  28. throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
  29. }
  30. let compiler;
  31. if (Array.isArray(options)) {
  32. compiler = new MultiCompiler(options.map(options => webpack(options)));
  33. } else if (typeof options === "object") {
  34. options = new WebpackOptionsDefaulter().process(options);
  35. compiler = new Compiler(options.context);
  36. compiler.options = options;
  37. new NodeEnvironmentPlugin().apply(compiler);
  38. if (options.plugins && Array.isArray(options.plugins)) {
  39. for (const plugin of options.plugins) {
  40. if (typeof plugin === "function") {
  41. plugin.call(compiler, compiler);
  42. } else {
  43. plugin.apply(compiler);
  44. }
  45. }
  46. }
  47. compiler.hooks.environment.call();
  48. compiler.hooks.afterEnvironment.call();
  49. compiler.options = new WebpackOptionsApply().process(options, compiler);
  50. } else {
  51. throw new Error("Invalid argument: options");
  52. }
  53. if (callback) {
  54. if (typeof callback !== "function") {
  55. throw new Error("Invalid argument: callback");
  56. }
  57. if (
  58. options.watch === true ||
  59. (Array.isArray(options) && options.some(o => o.watch))
  60. ) {
  61. const watchOptions = Array.isArray(options)
  62. ? options.map(o => o.watchOptions || {})
  63. : options.watchOptions || {};
  64. return compiler.watch(watchOptions, callback);
  65. }
  66. compiler.run(callback);
  67. }
  68. return compiler;
  69. };
  70. exports = module.exports = webpack;
  71. exports.version = version;
  72. webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
  73. webpack.WebpackOptionsApply = WebpackOptionsApply;
  74. webpack.Compiler = Compiler;
  75. webpack.MultiCompiler = MultiCompiler;
  76. webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
  77. // @ts-ignore Global @this directive is not supported
  78. webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
  79. webpack.validateSchema = validateSchema;
  80. webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
  81. const exportPlugins = (obj, mappings) => {
  82. for (const name of Object.keys(mappings)) {
  83. Object.defineProperty(obj, name, {
  84. configurable: false,
  85. enumerable: true,
  86. get: mappings[name]
  87. });
  88. }
  89. };
  90. exportPlugins(exports, {
  91. AutomaticPrefetchPlugin: () => require("./AutomaticPrefetchPlugin"),
  92. BannerPlugin: () => require("./BannerPlugin"),
  93. CachePlugin: () => require("./CachePlugin"),
  94. ContextExclusionPlugin: () => require("./ContextExclusionPlugin"),
  95. ContextReplacementPlugin: () => require("./ContextReplacementPlugin"),
  96. DefinePlugin: () => require("./DefinePlugin"),
  97. Dependency: () => require("./Dependency"),
  98. DllPlugin: () => require("./DllPlugin"),
  99. DllReferencePlugin: () => require("./DllReferencePlugin"),
  100. EnvironmentPlugin: () => require("./EnvironmentPlugin"),
  101. EvalDevToolModulePlugin: () => require("./EvalDevToolModulePlugin"),
  102. EvalSourceMapDevToolPlugin: () => require("./EvalSourceMapDevToolPlugin"),
  103. ExtendedAPIPlugin: () => require("./ExtendedAPIPlugin"),
  104. ExternalsPlugin: () => require("./ExternalsPlugin"),
  105. HashedModuleIdsPlugin: () => require("./HashedModuleIdsPlugin"),
  106. HotModuleReplacementPlugin: () => require("./HotModuleReplacementPlugin"),
  107. IgnorePlugin: () => require("./IgnorePlugin"),
  108. LibraryTemplatePlugin: () => require("./LibraryTemplatePlugin"),
  109. LoaderOptionsPlugin: () => require("./LoaderOptionsPlugin"),
  110. LoaderTargetPlugin: () => require("./LoaderTargetPlugin"),
  111. MemoryOutputFileSystem: () => require("./MemoryOutputFileSystem"),
  112. Module: () => require("./Module"),
  113. ModuleFilenameHelpers: () => require("./ModuleFilenameHelpers"),
  114. NamedChunksPlugin: () => require("./NamedChunksPlugin"),
  115. NamedModulesPlugin: () => require("./NamedModulesPlugin"),
  116. NoEmitOnErrorsPlugin: () => require("./NoEmitOnErrorsPlugin"),
  117. NormalModuleReplacementPlugin: () =>
  118. require("./NormalModuleReplacementPlugin"),
  119. PrefetchPlugin: () => require("./PrefetchPlugin"),
  120. ProgressPlugin: () => require("./ProgressPlugin"),
  121. ProvidePlugin: () => require("./ProvidePlugin"),
  122. SetVarMainTemplatePlugin: () => require("./SetVarMainTemplatePlugin"),
  123. SingleEntryPlugin: () => require("./SingleEntryPlugin"),
  124. SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"),
  125. Stats: () => require("./Stats"),
  126. Template: () => require("./Template"),
  127. UmdMainTemplatePlugin: () => require("./UmdMainTemplatePlugin"),
  128. WatchIgnorePlugin: () => require("./WatchIgnorePlugin")
  129. });
  130. exportPlugins((exports.dependencies = {}), {
  131. DependencyReference: () => require("./dependencies/DependencyReference")
  132. });
  133. exportPlugins((exports.optimize = {}), {
  134. AggressiveMergingPlugin: () => require("./optimize/AggressiveMergingPlugin"),
  135. AggressiveSplittingPlugin: () =>
  136. require("./optimize/AggressiveSplittingPlugin"),
  137. ChunkModuleIdRangePlugin: () =>
  138. require("./optimize/ChunkModuleIdRangePlugin"),
  139. LimitChunkCountPlugin: () => require("./optimize/LimitChunkCountPlugin"),
  140. MinChunkSizePlugin: () => require("./optimize/MinChunkSizePlugin"),
  141. ModuleConcatenationPlugin: () =>
  142. require("./optimize/ModuleConcatenationPlugin"),
  143. OccurrenceOrderPlugin: () => require("./optimize/OccurrenceOrderPlugin"),
  144. OccurrenceModuleOrderPlugin: () =>
  145. require("./optimize/OccurrenceModuleOrderPlugin"),
  146. OccurrenceChunkOrderPlugin: () =>
  147. require("./optimize/OccurrenceChunkOrderPlugin"),
  148. RuntimeChunkPlugin: () => require("./optimize/RuntimeChunkPlugin"),
  149. SideEffectsFlagPlugin: () => require("./optimize/SideEffectsFlagPlugin"),
  150. SplitChunksPlugin: () => require("./optimize/SplitChunksPlugin")
  151. });
  152. exportPlugins((exports.web = {}), {
  153. FetchCompileWasmTemplatePlugin: () =>
  154. require("./web/FetchCompileWasmTemplatePlugin"),
  155. JsonpTemplatePlugin: () => require("./web/JsonpTemplatePlugin")
  156. });
  157. exportPlugins((exports.webworker = {}), {
  158. WebWorkerTemplatePlugin: () => require("./webworker/WebWorkerTemplatePlugin")
  159. });
  160. exportPlugins((exports.node = {}), {
  161. NodeTemplatePlugin: () => require("./node/NodeTemplatePlugin"),
  162. ReadFileCompileWasmTemplatePlugin: () =>
  163. require("./node/ReadFileCompileWasmTemplatePlugin")
  164. });
  165. exportPlugins((exports.debug = {}), {
  166. ProfilingPlugin: () => require("./debug/ProfilingPlugin")
  167. });
  168. exportPlugins((exports.util = {}), {
  169. createHash: () => require("./util/createHash")
  170. });
  171. const defineMissingPluginError = (namespace, pluginName, errorMessage) => {
  172. Object.defineProperty(namespace, pluginName, {
  173. configurable: false,
  174. enumerable: true,
  175. get() {
  176. throw new RemovedPluginError(errorMessage);
  177. }
  178. });
  179. };
  180. // TODO remove in webpack 5
  181. defineMissingPluginError(
  182. exports.optimize,
  183. "UglifyJsPlugin",
  184. "webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead."
  185. );
  186. // TODO remove in webpack 5
  187. defineMissingPluginError(
  188. exports.optimize,
  189. "CommonsChunkPlugin",
  190. "webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead."
  191. );