-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbabel.ts
419 lines (379 loc) · 14.8 KB
/
babel.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import {ConfigItem, PluginObj, TransformOptions} from "@babel/core";
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,
BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_1,
BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_2,
BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_1,
BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_2,
BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_3,
BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_4
} from "../constant/constant.js";
import {BabelConfigHook, TranspilationPhase, TranspilerOptions, TypescriptPluginOptions} from "../plugin/typescript-plugin-options.js";
import {Babel, BabelConfig} from "../type/babel.js";
import {isDefined} from "../util/is-defined/is-defined.js";
import {
isBabelPluginTransformRuntime,
isBabelPresetEnv,
isBabelPresetTypescript,
isYearlyBabelPreset,
removeSearchPathFromFilename,
resolveModule,
somePathsAreRelated
} from "../util/path/path-util.js";
import {SourceMap} from "rollup";
import MagicString from "magic-string";
import {matchAll} from "@wessberg/stringutil";
export interface GetForcedBabelOptionsOptions {
cwd: string;
}
/**
* Retrieves the Babel config options that will be forced
*/
export function getForcedBabelOptions({cwd}: GetForcedBabelOptionsOptions): TransformOptions {
return {
// Always use the cwd provided to the plugin
cwd,
// Always produce sourcemaps. Rollup will be the decider of what to do with them.
sourceMaps: true,
// Never let Babel be the decider of which files to ignore. Rather let Rollup decide that
ignore: undefined,
// Never let Babel be the decider of which files to include. Rather let Rollup decide that
only: undefined,
// Always parse things as modules. Rollup will then decide what to do based on the output format
sourceType: "module",
plugins: [
// Needed to make babel understand dynamic imports
resolveModule("@babel/plugin-syntax-dynamic-import")
]
};
}
export interface GetDefaultBabelOptionsOptions {
browserslist?: string[];
transpilerOptions: TranspilerOptions;
}
/**
* Retrieves the Babel config options that will be used by default. If the user provides the same keys/presets/plugins, *they*
* will take precedence
*/
export function getDefaultBabelOptions({browserslist, transpilerOptions}: GetDefaultBabelOptionsOptions): TransformOptions {
const includePresetEnv = browserslist != null;
const includePresetTypescript = transpilerOptions.typescriptSyntax === "babel";
return {
presets: [
// Use @babel/preset-typescript when Babel is responsible for transforming TypeScript specific syntax
...(!includePresetEnv
? []
: [
[
resolveModule("@babel/preset-env"),
{
...FORCED_BABEL_PRESET_ENV_OPTIONS,
// Loose breaks things such as spreading an iterable that isn't an array
loose: false,
spec: false,
debug: false,
ignoreBrowserslistConfig: false,
shippedProposals: true,
targets: {
browsers: browserslist
}
}
]
]),
// Use @babel/preset-env when a Browserslist has been given
...(!includePresetTypescript
? []
: [
[
resolveModule("@babel/preset-typescript"),
{
// There are no default options here
}
]
])
],
plugins: [
// If preset-env will be included, shipped proposals will be included already. If not, apply them here
...(includePresetEnv
? []
: [
resolveModule("@babel/plugin-proposal-object-rest-spread"),
resolveModule("@babel/plugin-proposal-async-generator-functions"),
resolveModule("@babel/plugin-proposal-optional-catch-binding"),
resolveModule("@babel/plugin-proposal-unicode-property-regex"),
resolveModule("@babel/plugin-proposal-json-strings")
]),
// Force the use of helpers (e.g. the runtime). But *don't* apply polyfills.
[
resolveModule("@babel/plugin-transform-runtime"),
{
...FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS,
corejs: false
}
]
]
};
}
export interface GetBabelConfigOptions {
babel: typeof Babel;
cwd: string;
hook: BabelConfigHook | undefined;
babelConfig: TypescriptPluginOptions["babelConfig"];
browserslist: string[] | undefined;
forcedOptions: TransformOptions | undefined;
defaultOptions: TransformOptions | undefined;
phase: TranspilationPhase;
}
export interface PluginObject<S = unknown> extends PluginObj<S> {
key: string;
}
export interface FullConfig {
cwd: string;
root: string;
filename: string | undefined;
babelrc: false;
configFile: false;
envName: string;
sourceMaps: boolean;
sourceType: TransformOptions["sourceType"];
passPerPreset: boolean;
plugins: PluginObject[];
presets: PluginObject[];
}
export interface GetBabelConfigResult {
config: FullConfig | undefined;
}
export type BabelConfigFactory = (filename: string, inTypescriptStep?: boolean) => Promise<GetBabelConfigResult>;
/**
* Gets a Babel Config based on the given options
*/
export function getBabelConfig({babel, babelConfig, cwd, forcedOptions = {}, defaultOptions = {}, browserslist, phase, hook}: GetBabelConfigOptions): BabelConfigFactory {
return async (filename: string, inTypescriptStep = false) => {
// Load a partial Babel config based on the input options
const partialConfig = await babel.loadPartialConfigAsync(
// If babel options are provided directly
isBabelConfig(babelConfig) && Object.keys(babelConfig).length > 0
? // If the given babelConfig is an object of input options, use that as the basis for the full config
{cwd, root: cwd, ...babelConfig}
: // Load the path to a babel config provided to the plugin if any, otherwise try to resolve it
{
cwd,
root: cwd,
filename: removeSearchPathFromFilename(filename),
...(babelConfig == null || typeof babelConfig !== "string" ? {} : {configFile: babelConfig})
}
);
if (partialConfig == null) {
return {
config: undefined
};
}
const {options} = partialConfig;
const {presets: forcedPresets, plugins: forcedPlugins, ...otherForcedOptions} = forcedOptions;
const {presets: defaultPresets, plugins: defaultPlugins, ...otherDefaultOptions} = defaultOptions;
const configFileOption: TransformOptions = {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 as ConfigItem[]).map(preset => {
if (preset.file == null) return preset;
// Apply the forced @babel/preset-env options here
if (isBabelPresetEnv(preset.file.resolved)) {
return babel.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 as {targets?: unknown}).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 babel.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 as ConfigItem[]).map((plugin: ConfigItem) => {
if (plugin.file == null) return plugin;
if (isBabelPluginTransformRuntime(plugin.file.resolved)) {
return babel.createConfigItem(
[
plugin.file.request,
{
...(plugin.options == null ? {} : plugin.options),
...FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS
}
],
{type: "plugin", dirname: cwd}
);
}
return plugin;
});
}
// Combine the partial config with the default and forced options
const combined: TransformOptions = {
...otherDefaultOptions,
...options,
...otherForcedOptions,
presets: combineConfigItems(
(options.presets ?? []) as ConfigItem[],
defaultPresets == null ? undefined : (babel.loadPartialConfig({presets: defaultPresets, ...configFileOption})?.options.presets as ConfigItem[] | null) ?? undefined,
forcedPresets == null
? undefined
: (babel.loadPartialConfig({presets: forcedPresets, ...configFileOption})?.options.presets as ConfigItem[] | null | undefined) ?? undefined,
phase === "chunk",
inTypescriptStep
),
plugins: combineConfigItems(
(options.plugins ?? []) as ConfigItem[],
defaultPlugins == null ? undefined : (babel.loadPartialConfig({plugins: defaultPlugins, ...configFileOption})?.options.plugins as ConfigItem[] | null) ?? undefined,
forcedPlugins == null
? undefined
: (babel.loadPartialConfig({plugins: forcedPlugins, ...configFileOption})?.options.plugins as ConfigItem[] | null | undefined) ?? undefined,
phase === "chunk",
inTypescriptStep
)
};
// 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 as {sourceMap?: unknown})) {
delete (combined as {sourceMap?: unknown}).sourceMap;
}
const combinedOptionsAfterHook = hook != null ? hook(combined, partialConfig.config ?? partialConfig.babelrc ?? undefined, phase) : combined;
const loadedOptions = (babel.loadOptions({...combinedOptionsAfterHook, filename, ...configFileOption}) as FullConfig | null) ?? undefined;
// Only return a config in the chunk phase if it includes at least one plugin or preset that is relevant to it
if (phase === "chunk") {
const hasRelevantConfigItems =
loadedOptions != null &&
[
...((combined.plugins as ConfigItem[]) ?? []).filter(configItemIsRelevantForChunkPhase),
...((combined.presets as ConfigItem[]) ?? []).filter(configItemIsRelevantForChunkPhase)
].length > 0;
return {
config: hasRelevantConfigItems ? loadedOptions : undefined
};
} else {
return {
config: loadedOptions
};
}
};
}
function isBabelConfig(babelConfig?: TypescriptPluginOptions["babelConfig"]): babelConfig is Partial<BabelConfig> {
return babelConfig != null && typeof babelConfig !== "string";
}
/**
* Combines the given two sets of presets
*/
function combineConfigItems(
userItems: ConfigItem[],
defaultItems: ConfigItem[] = [],
forcedItems: ConfigItem[] = [],
inChunkPhase: boolean,
inTypescriptStep: boolean
): ConfigItem[] {
const namesInUserItems = new Set(userItems.map(item => item.file?.resolved).filter(isDefined));
const namesInForcedItems = new Set(forcedItems.map(item => item.file?.resolved).filter(isDefined));
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 =>
item.file == null ||
(!somePathsAreRelated(namesInUserItems, item.file.resolved) &&
!somePathsAreRelated(namesInForcedItems, item.file.resolved) &&
(!userItemsHasYearlyPreset || !isBabelPresetEnv(item.file.resolved)))
),
// Only use those user items that doesn't appear within the forced items
...userItems.filter(item => item.file == null || !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 => (inChunkPhase ? configItemIsAllowedDuringChunkPhase(configItem) : configItemIsAllowedDuringFilePhase(configItem)))
// Only allow @babel/preset-typescript if we're actually in the initial TypeScript phase
.filter(configItem => configItem.file?.resolved == null || !isBabelPresetTypescript(configItem.file?.resolved) || inTypescriptStep)
);
}
/**
* Returns true if the given configItem is related to minification
*/
function configItemIsRelevantForChunkPhase(configItem: ConfigItem): boolean {
return (
BABEL_MINIFY_PRESET_NAMES.some(preset => configItem.file?.resolved.includes(preset)) || BABEL_MINIFY_PLUGIN_NAMES.some(plugin => configItem.file?.resolved.includes(plugin))
);
}
/**
* Returns true if the given configItem is allowed per chunk transformation
*/
function configItemIsAllowedDuringChunkPhase(configItem: ConfigItem): boolean {
return (
BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES.every(preset => configItem.file == null || !configItem.file.resolved.includes(preset)) &&
BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES.every(plugin => configItem.file == null || !configItem.file.resolved.includes(plugin))
);
}
/**
* Returns true if the given configItem is allowed per file transformations
*/
function configItemIsAllowedDuringFilePhase(configItem: ConfigItem): boolean {
return (
BABEL_MINIFY_PRESET_NAMES.every(preset => configItem.file == null || !configItem.file.resolved.includes(preset)) &&
BABEL_MINIFY_PLUGIN_NAMES.every(plugin => configItem.file == null || !configItem.file.resolved.includes(plugin))
);
}
export function replaceBabelHelpers(code: string, filename: string, target: "cjs" | "esm"): {code: string; map: SourceMap} | undefined {
const matches =
target === "cjs"
? [...matchAll(code, BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_1), ...matchAll(code, BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_2)]
: [
...matchAll(code, BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_1),
...matchAll(code, BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_2),
...matchAll(code, BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_3),
...matchAll(code, BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_4)
];
if (matches.length < 1) return undefined;
const magicString = new MagicString(code, {filename, indentExclusionRanges: []});
for (const match of matches) {
const start = match.index + match[1].length;
const end = match.index + match[1].length + match[2].length;
if (target === "cjs") {
magicString.overwrite(start, end, match[2].replace(`/esm/`, `/`));
} else {
magicString.overwrite(start, end, match[2].replace(/\/helpers\/(?!esm)/g, `/helpers/esm/`));
}
}
return {
code: magicString.toString(),
map: magicString.generateMap({hires: true, source: filename, includeContent: true})
};
}