forked from rollup/rollup
/
normalizeOutputOptions.ts
370 lines (348 loc) · 12.3 KB
/
normalizeOutputOptions.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import {
GetInterop,
GlobalsOption,
InternalModuleFormat,
InteropType,
ManualChunksOption,
ModuleFormat,
NormalizedInputOptions,
NormalizedOutputOptions,
OptionsPaths,
OutputOptions,
SourcemapPathTransformOption
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
import { errInvalidExportOptionValue, error, warnDeprecation } from '../error';
import { resolve } from '../path';
import { GenericConfigObject, warnUnknownOptions } from './options';
export function normalizeOutputOptions(
config: GenericConfigObject,
inputOptions: NormalizedInputOptions,
unsetInputOptions: Set<string>
): { options: NormalizedOutputOptions; unsetOptions: Set<string> } {
// These are options that may trigger special warnings or behaviour later
// if the user did not select an explicit value
const unsetOptions = new Set(unsetInputOptions);
const compact = (config.compact as boolean | undefined) || false;
const format = getFormat(config);
const inlineDynamicImports = getInlineDynamicImports(config, inputOptions);
const preserveModules = getPreserveModules(config, inlineDynamicImports, inputOptions);
const file = getFile(config, preserveModules, inputOptions);
const outputOptions: NormalizedOutputOptions & OutputOptions = {
amd: getAmd(config),
assetFileNames:
(config.assetFileNames as string | undefined) ?? 'assets/[name]-[hash][extname]',
banner: getAddon(config, 'banner'),
chunkFileNames: (config.chunkFileNames as string | undefined) ?? '[name]-[hash].js',
compact,
dir: getDir(config, file),
dynamicImportFunction: getDynamicImportFunction(config, inputOptions),
entryFileNames: getEntryFileNames(config, unsetOptions),
esModule: (config.esModule as boolean | undefined) ?? true,
exports: getExports(config, unsetOptions),
extend: (config.extend as boolean | undefined) || false,
externalLiveBindings: (config.externalLiveBindings as boolean | undefined) ?? true,
file,
footer: getAddon(config, 'footer'),
format,
freeze: (config.freeze as boolean | undefined) ?? true,
globals: (config.globals as GlobalsOption | undefined) || {},
hoistTransitiveImports: (config.hoistTransitiveImports as boolean | undefined) ?? true,
indent: getIndent(config, compact),
inlineDynamicImports,
interop: getInterop(config, inputOptions),
intro: getAddon(config, 'intro'),
manualChunks: getManualChunks(config, inlineDynamicImports, preserveModules, inputOptions),
minifyInternalExports: getMinifyInternalExports(config, format, compact),
name: config.name as string | undefined,
namespaceToStringTag: (config.namespaceToStringTag as boolean | undefined) || false,
noConflict: (config.noConflict as boolean | undefined) || false,
outro: getAddon(config, 'outro'),
paths: (config.paths as OptionsPaths | undefined) || {},
plugins: ensureArray(config.plugins) as Plugin[],
preferConst: (config.preferConst as boolean | undefined) || false,
preserveModules,
preserveModulesRoot: getPreserveModulesRoot(config),
sanitizeFileName: config.sanitizeFileName as NormalizedOutputOptions['sanitizeFileName'] ?? true,
sourcemap: (config.sourcemap as boolean | 'inline' | 'hidden' | undefined) || false,
sourcemapExcludeSources: (config.sourcemapExcludeSources as boolean | undefined) || false,
sourcemapFile: config.sourcemapFile as string | undefined,
sourcemapPathTransform: config.sourcemapPathTransform as
| SourcemapPathTransformOption
| undefined,
strict: (config.strict as boolean | undefined) ?? true,
systemNullSetters: (config.systemNullSetters as boolean | undefined) || false,
validate: (config.validate as boolean | undefined) || false
};
warnUnknownOptions(config, Object.keys(outputOptions), 'output options', inputOptions.onwarn);
return { options: outputOptions, unsetOptions };
}
const getFile = (
config: GenericConfigObject,
preserveModules: boolean,
inputOptions: NormalizedInputOptions
): string | undefined => {
const file = config.file as string | undefined;
if (typeof file === 'string') {
if (preserveModules) {
return error({
code: 'INVALID_OPTION',
message:
'You must set "output.dir" instead of "output.file" when using the "output.preserveModules" option.'
});
}
if (!Array.isArray(inputOptions.input))
return error({
code: 'INVALID_OPTION',
message: 'You must set "output.dir" instead of "output.file" when providing named inputs.'
});
}
return file;
};
const getFormat = (config: GenericConfigObject): InternalModuleFormat => {
const configFormat = config.format as ModuleFormat | undefined;
switch (configFormat) {
case undefined:
case 'es':
case 'esm':
case 'module':
return 'es';
case 'cjs':
case 'commonjs':
return 'cjs';
case 'system':
case 'systemjs':
return 'system';
case 'amd':
case 'iife':
case 'umd':
return configFormat;
default:
return error({
message: `You must specify "output.format", which can be one of "amd", "cjs", "system", "es", "iife" or "umd".`,
url: `https://rollupjs.org/guide/en/#outputformat`
});
}
};
const getInlineDynamicImports = (
config: GenericConfigObject,
inputOptions: NormalizedInputOptions
): boolean => {
const inlineDynamicImports =
((config.inlineDynamicImports as boolean | undefined) ?? inputOptions.inlineDynamicImports) ||
false;
const { input } = inputOptions;
if (inlineDynamicImports && (Array.isArray(input) ? input : Object.keys(input)).length > 1) {
return error({
code: 'INVALID_OPTION',
message: 'Multiple inputs are not supported for "output.inlineDynamicImports".'
});
}
return inlineDynamicImports;
};
const getPreserveModules = (
config: GenericConfigObject,
inlineDynamicImports: boolean,
inputOptions: NormalizedInputOptions
): boolean => {
const preserveModules =
((config.preserveModules as boolean | undefined) ?? inputOptions.preserveModules) || false;
if (preserveModules) {
if (inlineDynamicImports) {
return error({
code: 'INVALID_OPTION',
message: `The "output.inlineDynamicImports" option is not supported for "output.preserveModules".`
});
}
if (inputOptions.preserveEntrySignatures === false) {
return error({
code: 'INVALID_OPTION',
message:
'Setting "preserveEntrySignatures" to "false" is not supported for "output.preserveModules".'
});
}
}
return preserveModules;
};
const getPreserveModulesRoot = (config: GenericConfigObject): string | undefined => {
const preserveModulesRoot = config.preserveModulesRoot as string | null | undefined;
if (preserveModulesRoot === null || preserveModulesRoot === undefined) {
return undefined;
}
return resolve(preserveModulesRoot);
};
const getAmd = (config: GenericConfigObject): NormalizedOutputOptions['amd'] => {
const collection: { autoId: boolean; basePath: string; define: string; id?: string } = {
autoId: false,
basePath: '',
define: 'define',
...(config.amd as OutputOptions['amd'])
};
if ((collection.autoId || collection.basePath) && collection.id) {
return error({
code: 'INVALID_OPTION',
message:
'"output.amd.autoId"/"output.amd.basePath" and "output.amd.id" cannot be used together.'
});
}
if (collection.basePath && !collection.autoId) {
return error({
code: 'INVALID_OPTION',
message: '"output.amd.basePath" only works with "output.amd.autoId".'
});
}
let normalized: NormalizedOutputOptions['amd'];
if (collection.autoId) {
normalized = {
autoId: true,
basePath: collection.basePath,
define: collection.define
};
} else {
normalized = {
autoId: false,
define: collection.define,
id: collection.id
};
}
return normalized;
};
const getAddon = (config: GenericConfigObject, name: string): (() => string | Promise<string>) => {
const configAddon = config[name] as string | (() => string | Promise<string>);
if (typeof configAddon === 'function') {
return configAddon;
}
return () => configAddon || '';
};
const getDir = (config: GenericConfigObject, file: string | undefined): string | undefined => {
const dir = config.dir as string | undefined;
if (typeof dir === 'string' && typeof file === 'string') {
return error({
code: 'INVALID_OPTION',
message:
'You must set either "output.file" for a single-file build or "output.dir" when generating multiple chunks.'
});
}
return dir;
};
const getDynamicImportFunction = (
config: GenericConfigObject,
inputOptions: NormalizedInputOptions
): string | undefined => {
const configDynamicImportFunction = config.dynamicImportFunction as string | undefined;
if (configDynamicImportFunction) {
warnDeprecation(
`The "output.dynamicImportFunction" option is deprecated. Use the "renderDynamicImport" plugin hook instead.`,
false,
inputOptions
);
}
return configDynamicImportFunction;
};
const getEntryFileNames = (config: GenericConfigObject, unsetOptions: Set<string>): string => {
const configEntryFileNames = config.entryFileNames as string | undefined;
if (configEntryFileNames == null) {
unsetOptions.add('entryFileNames');
}
return configEntryFileNames ?? '[name].js';
};
function getExports(
config: GenericConfigObject,
unsetOptions: Set<string>
): 'default' | 'named' | 'none' | 'auto' {
const configExports = config.exports as string | undefined;
if (configExports == null) {
unsetOptions.add('exports');
} else if (!['default', 'named', 'none', 'auto'].includes(configExports)) {
return error(errInvalidExportOptionValue(configExports));
}
return (configExports as 'default' | 'named' | 'none' | 'auto') || 'auto';
}
const getIndent = (config: GenericConfigObject, compact: boolean): string | true => {
if (compact) {
return '';
}
const configIndent = config.indent as string | boolean | undefined;
return configIndent === false ? '' : configIndent ?? true;
};
const ALLOWED_INTEROP_TYPES = new Set(['auto', 'esModule', 'default', 'defaultOnly', true, false]);
const getInterop = (
config: GenericConfigObject,
inputOptions: NormalizedInputOptions
): GetInterop => {
const configInterop = config.interop as InteropType | GetInterop | undefined;
const validatedInteropTypes = new Set<InteropType>();
const validateInterop = (interop: InteropType): InteropType => {
if (!validatedInteropTypes.has(interop)) {
validatedInteropTypes.add(interop);
if (!ALLOWED_INTEROP_TYPES.has(interop)) {
return error({
code: 'INVALID_OPTION',
message: `The value ${JSON.stringify(
interop
)} is not supported for "output.interop". Use one of ${Array.from(
ALLOWED_INTEROP_TYPES.values(),
value => JSON.stringify(value)
).join(', ')} instead.`,
url: 'https://rollupjs.org/guide/en/#outputinterop'
});
}
if (typeof interop === 'boolean') {
warnDeprecation(
{
message: `The boolean value "${interop}" for the "output.interop" option is deprecated. Use ${
interop ? '"auto"' : '"esModule", "default" or "defaultOnly"'
} instead.`,
url: 'https://rollupjs.org/guide/en/#outputinterop'
},
false,
inputOptions
);
}
}
return interop;
};
if (typeof configInterop === 'function') {
const interopPerId: { [id: string]: InteropType } = Object.create(null);
let defaultInterop: InteropType | null = null;
return id =>
id === null
? defaultInterop || validateInterop((defaultInterop = configInterop(id)))
: id in interopPerId
? interopPerId[id]
: validateInterop((interopPerId[id] = configInterop(id)));
}
return configInterop === undefined ? () => true : () => validateInterop(configInterop);
};
const getManualChunks = (
config: GenericConfigObject,
inlineDynamicImports: boolean,
preserveModules: boolean,
inputOptions: NormalizedInputOptions
): ManualChunksOption => {
const configManualChunks =
(config.manualChunks as ManualChunksOption | undefined) || inputOptions.manualChunks;
if (configManualChunks) {
if (inlineDynamicImports) {
return error({
code: 'INVALID_OPTION',
message:
'The "output.manualChunks" option is not supported for "output.inlineDynamicImports".'
});
}
if (preserveModules) {
return error({
code: 'INVALID_OPTION',
message: 'The "output.manualChunks" option is not supported for "output.preserveModules".'
});
}
}
return configManualChunks || {};
};
const getMinifyInternalExports = (
config: GenericConfigObject,
format: InternalModuleFormat,
compact: boolean
): boolean =>
(config.minifyInternalExports as boolean | undefined) ??
(compact || format === 'es' || format === 'system');