webpack.js 3.9 KB

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