-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-babel-transformer.js
43 lines (40 loc) · 1.23 KB
/
get-babel-transformer.js
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
const { default: BT } = require("babel-jest");
const { viteMetaTransformPlugin } = require("./transform-vite-meta");
/**
* @param {GetOptions} options
*/
function getBabelTransformer({ inputSourceMap, rootPath, base }) {
const aliasPrefix = base.endsWith("/") ? base.slice(0, -1) : base;
const babelTransformer = BT.createTransformer({
inputSourceMap,
configFile: false,
babelrc: false,
plugins: [
[
require.resolve("babel-plugin-module-resolver"),
{
alias: {
[`${aliasPrefix}/@vite/client`]: "vite/dist/client/client.mjs",
[`${aliasPrefix}/@vite/env`]: "vite/dist/client/env.mjs",
[`${aliasPrefix}`]: rootPath,
},
},
],
[require.resolve("@babel/plugin-transform-modules-commonjs"), {}],
[require.resolve("babel-plugin-transform-import-meta"), {}],
viteMetaTransformPlugin,
],
});
return babelTransformer;
}
module.exports = {
getBabelTransformer,
};
/**
* @typedef {import('@babel/core').TransformOptions} TransformOptions
* @typedef {import('@jest/transform').SyncTransformer} SyncTransformer
* @typedef {{
* inputSourceMap: TransformOptions['inputSourceMap'],
* rootPath: string,
* }} GetOptions
*/