mergeOptions.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. @license
  3. Rollup.js v2.79.1
  4. Thu, 22 Sep 2022 04:55:29 GMT - commit 69ff4181e701a0fe0026d0ba147f31bc86beffa8
  5. https://github.com/rollup/rollup
  6. Released under the MIT License.
  7. */
  8. 'use strict';
  9. const rollup = require('./rollup.js');
  10. const commandAliases = {
  11. c: 'config',
  12. d: 'dir',
  13. e: 'external',
  14. f: 'format',
  15. g: 'globals',
  16. h: 'help',
  17. i: 'input',
  18. m: 'sourcemap',
  19. n: 'name',
  20. o: 'file',
  21. p: 'plugin',
  22. v: 'version',
  23. w: 'watch'
  24. };
  25. function mergeOptions(config, rawCommandOptions = { external: [], globals: undefined }, defaultOnWarnHandler = rollup.defaultOnWarn) {
  26. const command = getCommandOptions(rawCommandOptions);
  27. const inputOptions = mergeInputOptions(config, command, defaultOnWarnHandler);
  28. const warn = inputOptions.onwarn;
  29. if (command.output) {
  30. Object.assign(command, command.output);
  31. }
  32. const outputOptionsArray = rollup.ensureArray(config.output);
  33. if (outputOptionsArray.length === 0)
  34. outputOptionsArray.push({});
  35. const outputOptions = outputOptionsArray.map(singleOutputOptions => mergeOutputOptions(singleOutputOptions, command, warn));
  36. rollup.warnUnknownOptions(command, Object.keys(inputOptions).concat(Object.keys(outputOptions[0]).filter(option => option !== 'sourcemapPathTransform'), Object.keys(commandAliases), 'config', 'environment', 'plugin', 'silent', 'failAfterWarnings', 'stdin', 'waitForBundleInput', 'configPlugin'), 'CLI flags', warn, /^_$|output$|config/);
  37. inputOptions.output = outputOptions;
  38. return inputOptions;
  39. }
  40. function getCommandOptions(rawCommandOptions) {
  41. const external = rawCommandOptions.external && typeof rawCommandOptions.external === 'string'
  42. ? rawCommandOptions.external.split(',')
  43. : [];
  44. return {
  45. ...rawCommandOptions,
  46. external,
  47. globals: typeof rawCommandOptions.globals === 'string'
  48. ? rawCommandOptions.globals.split(',').reduce((globals, globalDefinition) => {
  49. const [id, variableName] = globalDefinition.split(':');
  50. globals[id] = variableName;
  51. if (!external.includes(id)) {
  52. external.push(id);
  53. }
  54. return globals;
  55. }, Object.create(null))
  56. : undefined
  57. };
  58. }
  59. function mergeInputOptions(config, overrides, defaultOnWarnHandler) {
  60. const getOption = (name) => { var _a; return (_a = overrides[name]) !== null && _a !== void 0 ? _a : config[name]; };
  61. const inputOptions = {
  62. acorn: getOption('acorn'),
  63. acornInjectPlugins: config.acornInjectPlugins,
  64. cache: config.cache,
  65. context: getOption('context'),
  66. experimentalCacheExpiry: getOption('experimentalCacheExpiry'),
  67. external: getExternal(config, overrides),
  68. inlineDynamicImports: getOption('inlineDynamicImports'),
  69. input: getOption('input') || [],
  70. makeAbsoluteExternalsRelative: getOption('makeAbsoluteExternalsRelative'),
  71. manualChunks: getOption('manualChunks'),
  72. maxParallelFileOps: getOption('maxParallelFileOps'),
  73. maxParallelFileReads: getOption('maxParallelFileReads'),
  74. moduleContext: getOption('moduleContext'),
  75. onwarn: getOnWarn(config, defaultOnWarnHandler),
  76. perf: getOption('perf'),
  77. plugins: rollup.ensureArray(config.plugins),
  78. preserveEntrySignatures: getOption('preserveEntrySignatures'),
  79. preserveModules: getOption('preserveModules'),
  80. preserveSymlinks: getOption('preserveSymlinks'),
  81. shimMissingExports: getOption('shimMissingExports'),
  82. strictDeprecations: getOption('strictDeprecations'),
  83. treeshake: getObjectOption(config, overrides, 'treeshake', rollup.objectifyOptionWithPresets(rollup.treeshakePresets, 'treeshake', 'false, true, ')),
  84. watch: getWatch(config, overrides)
  85. };
  86. rollup.warnUnknownOptions(config, Object.keys(inputOptions), 'input options', inputOptions.onwarn, /^output$/);
  87. return inputOptions;
  88. }
  89. const getExternal = (config, overrides) => {
  90. const configExternal = config.external;
  91. return typeof configExternal === 'function'
  92. ? (source, importer, isResolved) => configExternal(source, importer, isResolved) || overrides.external.includes(source)
  93. : rollup.ensureArray(configExternal).concat(overrides.external);
  94. };
  95. const getOnWarn = (config, defaultOnWarnHandler) => config.onwarn
  96. ? warning => config.onwarn(warning, defaultOnWarnHandler)
  97. : defaultOnWarnHandler;
  98. const getObjectOption = (config, overrides, name, objectifyValue = rollup.objectifyOption) => {
  99. const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue);
  100. const configOption = normalizeObjectOptionValue(config[name], objectifyValue);
  101. if (commandOption !== undefined) {
  102. return commandOption && { ...configOption, ...commandOption };
  103. }
  104. return configOption;
  105. };
  106. const getWatch = (config, overrides) => config.watch !== false && getObjectOption(config, overrides, 'watch');
  107. const isWatchEnabled = (optionValue) => {
  108. if (Array.isArray(optionValue)) {
  109. return optionValue.reduce((result, value) => (typeof value === 'boolean' ? value : result), false);
  110. }
  111. return optionValue === true;
  112. };
  113. const normalizeObjectOptionValue = (optionValue, objectifyValue) => {
  114. if (!optionValue) {
  115. return optionValue;
  116. }
  117. if (Array.isArray(optionValue)) {
  118. return optionValue.reduce((result, value) => value && result && { ...result, ...objectifyValue(value) }, {});
  119. }
  120. return objectifyValue(optionValue);
  121. };
  122. function mergeOutputOptions(config, overrides, warn) {
  123. const getOption = (name) => { var _a; return (_a = overrides[name]) !== null && _a !== void 0 ? _a : config[name]; };
  124. const outputOptions = {
  125. amd: getObjectOption(config, overrides, 'amd'),
  126. assetFileNames: getOption('assetFileNames'),
  127. banner: getOption('banner'),
  128. chunkFileNames: getOption('chunkFileNames'),
  129. compact: getOption('compact'),
  130. dir: getOption('dir'),
  131. dynamicImportFunction: getOption('dynamicImportFunction'),
  132. entryFileNames: getOption('entryFileNames'),
  133. esModule: getOption('esModule'),
  134. exports: getOption('exports'),
  135. extend: getOption('extend'),
  136. externalLiveBindings: getOption('externalLiveBindings'),
  137. file: getOption('file'),
  138. footer: getOption('footer'),
  139. format: getOption('format'),
  140. freeze: getOption('freeze'),
  141. generatedCode: getObjectOption(config, overrides, 'generatedCode', rollup.objectifyOptionWithPresets(rollup.generatedCodePresets, 'output.generatedCode', '')),
  142. globals: getOption('globals'),
  143. hoistTransitiveImports: getOption('hoistTransitiveImports'),
  144. indent: getOption('indent'),
  145. inlineDynamicImports: getOption('inlineDynamicImports'),
  146. interop: getOption('interop'),
  147. intro: getOption('intro'),
  148. manualChunks: getOption('manualChunks'),
  149. minifyInternalExports: getOption('minifyInternalExports'),
  150. name: getOption('name'),
  151. namespaceToStringTag: getOption('namespaceToStringTag'),
  152. noConflict: getOption('noConflict'),
  153. outro: getOption('outro'),
  154. paths: getOption('paths'),
  155. plugins: rollup.ensureArray(config.plugins),
  156. preferConst: getOption('preferConst'),
  157. preserveModules: getOption('preserveModules'),
  158. preserveModulesRoot: getOption('preserveModulesRoot'),
  159. sanitizeFileName: getOption('sanitizeFileName'),
  160. sourcemap: getOption('sourcemap'),
  161. sourcemapBaseUrl: getOption('sourcemapBaseUrl'),
  162. sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
  163. sourcemapFile: getOption('sourcemapFile'),
  164. sourcemapPathTransform: getOption('sourcemapPathTransform'),
  165. strict: getOption('strict'),
  166. systemNullSetters: getOption('systemNullSetters'),
  167. validate: getOption('validate')
  168. };
  169. rollup.warnUnknownOptions(config, Object.keys(outputOptions), 'output options', warn);
  170. return outputOptions;
  171. }
  172. exports.commandAliases = commandAliases;
  173. exports.isWatchEnabled = isWatchEnabled;
  174. exports.mergeOptions = mergeOptions;
  175. //# sourceMappingURL=mergeOptions.js.map