-
Notifications
You must be signed in to change notification settings - Fork 18
/
AllSyntaxPlugin.ts
46 lines (42 loc) · 1.34 KB
/
AllSyntaxPlugin.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
import * as Babel from '@babel/core';
import { ParserPlugin } from '@babel/parser';
import { extname } from 'path';
import { PluginObj } from './BabelPluginTypes';
import { TypeScriptExtensions } from './extensions';
const BASIC_PLUGINS: Array<ParserPlugin | [ParserPlugin, object]> = [
'jsx',
'asyncGenerators',
'classProperties',
'doExpressions',
'functionBind',
'functionSent',
'objectRestSpread',
'dynamicImport',
['decorators', { decoratorsBeforeExport: true }]
];
function pluginsForFilename(
filename: string
): Array<ParserPlugin | [ParserPlugin, object]> {
let isTypeScript = TypeScriptExtensions.has(extname(filename));
return isTypeScript
? [...BASIC_PLUGINS, 'typescript']
: [...BASIC_PLUGINS, 'flow'];
}
export default function(babel: typeof Babel): PluginObj {
return {
manipulateOptions(
opts: Babel.TransformOptions,
parserOpts: Babel.ParserOptions
): void {
parserOpts.sourceType = 'module';
parserOpts.allowImportExportEverywhere = true;
parserOpts.allowReturnOutsideFunction = true;
parserOpts.allowSuperOutsideMethod = true;
// Cast this because @babel/types typings don't allow plugin options.
parserOpts.plugins = [
...(parserOpts.plugins || []),
...pluginsForFilename(opts.filename as string)
] as Array<ParserPlugin>;
}
};
}