/
options.ts
156 lines (144 loc) · 4.15 KB
/
options.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
import type {
InputOptions,
InputPluginOption,
NormalizedGeneratedCodeOptions,
NormalizedOutputOptions,
NormalizedTreeshakingOptions,
OutputOptions,
OutputPlugin,
OutputPluginOption,
Plugin,
WarningHandler
} from '../../rollup/types';
import { errInvalidOption, error, errUnknownOption } from '../error';
import { printQuotedStringList } from '../printStringList';
export interface GenericConfigObject {
[key: string]: unknown;
}
export const defaultOnWarn: WarningHandler = warning => console.warn(warning.message || warning);
export function warnUnknownOptions(
passedOptions: GenericConfigObject,
validOptions: readonly string[],
optionType: string,
warn: WarningHandler,
ignoredKeys = /$./
): void {
const validOptionSet = new Set(validOptions);
const unknownOptions = Object.keys(passedOptions).filter(
key => !(validOptionSet.has(key) || ignoredKeys.test(key))
);
if (unknownOptions.length > 0) {
warn(errUnknownOption(optionType, unknownOptions, [...validOptionSet].sort()));
}
}
type ObjectValue<Base> = Base extends Record<string, any> ? Base : never;
export const treeshakePresets: {
[key in NonNullable<
ObjectValue<InputOptions['treeshake']>['preset']
>]: NormalizedTreeshakingOptions;
} = {
recommended: {
annotations: true,
correctVarValueBeforeDeclaration: false,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: false
},
safest: {
annotations: true,
correctVarValueBeforeDeclaration: true,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: true
},
smallest: {
annotations: true,
correctVarValueBeforeDeclaration: false,
moduleSideEffects: () => false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
unknownGlobalSideEffects: false
}
};
export const generatedCodePresets: {
[key in NonNullable<
ObjectValue<OutputOptions['generatedCode']>['preset']
>]: NormalizedOutputOptions['generatedCode'];
} = {
es2015: {
arrowFunctions: true,
constBindings: true,
objectShorthand: true,
reservedNamesAsProps: true,
symbols: true
},
es5: {
arrowFunctions: false,
constBindings: false,
objectShorthand: false,
reservedNamesAsProps: true,
symbols: false
}
};
type ObjectOptionWithPresets =
| Partial<NormalizedTreeshakingOptions>
| Partial<NormalizedGeneratedCodeOptions>;
export const objectifyOption = (value: unknown): Record<string, unknown> =>
value && typeof value === 'object' ? (value as Record<string, unknown>) : {};
export const objectifyOptionWithPresets =
<T extends ObjectOptionWithPresets>(
presets: Record<string, T>,
optionName: string,
additionalValues: string
) =>
(value: unknown): Record<string, unknown> => {
if (typeof value === 'string') {
const preset = presets[value];
if (preset) {
return preset;
}
error(
errInvalidOption(
optionName,
getHashFromObjectOption(optionName),
`valid values are ${additionalValues}${printQuotedStringList(
Object.keys(presets)
)}. You can also supply an object for more fine-grained control`,
value
)
);
}
return objectifyOption(value);
};
export const getOptionWithPreset = <T extends ObjectOptionWithPresets>(
value: unknown,
presets: Record<string, T>,
optionName: string,
additionalValues: string
): Record<string, unknown> => {
const presetName: string | undefined = (value as any)?.preset;
if (presetName) {
const preset = presets[presetName];
if (preset) {
return { ...preset, ...(value as Record<string, unknown>) };
} else {
error(
errInvalidOption(
`${optionName}.preset`,
getHashFromObjectOption(optionName),
`valid values are ${printQuotedStringList(Object.keys(presets))}`,
presetName
)
);
}
}
return objectifyOptionWithPresets(presets, optionName, additionalValues)(value);
};
const getHashFromObjectOption = (optionName: string): string =>
optionName.split('.').join('').toLowerCase();
export const normalizePluginOption: {
(plugins: InputPluginOption): Plugin[];
(plugins: OutputPluginOption): OutputPlugin[];
} = (plugins: any) => [plugins].flat(Infinity).filter(Boolean);