-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-jsx-loader.js
More file actions
26 lines (24 loc) · 815 Bytes
/
node-jsx-loader.js
File metadata and controls
26 lines (24 loc) · 815 Bytes
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
import babel from "@babel/core";
const babelOptions = {
babelrc: false,
ignore: [/\/(build|node_modules)\//],
plugins: [["@babel/plugin-transform-react-jsx", { runtime: "automatic" }]],
};
export async function load(url, context, defaultLoad) {
const result = await defaultLoad(url, context, defaultLoad);
if (result.format === "module") {
const opt = Object.assign({ filename: url }, babelOptions);
const newResult = await babel.transformAsync(result.source, opt);
if (!newResult) {
if (typeof result.source === "string") {
return result;
}
return {
source: Buffer.from(result.source).toString("utf8"),
format: "module",
};
}
return { source: newResult.code, format: "module" };
}
return defaultLoad(url, context, defaultLoad);
}