-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.ts
66 lines (55 loc) · 1.36 KB
/
index.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
import babel, { TransformOptions } from '@babel/core';
import { Loader } from 'esbuild';
import { createFilter, FilterPattern, Plugin } from 'vite';
import { esbuildPluginBabel } from './esbuildBabel';
import { Filter, testFilter } from './filter'
export interface BabelPluginOptions {
apply?: 'serve' | 'build';
babelConfig?: TransformOptions;
filter?: Filter;
include?: FilterPattern
exclude?: FilterPattern
loader?: Loader | ((path: string) => Loader);
}
const DEFAULT_FILTER = /\.jsx?$/;
const babelPlugin = ({
babelConfig = {},
filter = DEFAULT_FILTER,
include,
exclude,
apply,
loader
}: BabelPluginOptions = {}): Plugin => {
const customFilter = createFilter(include, exclude)
return {
name: 'babel-plugin',
apply,
enforce: 'pre',
config() {
return {
optimizeDeps: {
esbuildOptions: {
plugins: [
esbuildPluginBabel({
config: { ...babelConfig },
customFilter,
filter,
loader,
}),
],
},
},
};
},
transform(code, id) {
const shouldTransform = customFilter(id) && testFilter(filter, id);
if (!shouldTransform) return;
return babel
.transformAsync(code, { filename: id, ...babelConfig })
.then((result) => ({ code: result?.code ?? '', map: result?.map }));
},
};
};
export default babelPlugin;
export * from './esbuildBabel';
export type { Filter }