-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
normalizeOutputOptions.ts
196 lines (183 loc) · 6.69 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
import {
GlobalsOption,
InternalModuleFormat,
ModuleFormat,
NormalizedInputOptions,
NormalizedOutputOptions,
OptionsPaths,
OutputOptions
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
import { errInvalidExportOptionValue, error, warnDeprecation } from '../error';
import { GenericConfigObject, warnUnknownOptions } from './options';
export function normalizeOutputOptions(
config: GenericConfigObject,
inputOptions: NormalizedInputOptions
): { 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<string>();
const compact = (config.compact as boolean | undefined) || false;
const file = getFile(config, inputOptions);
const format = getFormat(config);
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),
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),
interop: (config.interop as boolean | undefined) ?? true,
intro: getAddon(config, 'intro'),
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,
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
| ((sourcePath: string) => string)
| undefined,
strict: (config.strict as boolean | undefined) ?? true,
systemNullSetters: (config.systemNullSetters as boolean | undefined) || false
};
warnUnknownOptions(config, Object.keys(outputOptions), 'output options', inputOptions.onwarn);
return { options: outputOptions, unsetOptions };
}
const getFile = (
config: GenericConfigObject,
inputOptions: NormalizedInputOptions
): string | undefined => {
const file = config.file as string | undefined;
if (typeof file === 'string') {
if (inputOptions.preserveModules) {
return error({
code: 'INVALID_OPTION',
message:
'You must set "output.dir" instead of "output.file" when using the "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/#output-format`
});
}
};
const getAmd = (
config: GenericConfigObject
): {
define: string;
id?: string;
} => ({
define: 'define',
...(config.amd as {
define?: string;
id?: string;
})
});
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): 'default' | 'named' | 'none' | 'auto' {
const configExports = config.exports as string | undefined;
if (configExports && !['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 getMinifyInternalExports = (
config: GenericConfigObject,
format: InternalModuleFormat,
compact: boolean
): boolean =>
(config.minifyInternalExports as boolean | undefined) ??
(compact || format === 'es' || format === 'system');