123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/webpackOptionsSchema.json");
  14. function webpack(options, callback) {
  15. const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, options);
  16. if(webpackOptionsValidationErrors.length) {
  17. throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
  18. }
  19. let compiler;
  20. if(Array.isArray(options)) {
  21. compiler = new MultiCompiler(options.map(options => webpack(options)));
  22. } else if(typeof options === "object") {
  23. // TODO webpack 4: process returns options
  24. new WebpackOptionsDefaulter().process(options);
  25. compiler = new Compiler();
  26. compiler.context = options.context;
  27. compiler.options = options;
  28. new NodeEnvironmentPlugin().apply(compiler);
  29. if(options.plugins && Array.isArray(options.plugins)) {
  30. compiler.apply.apply(compiler, options.plugins);
  31. }
  32. compiler.applyPlugins("environment");
  33. compiler.applyPlugins("after-environment");
  34. compiler.options = new WebpackOptionsApply().process(options, compiler);
  35. } else {
  36. throw new Error("Invalid argument: options");
  37. }
  38. if(callback) {
  39. if(typeof callback !== "function") throw new Error("Invalid argument: callback");
  40. if(options.watch === true || (Array.isArray(options) && options.some(o => o.watch))) {
  41. const watchOptions = Array.isArray(options) ? options.map(o => o.watchOptions || {}) : (options.watchOptions || {});
  42. return compiler.watch(watchOptions, callback);
  43. }
  44. compiler.run(callback);
  45. }
  46. return compiler;
  47. }
  48. exports = module.exports = webpack;
  49. webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
  50. webpack.WebpackOptionsApply = WebpackOptionsApply;
  51. webpack.Compiler = Compiler;
  52. webpack.MultiCompiler = MultiCompiler;
  53. webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
  54. webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
  55. webpack.validateSchema = validateSchema;
  56. webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
  57. function exportPlugins(obj, mappings) {
  58. Object.keys(mappings).forEach(name => {
  59. Object.defineProperty(obj, name, {
  60. configurable: false,
  61. enumerable: true,
  62. get: mappings[name]
  63. });
  64. });
  65. }
  66. exportPlugins(exports, {
  67. "DefinePlugin": () => require("./DefinePlugin"),
  68. "NormalModuleReplacementPlugin": () => require("./NormalModuleReplacementPlugin"),
  69. "ContextReplacementPlugin": () => require("./ContextReplacementPlugin"),
  70. "ContextExclusionPlugin": () => require("./ContextExclusionPlugin"),
  71. "IgnorePlugin": () => require("./IgnorePlugin"),
  72. "WatchIgnorePlugin": () => require("./WatchIgnorePlugin"),
  73. "BannerPlugin": () => require("./BannerPlugin"),
  74. "PrefetchPlugin": () => require("./PrefetchPlugin"),
  75. "AutomaticPrefetchPlugin": () => require("./AutomaticPrefetchPlugin"),
  76. "ProvidePlugin": () => require("./ProvidePlugin"),
  77. "HotModuleReplacementPlugin": () => require("./HotModuleReplacementPlugin"),
  78. "SourceMapDevToolPlugin": () => require("./SourceMapDevToolPlugin"),
  79. "EvalSourceMapDevToolPlugin": () => require("./EvalSourceMapDevToolPlugin"),
  80. "EvalDevToolModulePlugin": () => require("./EvalDevToolModulePlugin"),
  81. "CachePlugin": () => require("./CachePlugin"),
  82. "ExtendedAPIPlugin": () => require("./ExtendedAPIPlugin"),
  83. "ExternalsPlugin": () => require("./ExternalsPlugin"),
  84. "JsonpTemplatePlugin": () => require("./JsonpTemplatePlugin"),
  85. "LibraryTemplatePlugin": () => require("./LibraryTemplatePlugin"),
  86. "LoaderTargetPlugin": () => require("./LoaderTargetPlugin"),
  87. "MemoryOutputFileSystem": () => require("./MemoryOutputFileSystem"),
  88. "ProgressPlugin": () => require("./ProgressPlugin"),
  89. "SetVarMainTemplatePlugin": () => require("./SetVarMainTemplatePlugin"),
  90. "UmdMainTemplatePlugin": () => require("./UmdMainTemplatePlugin"),
  91. "NoErrorsPlugin": () => require("./NoErrorsPlugin"),
  92. "NoEmitOnErrorsPlugin": () => require("./NoEmitOnErrorsPlugin"),
  93. "NewWatchingPlugin": () => require("./NewWatchingPlugin"),
  94. "EnvironmentPlugin": () => require("./EnvironmentPlugin"),
  95. "DllPlugin": () => require("./DllPlugin"),
  96. "DllReferencePlugin": () => require("./DllReferencePlugin"),
  97. "LoaderOptionsPlugin": () => require("./LoaderOptionsPlugin"),
  98. "NamedModulesPlugin": () => require("./NamedModulesPlugin"),
  99. "NamedChunksPlugin": () => require("./NamedChunksPlugin"),
  100. "HashedModuleIdsPlugin": () => require("./HashedModuleIdsPlugin"),
  101. "ModuleFilenameHelpers": () => require("./ModuleFilenameHelpers")
  102. });
  103. exportPlugins(exports.optimize = {}, {
  104. "AggressiveMergingPlugin": () => require("./optimize/AggressiveMergingPlugin"),
  105. "AggressiveSplittingPlugin": () => require("./optimize/AggressiveSplittingPlugin"),
  106. "CommonsChunkPlugin": () => require("./optimize/CommonsChunkPlugin"),
  107. "ChunkModuleIdRangePlugin": () => require("./optimize/ChunkModuleIdRangePlugin"),
  108. "DedupePlugin": () => require("./optimize/DedupePlugin"),
  109. "LimitChunkCountPlugin": () => require("./optimize/LimitChunkCountPlugin"),
  110. "MinChunkSizePlugin": () => require("./optimize/MinChunkSizePlugin"),
  111. "ModuleConcatenationPlugin": () => require("./optimize/ModuleConcatenationPlugin"),
  112. "OccurrenceOrderPlugin": () => require("./optimize/OccurrenceOrderPlugin"),
  113. "UglifyJsPlugin": () => require("./optimize/UglifyJsPlugin")
  114. });