forked from chaskiq/chaskiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-esbuild.mjs
54 lines (42 loc) · 1.36 KB
/
babel-esbuild.mjs
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
import babel from '@babel/core';
import fs from 'fs';
import path from 'path';
const pluginBabel = (options = {}) => ({
name: 'babel',
setup(build, { transform } = {}) {
const { filter = /.*/, namespace = '', config = {} } = options;
const transformContents = ({ args, contents }) => {
const babelOptions = babel.loadOptions({
...config,
filename: args.path,
caller: {
name: 'esbuild-plugin-babel',
supportsStaticESM: true
}
});
if (!babelOptions) return { contents };
if (babelOptions.sourceMaps) {
const filename = path.relative(process.cwd(), args.path);
babelOptions.sourceFileName = filename;
}
return new Promise((resolve, reject) => {
babel.transform(contents, babelOptions, (error, result) => {
if(error) {
console.log(error)
reject(error)
} else {
resolve({ contents: result.code });
}
});
});
};
if (transform) return transformContents(transform);
build.onLoad({ filter, namespace }, async args => {
if (args.path.includes('node_modules') && !args.path.includes('reactour') ) return null;
if(args.path.includes(".json") || args.path.includes(".png") || args.path.includes(".css") ) return null;
const contents = await fs.promises.readFile(args.path, 'utf8');
return transformContents({ args, contents });
});
}
});
export default pluginBabel;