WebpackOptionsDefaulter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const OptionsDefaulter = require("./OptionsDefaulter");
  7. const Template = require("./Template");
  8. class WebpackOptionsDefaulter extends OptionsDefaulter {
  9. constructor() {
  10. super();
  11. this.set("devtool", false);
  12. this.set("cache", true);
  13. this.set("context", process.cwd());
  14. this.set("target", "web");
  15. this.set("module.unknownContextRequest", ".");
  16. this.set("module.unknownContextRegExp", false);
  17. this.set("module.unknownContextRecursive", true);
  18. this.set("module.unknownContextCritical", true);
  19. this.set("module.exprContextRequest", ".");
  20. this.set("module.exprContextRegExp", false);
  21. this.set("module.exprContextRecursive", true);
  22. this.set("module.exprContextCritical", true);
  23. this.set("module.wrappedContextRegExp", /.*/);
  24. this.set("module.wrappedContextRecursive", true);
  25. this.set("module.wrappedContextCritical", false);
  26. this.set("module.strictExportPresence", false);
  27. this.set("module.strictThisContextOnImports", false);
  28. this.set("module.unsafeCache", true);
  29. this.set("output", "call", (value, options) => {
  30. if(typeof value === "string") {
  31. return {
  32. filename: value
  33. };
  34. } else if(typeof value !== "object") {
  35. return {};
  36. } else {
  37. return value;
  38. }
  39. });
  40. this.set("output.filename", "[name].js");
  41. this.set("output.chunkFilename", "make", (options) => {
  42. const filename = options.output.filename;
  43. return filename.indexOf("[name]") >= 0 ? filename.replace("[name]", "[id]") : "[id]." + filename;
  44. });
  45. this.set("output.library", "");
  46. this.set("output.hotUpdateFunction", "make", (options) => {
  47. return Template.toIdentifier("webpackHotUpdate" + options.output.library);
  48. });
  49. this.set("output.jsonpFunction", "make", (options) => {
  50. return Template.toIdentifier("webpackJsonp" + options.output.library);
  51. });
  52. this.set("output.libraryTarget", "var");
  53. this.set("output.path", process.cwd());
  54. this.set("output.sourceMapFilename", "[file].map[query]");
  55. this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
  56. this.set("output.hotUpdateMainFilename", "[hash].hot-update.json");
  57. this.set("output.crossOriginLoading", false);
  58. this.set("output.chunkLoadTimeout", 120000);
  59. this.set("output.hashFunction", "md5");
  60. this.set("output.hashDigest", "hex");
  61. this.set("output.hashDigestLength", 20);
  62. this.set("output.devtoolLineToLine", false);
  63. this.set("output.strictModuleExceptionHandling", false);
  64. this.set("node", {});
  65. this.set("node.console", false);
  66. this.set("node.process", true);
  67. this.set("node.global", true);
  68. this.set("node.Buffer", true);
  69. this.set("node.setImmediate", true);
  70. this.set("node.__filename", "mock");
  71. this.set("node.__dirname", "mock");
  72. this.set("performance.maxAssetSize", 250000);
  73. this.set("performance.maxEntrypointSize", 250000);
  74. this.set("performance.hints", false);
  75. this.set("resolve", {});
  76. this.set("resolve.unsafeCache", true);
  77. this.set("resolve.modules", ["node_modules"]);
  78. this.set("resolve.extensions", [".js", ".json"]);
  79. this.set("resolve.mainFiles", ["index"]);
  80. this.set("resolve.aliasFields", "make", (options) => {
  81. if(options.target === "web" || options.target === "webworker")
  82. return ["browser"];
  83. else
  84. return [];
  85. });
  86. this.set("resolve.mainFields", "make", (options) => {
  87. if(options.target === "web" || options.target === "webworker")
  88. return ["browser", "module", "main"];
  89. else
  90. return ["module", "main"];
  91. });
  92. this.set("resolve.cacheWithContext", "make", (options) => {
  93. return Array.isArray(options.resolve.plugins) && options.resolve.plugins.length > 0;
  94. });
  95. this.set("resolveLoader", {});
  96. this.set("resolveLoader.unsafeCache", true);
  97. this.set("resolveLoader.mainFields", ["loader", "main"]);
  98. this.set("resolveLoader.extensions", [".js", ".json"]);
  99. this.set("resolveLoader.mainFiles", ["index"]);
  100. this.set("resolveLoader.cacheWithContext", "make", (options) => {
  101. return Array.isArray(options.resolveLoader.plugins) && options.resolveLoader.plugins.length > 0;
  102. });
  103. }
  104. }
  105. module.exports = WebpackOptionsDefaulter;