-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins.ts
87 lines (80 loc) · 2.31 KB
/
plugins.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
import type * as babel from '@babel/core';
import babelPluginNumericSeparator from '@babel/plugin-proposal-numeric-separator';
import babelPluginProposalDynamicImport from '@babel/plugin-proposal-dynamic-import';
import babelPluginProposalExportDefaultFrom from '@babel/plugin-proposal-export-default-from';
import babelPluginProposalExportNamespaceFrom from '@babel/plugin-proposal-export-namespace-from';
import babelPluginSyntaxClassProperties from '@babel/plugin-syntax-class-properties';
import babelPluginTransformModulesSystemJS from '@babel/plugin-transform-modules-systemjs';
import babelPluginTransformReactJsx from '@babel/plugin-transform-react-jsx';
import babelPluginTransformTypescript from '@babel/plugin-transform-typescript';
import './types.d.ts';
const SYNTAX_PLUGINS: babel.ParserOptions['plugins'] = [
'asyncGenerators',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'dynamicImport',
'importMeta',
'nullishCoalescingOperator',
'numericSeparator',
'optionalCatchBinding',
'optionalChaining',
'objectRestSpread',
'topLevelAwait',
];
const DEFAULT_PLUGINS = [
babelPluginTransformModulesSystemJS,
babelPluginProposalDynamicImport,
babelPluginProposalExportDefaultFrom,
babelPluginProposalExportNamespaceFrom,
babelPluginSyntaxClassProperties,
babelPluginNumericSeparator,
];
const TS_PLUGINS: Array<babel.PluginItem> = [
[
babelPluginTransformTypescript,
{
onlyRemoveTypeImports: true,
},
],
...DEFAULT_PLUGINS,
];
const TSX_PLUGINS: Array<babel.PluginItem> = [
[
babelPluginTransformTypescript,
{
isTSX: true,
onlyRemoveTypeImports: true,
},
],
[
babelPluginTransformReactJsx,
{
runtime: 'automatic',
},
],
...DEFAULT_PLUGINS,
];
const JSX_PLUGINS: Array<babel.PluginItem> = [
[
babelPluginTransformReactJsx,
{
runtime: 'automatic',
},
],
...DEFAULT_PLUGINS,
];
const PLUGINS_BY_EXTENSION: Record<string, Array<babel.PluginItem>> = {
'.ts': TS_PLUGINS,
'.tsx': TSX_PLUGINS,
'.jsx': JSX_PLUGINS,
};
export function getBabelPlugins(extension: string | null): {
syntax: babel.ParserOptions['plugins'];
transform: Array<babel.PluginItem>;
} {
return {
syntax: SYNTAX_PLUGINS,
transform: (extension && PLUGINS_BY_EXTENSION[extension]) || DEFAULT_PLUGINS,
};
}