Config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const ChainedMap = require('./ChainedMap');
  2. const ChainedSet = require('./ChainedSet');
  3. const Resolve = require('./Resolve');
  4. const ResolveLoader = require('./ResolveLoader');
  5. const Output = require('./Output');
  6. const DevServer = require('./DevServer');
  7. const Plugin = require('./Plugin');
  8. const Module = require('./Module');
  9. const Optimization = require('./Optimization');
  10. const Performance = require('./Performance');
  11. module.exports = class extends ChainedMap {
  12. constructor() {
  13. super();
  14. this.devServer = new DevServer(this);
  15. this.entryPoints = new ChainedMap(this);
  16. this.module = new Module(this);
  17. this.node = new ChainedMap(this);
  18. this.optimization = new Optimization(this);
  19. this.output = new Output(this);
  20. this.performance = new Performance(this);
  21. this.plugins = new ChainedMap(this);
  22. this.resolve = new Resolve(this);
  23. this.resolveLoader = new ResolveLoader(this);
  24. this.extend([
  25. 'amd',
  26. 'bail',
  27. 'cache',
  28. 'context',
  29. 'devtool',
  30. 'externals',
  31. 'loader',
  32. 'mode',
  33. 'parallelism',
  34. 'profile',
  35. 'recordsInputPath',
  36. 'recordsPath',
  37. 'recordsOutputPath',
  38. 'stats',
  39. 'target',
  40. 'watch',
  41. 'watchOptions',
  42. ]);
  43. }
  44. static toString(config, { verbose = false, configPrefix = 'config' } = {}) {
  45. // eslint-disable-next-line global-require
  46. const stringify = require('javascript-stringify');
  47. return stringify(
  48. config,
  49. (value, indent, stringify) => {
  50. // improve plugin output
  51. if (value && value.__pluginName) {
  52. const prefix = `/* ${configPrefix}.plugin('${
  53. value.__pluginName
  54. }') */\n`;
  55. const constructorExpression = value.__pluginPath
  56. ? // The path is stringified to ensure special characters are escaped
  57. // (such as the backslashes in Windows-style paths).
  58. `(require(${stringify(value.__pluginPath)}))`
  59. : value.__pluginConstructorName;
  60. if (constructorExpression) {
  61. // get correct indentation for args by stringifying the args array and
  62. // discarding the square brackets.
  63. const args = stringify(value.__pluginArgs).slice(1, -1);
  64. return `${prefix}new ${constructorExpression}(${args})`;
  65. }
  66. return (
  67. prefix +
  68. stringify(
  69. value.__pluginArgs && value.__pluginArgs.length
  70. ? { args: value.__pluginArgs }
  71. : {}
  72. )
  73. );
  74. }
  75. // improve rule/use output
  76. if (value && value.__ruleNames) {
  77. const prefix = `/* ${configPrefix}.module.rule('${
  78. value.__ruleNames[0]
  79. }')${value.__ruleNames
  80. .slice(1)
  81. .map(r => `.oneOf('${r}')`)
  82. .join('')}${
  83. value.__useName ? `.use('${value.__useName}')` : ``
  84. } */\n`;
  85. return prefix + stringify(value);
  86. }
  87. // shorten long functions
  88. if (typeof value === 'function') {
  89. if (value.__expression) {
  90. return value.__expression;
  91. }
  92. if (!verbose && value.toString().length > 100) {
  93. return `function () { /* omitted long function */ }`;
  94. }
  95. }
  96. return stringify(value);
  97. },
  98. 2
  99. );
  100. }
  101. entry(name) {
  102. return this.entryPoints.getOrCompute(name, () => new ChainedSet(this));
  103. }
  104. plugin(name) {
  105. return this.plugins.getOrCompute(name, () => new Plugin(this, name));
  106. }
  107. toConfig() {
  108. const entryPoints = this.entryPoints.entries() || {};
  109. return this.clean(
  110. Object.assign(this.entries() || {}, {
  111. node: this.node.entries(),
  112. output: this.output.entries(),
  113. resolve: this.resolve.toConfig(),
  114. resolveLoader: this.resolveLoader.toConfig(),
  115. devServer: this.devServer.toConfig(),
  116. module: this.module.toConfig(),
  117. optimization: this.optimization.entries(),
  118. plugins: this.plugins.values().map(plugin => plugin.toConfig()),
  119. performance: this.performance.entries(),
  120. entry: Object.keys(entryPoints).reduce(
  121. (acc, key) =>
  122. Object.assign(acc, { [key]: entryPoints[key].values() }),
  123. {}
  124. ),
  125. })
  126. );
  127. }
  128. toString(options) {
  129. return module.exports.toString(this.toConfig(), options);
  130. }
  131. merge(obj = {}, omit = []) {
  132. const omissions = [
  133. 'node',
  134. 'output',
  135. 'resolve',
  136. 'resolveLoader',
  137. 'devServer',
  138. 'optimization',
  139. 'performance',
  140. 'module',
  141. ];
  142. if (!omit.includes('entry') && 'entry' in obj) {
  143. Object.keys(obj.entry).forEach(name =>
  144. this.entry(name).merge([].concat(obj.entry[name]))
  145. );
  146. }
  147. if (!omit.includes('plugin') && 'plugin' in obj) {
  148. Object.keys(obj.plugin).forEach(name =>
  149. this.plugin(name).merge(obj.plugin[name])
  150. );
  151. }
  152. omissions.forEach(key => {
  153. if (!omit.includes(key) && key in obj) {
  154. this[key].merge(obj[key]);
  155. }
  156. });
  157. return super.merge(obj, [...omit, ...omissions, 'entry', 'plugin']);
  158. }
  159. };