WebpackOptionsDefaulter.js 4.5KB

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.chunkLoadTimeout", 120000);
  60. this.set("output.hashFunction", "md5");
  61. this.set("output.hashDigest", "hex");
  62. this.set("output.hashDigestLength", 20);
  63. this.set("output.devtoolLineToLine", false);
  64. this.set("output.strictModuleExceptionHandling", false);
  65. this.set("node", "call", value => {
  66. if(typeof value === "boolean") {
  67. return value;
  68. } else {
  69. return Object.assign({}, value);
  70. }
  71. });
  72. this.set("node.console", false);
  73. this.set("node.process", true);
  74. this.set("node.global", true);
  75. this.set("node.Buffer", true);
  76. this.set("node.setImmediate", true);
  77. this.set("node.__filename", "mock");
  78. this.set("node.__dirname", "mock");
  79. this.set("performance", "call", value => {
  80. if(typeof value === "boolean") {
  81. return value;
  82. } else {
  83. return Object.assign({}, value);
  84. }
  85. });
  86. this.set("performance.maxAssetSize", 250000);
  87. this.set("performance.maxEntrypointSize", 250000);
  88. this.set("performance.hints", false);
  89. this.set("resolve", "call", value => Object.assign({}, value));
  90. this.set("resolve.unsafeCache", true);
  91. this.set("resolve.modules", ["node_modules"]);
  92. this.set("resolve.extensions", [".js", ".json"]);
  93. this.set("resolve.mainFiles", ["index"]);
  94. this.set("resolve.aliasFields", "make", (options) => {
  95. if(options.target === "web" || options.target === "webworker")
  96. return ["browser"];
  97. else
  98. return [];
  99. });
  100. this.set("resolve.mainFields", "make", (options) => {
  101. if(options.target === "web" || options.target === "webworker")
  102. return ["browser", "module", "main"];
  103. else
  104. return ["module", "main"];
  105. });
  106. this.set("resolve.cacheWithContext", "make", (options) => {
  107. return Array.isArray(options.resolve.plugins) && options.resolve.plugins.length > 0;
  108. });
  109. this.set("resolveLoader", "call", value => Object.assign({}, value));
  110. this.set("resolveLoader.unsafeCache", true);
  111. this.set("resolveLoader.mainFields", ["loader", "main"]);
  112. this.set("resolveLoader.extensions", [".js", ".json"]);
  113. this.set("resolveLoader.mainFiles", ["index"]);
  114. this.set("resolveLoader.cacheWithContext", "make", (options) => {
  115. return Array.isArray(options.resolveLoader.plugins) && options.resolveLoader.plugins.length > 0;
  116. });
  117. }
  118. }
  119. module.exports = WebpackOptionsDefaulter;