Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/node-loader-babel.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import babel from "@babel/core";
import path from "path";
import url from "url";

const { OptionManager, transform } = babel;
const { loadOptionsAsync, transformAsync } = babel;

function isBabelConfigFile(filename) {
const basename = path.basename(filename);
return (
basename === ".babelrc.js" ||
basename === ".babelrc.mjs" ||
basename === "babel.config.js" ||
basename === "babel.config.mjs"
);
}

export async function transformSource(source, context, defaultGetSource) {
const options = new OptionManager().init({
const filename = url.fileURLToPath(context.url);
// Babel config files can themselves be ES modules,
// but we cannot transform those since doing so would cause an infinite loop.
if (isBabelConfigFile(filename)) {
return { source };
}

const options = await loadOptionsAsync({
sourceType: "module",
root: process.cwd(),
rootMode: "root",
filename: url.fileURLToPath(context.url),
filename: filename,
configFile: true,
});

const transformedSource = transform(source, options).code;
const transformed = await transformAsync(source, options);

return {
source: transformedSource,
source: transformed.code,
};
}
11 changes: 10 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import assert from "assert";

describe(`basic babel usage`, () => {
it(`can transform jsx and run it in node`, async () => {
const basicModule = await import("./fixtures/basic.js");
const basicModule = await import("./fixtures/basic/basic.js");
assert.deepEqual(basicModule.default, {
type: "button",
props: null,
children: undefined,
});
});

it(`supports ES module babel config files`, async () => {
const mjsConfig = await import("./fixtures/mjs-config/main.js");
assert.deepEqual(mjsConfig.default, {
type: "div",
props: null,
children: undefined,
});
});
});
3 changes: 0 additions & 3 deletions test/fixtures/basic.js

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/basic/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from "../../jsx-impl.js";

export default <button />;
11 changes: 11 additions & 0 deletions test/fixtures/mjs-config/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
plugins: [
[
"@babel/plugin-transform-react-jsx",
{
runtime: "classic",
pragma: "h",
},
],
],
};
3 changes: 3 additions & 0 deletions test/fixtures/mjs-config/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from "../../jsx-impl.js";

export default <div />;
Loading