-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mergeOptions.js
129 lines (108 loc) · 3.39 KB
/
mergeOptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import ensureArray from '../../../src/utils/ensureArray.js';
import deprecateOptions from '../../../src/utils/deprecateOptions.js';
export default function mergeOptions ( config, command ) {
// deprecations... TODO
const deprecations = deprecate( config, command );
function getOption(name) {
return command[name] !== undefined ? command[name] : config[name];
}
const inputOptions = {
input: getOption('input'),
legacy: getOption('legacy'),
treeshake: getOption('treeshake'),
acorn: config.acorn,
context: config.context,
moduleContext: config.moduleContext,
plugins: config.plugins,
onwarn: config.onwarn,
watch: config.watch
};
// legacy, to ensure e.g. commonjs plugin still works
inputOptions.entry = inputOptions.input;
const commandExternal = ( command.external || '' ).split( ',' );
const configExternal = config.external;
if ( command.globals ) {
const globals = Object.create( null );
command.globals.split( ',' ).forEach( str => {
const names = str.split( ':' );
globals[ names[0] ] = names[1];
// Add missing Module IDs to external.
if ( commandExternal.indexOf( names[0] ) === -1 ) {
commandExternal.push( names[0] );
}
});
command.globals = globals;
}
if ( typeof configExternal === 'function' ) {
inputOptions.external = id => {
return configExternal( id ) || ~commandExternal.indexOf( id );
};
} else {
inputOptions.external = ( configExternal || [] ).concat( commandExternal );
}
if (command.silent) {
inputOptions.onwarn = () => {};
}
const baseOutputOptions = {
extend: command.extend !== undefined ? command.extend : config.extend,
amd: Object.assign({}, config.amd, command.amd),
banner: getOption('banner'),
footer: getOption('footer'),
intro: getOption('intro'),
outro: getOption('outro'),
sourcemap: getOption('sourcemap'),
name: getOption('name'),
globals: getOption('globals'),
interop: getOption('interop'),
legacy: getOption('legacy'),
indent: getOption('indent'),
strict: getOption('strict'),
noConflict: getOption('noConflict'),
paths: getOption('paths')
};
let mergedOutputOptions;
if (Array.isArray(config.output)) {
mergedOutputOptions = config.output.map((output) => Object.assign({}, output, command.output));
} else if (config.output && command.output) {
mergedOutputOptions = [Object.assign({}, config.output, command.output)];
} else {
mergedOutputOptions = (command.output || config.output) ?
ensureArray(command.output || config.output) :
[{
file: command.output ? command.output.file : null,
format: command.output ? command.output.format : null
}];
}
const outputOptions = mergedOutputOptions.map(output => {
return Object.assign({}, baseOutputOptions, output);
});
return { inputOptions, outputOptions, deprecations };
}
function deprecate( config, command ) {
const deprecations = [];
// CLI
if ( command.id ) {
deprecations.push({
old: '-u/--id',
new: '--amd.id'
});
(command.amd || (command.amd = {})).id = command.id;
}
if ( typeof command.output === 'string' ) {
deprecations.push({
old: '--output',
new: '--output.file'
});
command.output = { file: command.output };
}
if ( command.format ) {
deprecations.push({
old: '--format',
new: '--output.format'
});
(command.output || (command.output = {})).format = command.format;
}
// config file
deprecations.push(...deprecateOptions(config));
return deprecations;
}