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