-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
get-babel-config.ts
248 lines (227 loc) · 8.99 KB
/
get-babel-config.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
import {IBabelConfigItem} from "../../plugin/i-babel-options";
import {isBabelPluginTransformRuntime, isBabelPresetEnv, isYearlyBabelPreset} from "../path/path-util";
import {
BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES,
BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES,
BABEL_MINIFY_PLUGIN_NAMES,
BABEL_MINIFY_PRESET_NAMES,
FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS,
FORCED_BABEL_PRESET_ENV_OPTIONS,
FORCED_BABEL_YEARLY_PRESET_OPTIONS
} from "../../constant/constant";
// @ts-ignore
import {createConfigItem, loadOptions, loadPartialConfig} from "@babel/core";
import {GetBabelConfigOptions} from "./get-babel-config-options";
import {GetBabelConfigResult} from "./get-babel-config-result";
import {findBabelConfig} from "./find-babel-config";
// tslint:disable:no-any
/**
* Combines the given two sets of presets
* @param {IBabelConfigItem[]} userItems
* @param {IBabelConfigItem[]} defaultItems
* @param {object[]} [forcedItems]
* @param {boolean} [useMinifyOptions]
* @returns {{}[]}
*/
function combineConfigItems(
userItems: IBabelConfigItem[],
defaultItems: IBabelConfigItem[] = [],
forcedItems: IBabelConfigItem[] = [],
useMinifyOptions: boolean = false
): {}[] {
const namesInUserItems = new Set(userItems.map(item => item.file.resolved));
const namesInForcedItems = new Set(forcedItems.map(item => item.file.resolved));
const userItemsHasYearlyPreset = [...namesInUserItems].some(isYearlyBabelPreset);
return (
[
// Only use those default items that doesn't appear within the forced items or the user-provided items.
// If the options contains a yearly preset such as "preset-es2015", filter out preset-env from the default items if it is given
...defaultItems.filter(
item =>
!namesInUserItems.has(item.file.resolved) &&
!namesInForcedItems.has(item.file.resolved) &&
(!userItemsHasYearlyPreset || !isBabelPresetEnv(item.file.resolved))
),
// Only use those user items that doesn't appear within the forced items
...userItems.filter(item => !namesInForcedItems.has(item.file.resolved)),
// Apply the forced items at all times
...forcedItems
]
// Filter out those options that do not apply depending on whether or not to apply minification
.filter(configItem =>
useMinifyOptions ? configItemIsAllowedDuringMinification(configItem) : configItemIsAllowedDuringNoMinification(configItem)
)
);
}
/**
* Returns true if the given configItem is related to minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsMinificationRelated({file: {resolved}}: IBabelConfigItem): boolean {
return BABEL_MINIFY_PRESET_NAMES.some(preset => resolved.includes(preset)) || BABEL_MINIFY_PLUGIN_NAMES.some(plugin => resolved.includes(plugin));
}
/**
* Returns true if the given configItem is allowed during minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsAllowedDuringMinification({file: {resolved}}: IBabelConfigItem): boolean {
return (
BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES.every(preset => !resolved.includes(preset)) &&
BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES.every(plugin => !resolved.includes(plugin))
);
}
/**
* Returns true if the given configItem is allowed when not applying minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsAllowedDuringNoMinification({file: {resolved}}: IBabelConfigItem): boolean {
return (
BABEL_MINIFY_PRESET_NAMES.every(preset => !resolved.includes(preset)) && BABEL_MINIFY_PLUGIN_NAMES.every(plugin => !resolved.includes(plugin))
);
}
/**
* Gets a Babel Config based on the given options
* @param {GetBabelConfigOptions} options
* @returns {GetBabelConfigResult}
*/
export function getBabelConfig({
babelConfig,
cwd,
forcedOptions = {},
defaultOptions = {},
browserslist,
rollupInputOptions
}: GetBabelConfigOptions): GetBabelConfigResult {
const resolvedConfig = findBabelConfig({cwd, babelConfig});
// Load a partial Babel config based on the input options
const partialConfig = loadPartialConfig(
resolvedConfig != null && resolvedConfig.kind === "dict"
? // If the given babelConfig is an object of input options, use that as the basis for the full config
{...resolvedConfig.options, cwd, root: cwd, configFile: false, babelrc: false}
: // Load the path to a babel config provided to the plugin if any, otherwise try to resolve it
{
cwd,
root: cwd,
...(resolvedConfig != null ? {configFile: resolvedConfig.path} : {babelrc: true})
}
);
const {options, config} = partialConfig;
const {presets: forcedPresets, plugins: forcedPlugins, ...otherForcedOptions} = forcedOptions;
const {presets: defaultPresets, plugins: defaultPlugins, ...otherDefaultOptions} = defaultOptions;
const configFileOption = {configFile: false, babelrc: false};
// If users have provided presets of their own, ensure that they are using respecting the forced options
if (options.presets != null) {
options.presets = options.presets.map((preset: any) => {
// Apply the forced @babel/preset-env options here
if (isBabelPresetEnv(preset.file.resolved)) {
return createConfigItem(
[
preset.file.request,
{
...(preset.options == null ? {} : preset.options),
...FORCED_BABEL_PRESET_ENV_OPTIONS,
// If targets have already been provided by the user options, accept them.
// Otherwise, apply the browserslist as the preset-env target
...(preset.options != null && preset.options.targets != null
? {}
: {
targets: {
browsers: browserslist
}
})
}
],
{type: "preset", dirname: cwd}
);
}
// Apply the forced @babel/preset-es[2015|2016|2017...] options here
else if (isYearlyBabelPreset(preset.file.resolved)) {
return createConfigItem(
[
preset.file.request,
{
...(preset.options == null ? {} : preset.options),
...FORCED_BABEL_YEARLY_PRESET_OPTIONS
}
],
{type: "preset", dirname: cwd}
);
}
return preset;
});
}
// If users have provided plugins of their own, ensure that they are using respecting the forced options
if (options.plugins != null) {
options.plugins = options.plugins.map((plugin: any) => {
// Apply the forced @babel/preset-env options here
if (isBabelPluginTransformRuntime(plugin.file.resolved)) {
return createConfigItem(
[
plugin.file.request,
{
...(plugin.options == null ? {} : plugin.options),
...FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS(rollupInputOptions)
}
],
{type: "plugin", dirname: cwd}
);
}
return plugin;
});
}
// Combine the partial config with the default and forced options
const combined = {
...otherDefaultOptions,
...options,
...otherForcedOptions,
presets: combineConfigItems(
options.presets,
defaultPresets == null ? undefined : loadPartialConfig({presets: defaultPresets, ...configFileOption}).options.presets,
forcedPresets == null ? undefined : loadPartialConfig({presets: forcedPresets, ...configFileOption}).options.presets,
false
),
plugins: combineConfigItems(
options.plugins,
defaultPlugins == null ? undefined : loadPartialConfig({plugins: defaultPlugins, ...configFileOption}).options.plugins,
forcedPlugins == null ? undefined : loadPartialConfig({plugins: forcedPlugins, ...configFileOption}).options.plugins,
false
)
};
// sourceMap is an alias for 'sourceMaps'. If the user provided it, make sure it is undefined. Otherwise, Babel will fail during validation
if ("sourceMap" in combined) {
delete combined.sourceMap;
}
// Combine the partial config with the default and forced options for the minifyConfig
const minifyCombined = {
...combined,
presets: combineConfigItems(
options.presets,
defaultPresets == null ? undefined : loadPartialConfig({presets: defaultPresets, ...configFileOption}).options.presets,
forcedPresets == null ? undefined : loadPartialConfig({presets: forcedPresets, ...configFileOption}).options.presets,
true
),
plugins: combineConfigItems(
options.plugins,
defaultPlugins == null ? undefined : loadPartialConfig({plugins: defaultPlugins, ...configFileOption}).options.plugins,
forcedPlugins == null ? undefined : loadPartialConfig({plugins: forcedPlugins, ...configFileOption}).options.plugins,
true
)
};
const finalConfigFileOption = config == null ? configFileOption : {configFile: config};
const finalMinifyConfigFileOption = config == null ? configFileOption : {configFile: `${config}.minify`};
// Normalize the options
return {
config: filename => loadOptions({...combined, filename, ...finalConfigFileOption}),
// Only return the minify config if it includes at least one plugin or preset
minifyConfig:
minifyCombined.plugins.length < 1 && minifyCombined.presets.length < 1
? undefined
: filename => loadOptions({...minifyCombined, filename, ...finalMinifyConfigFileOption}),
hasMinifyOptions:
[...minifyCombined.plugins.filter(configItemIsMinificationRelated), ...minifyCombined.presets.filter(configItemIsMinificationRelated)].length >
0
};
}