-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbabel.config.js
More file actions
65 lines (60 loc) · 1.85 KB
/
babel.config.js
File metadata and controls
65 lines (60 loc) · 1.85 KB
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
function transformImportMetaForJest({ types: t }) {
return {
name: "transform-import-meta-for-jest",
visitor: {
MemberExpression(path) {
if (!t.isMetaProperty(path.node.object)) return;
const meta = path.node.object;
if (meta.meta.name !== "import" || meta.property.name !== "meta") return;
if (!t.isIdentifier(path.node.property)) return;
if (path.node.property.name === "env") {
path.replaceWith(t.memberExpression(t.identifier("process"), t.identifier("env")));
return;
}
if (path.node.property.name === "url") {
// `import.meta.url` -> `require("url").pathToFileURL(__filename).toString()`
const requireUrl = t.callExpression(t.identifier("require"), [t.stringLiteral("url")]);
const pathToFileURL = t.memberExpression(requireUrl, t.identifier("pathToFileURL"));
const fileUrl = t.callExpression(pathToFileURL, [t.identifier("__filename")]);
const toString = t.memberExpression(fileUrl, t.identifier("toString"));
path.replaceWith(t.callExpression(toString, []));
}
},
},
};
}
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
modules: "commonjs",
},
],
[
"@babel/preset-typescript",
{
allowDeclareFields: true,
},
],
[
"@babel/preset-react",
{
runtime: "automatic",
},
],
],
env: {
test: {
// Jest runs with CommonJS output (`modules: "commonjs"` above) which cannot
// evaluate `import.meta.*` references. Vite-only code paths use:
// - `import.meta.env.*`
// - `import.meta.url`
// This plugin rewrites those to safe Node equivalents for Jest.
plugins: [transformImportMetaForJest],
},
},
};