Skip to content

Don't attempt to compile modules whose source is null. Skip wasm/json. Resolves #7 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2022
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
16 changes: 14 additions & 2 deletions lib/node-loader-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ function isBabelConfigFile(filename) {
);
}

// The formats that babel can compile
// It cannot compile wasm/json
const supportedModuleFormats = ["module", "commonjs"];

export async function load(url, context, defaultLoad) {
if (useLoader(url)) {
const { source } = await defaultLoad(url, context, defaultLoad);
const { source, format } = await defaultLoad(url, context, defaultLoad);

// NodeJS' implementation of defaultLoad returns a source of `null` for CommonJS modules.
// So we just skip compilation when it's commonjs until a future day when NodeJS (might) support that.
// Also, we skip compilation of wasm and json modules by babel, since babel isn't needed or possible
// in those situations
if (!source || (format && !supportedModuleFormats.includes(format))) {
return { source, format };
}

const filename = urlModule.fileURLToPath(url);
// Babel config files can themselves be ES modules,
Expand All @@ -32,7 +44,7 @@ export async function load(url, context, defaultLoad) {
}

const options = await loadOptionsAsync({
sourceType: "module",
sourceType: format || "module",
root: process.cwd(),
rootMode: "root",
filename: filename,
Expand Down
15 changes: 15 additions & 0 deletions test/commonjs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from "assert";

describe("babel compilation of commonjs files", () => {
it(`can transform commonjs modules`, async () => {
const commonJSModule = await import("./fixtures/commonjs/main.cjs");

assert.deepEqual(commonJSModule.default, {
red: "#ff0000",
blue: "#0000ff",
});

assert.deepEqual(commonJSModule.red, "#ff0000");
assert.deepEqual(commonJSModule.blue, "#0000ff");
});
});
2 changes: 2 additions & 0 deletions test/fixtures/commonjs/main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.red = "#ff0000";
exports.blue = "#0000ff";
1 change: 1 addition & 0 deletions test/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Mocha from "mocha";
const mocha = new Mocha();

mocha.addFile("./test/basic.test.js");
mocha.addFile("./test/commonjs.test.js");

mocha.loadFilesAsync().then(() => {
mocha.run();
Expand Down