WebpackOptionsDefaulter.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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", "call", value => Object.assign({}, value));
  16. this.set("module.unknownContextRequest", ".");
  17. this.set("module.unknownContextRegExp", false);
  18. this.set("module.unknownContextRecursive", true);
  19. this.set("module.unknownContextCritical", true);
  20. this.set("module.exprContextRequest", ".");
  21. this.set("module.exprContextRegExp", false);
  22. this.set("module.exprContextRecursive", true);
  23. this.set("module.exprContextCritical", true);
  24. this.set("module.wrappedContextRegExp", /.*/);
  25. this.set("module.wrappedContextRecursive", true);
  26. this.set("module.wrappedContextCritical", false);
  27. this.set("module.strictExportPresence", false);
  28. this.set("module.strictThisContextOnImports", false);
  29. this.set("module.unsafeCache", true);
  30. this.set("output", "call", (value, options) => {
  31. if(typeof value === "string") {
  32. return {
  33. filename: value
  34. };
  35. } else if(typeof value !== "object") {
  36. return {};
  37. } else {
  38. return Object.assign({}, value);
  39. }
  40. });
  41. this.set("output.filename", "[name].js");
  42. this.set("output.chunkFilename", "make", (options) => {
  43. const filename = options.output.filename;
  44. return filename.indexOf("[name]") >= 0 ? filename.replace("[name]", "[id]") : "[id]." + filename;
  45. });
  46. this.set("output.library", "");
  47. this.set("output.hotUpdateFunction", "make", (options) => {
  48. return Template.toIdentifier("webpackHotUpdate" + options.output.library);
  49. });
  50. this.set("output.jsonpFunction", "make", (options) => {
  51. return Template.toIdentifier("webpackJsonp" + options.output.library);
  52. });
  53. this.set("output.libraryTarget", "var");
  54. this.set("output.path", process.cwd());
  55. this.set("output.sourceMapFilename", "[file].map[query]");
  56. this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
  57. this.set("output.hotUpdateMainFilename", "[hash].hot-update.json");
  58. this.set("output.crossOriginLoading", false);
  59. this.set("output.jsonpScriptType", "text/javascript");
  60. this.set("output.chunkLoadTimeout", 120000);
  61. this.set("output.hashFunction", "md5");
  62. this.set("output.hashDigest", "hex");
  63. this.set("output.hashDigestLength", 20);
  64. this.set("output.devtoolLineToLine", false);
  65. this.set("output.strictModuleExceptionHandling", false);
  66. this.set("node", "call", value => {
  67. if(typeof value === "boolean") {
  68. return value;
  69. } else {
  70. return Object.assign({}, value);
  71. }
  72. });
  73. this.set("node.console", false);
  74. this.set("node.process", true);
  75. this.set("node.global", true);
  76. this.set("node.Buffer", true);
  77. this.set("node.setImmediate", true);
  78. this.set("node.__filename", "mock");
  79. this.set("node.__dirname", "mock");
  80. this.set("performance", "call", value => {
  81. if(typeof value === "boolean") {
  82. return value;
  83. } else {
  84. return Object.assign({}, value);
  85. }
  86. });
  87. this.set("performance.maxAssetSize", 250000);
  88. this.set("performance.maxEntrypointSize", 250000);
  89. this.set("performance.hints", false);
  90. this.set("resolve", "call", value => Object.assign({}, value));
  91. this.set("resolve.unsafeCache", true);
  92. this.set("resolve.modules", ["node_modules"]);
  93. this.set("resolve.extensions", [".js", ".json"]);
  94. this.set("resolve.mainFiles", ["index"]);
  95. this.set("resolve.aliasFields", "make", (options) => {
  96. if(options.target === "web" || options.target === "webworker")
  97. return ["browser"];
  98. else
  99. return [];
  100. });
  101. this.set("resolve.mainFields", "make", (options) => {
  102. if(options.target === "web" || options.target === "webworker")
  103. return ["browser", "module", "main"];
  104. else
  105. return ["module", "main"];
  106. });
  107. this.set("resolve.cacheWithContext", "make", (options) => {
  108. return Array.isArray(options.resolve.plugins) && options.resolve.plugins.length > 0;
  109. });
  110. this.set("resolveLoader", "call", value => Object.assign({}, value));
  111. this.set("resolveLoader.unsafeCache", true);
  112. this.set("resolveLoader.mainFields", ["loader", "main"]);
  113. this.set("resolveLoader.extensions", [".js", ".json"]);
  114. this.set("resolveLoader.mainFiles", ["index"]);
  115. this.set("resolveLoader.cacheWithContext", "make", (options) => {
  116. return Array.isArray(options.resolveLoader.plugins) && options.resolveLoader.plugins.length > 0;
  117. });
  118. }
  119. }
  120. module.exports = WebpackOptionsDefaulter;