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