From 3fc0cbf79adc859d083f997be6f081fb17a9a091 Mon Sep 17 00:00:00 2001 From: joeldenning Date: Tue, 4 Jan 2022 15:59:37 -0700 Subject: [PATCH 1/2] Don't attempt to compile modules whose source is null. Skip wasm/json --- lib/node-loader-babel.js | 16 ++++++++++++++-- test/commonjs.test.js | 16 ++++++++++++++++ test/fixtures/commonjs/main.cjs | 2 ++ test/run-tests.js | 1 + 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/commonjs.test.js create mode 100644 test/fixtures/commonjs/main.cjs diff --git a/lib/node-loader-babel.js b/lib/node-loader-babel.js index e41a59b..88c7984 100644 --- a/lib/node-loader-babel.js +++ b/lib/node-loader-babel.js @@ -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, @@ -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, diff --git a/test/commonjs.test.js b/test/commonjs.test.js new file mode 100644 index 0000000..6b64f61 --- /dev/null +++ b/test/commonjs.test.js @@ -0,0 +1,16 @@ +import assert from "assert"; + +describe('babel compilation of commonjs files', () => { + it(`can transform commonjs modules`, async () => { + const commonJSModule = await import('./fixtures/commonjs/main.cjs'); + console.log(commonJSModule) + + assert.deepEqual(commonJSModule.default, { + red: '#ff0000', + blue: '#0000ff' + }) + + assert.deepEqual(commonJSModule.red, '#ff0000') + assert.deepEqual(commonJSModule.blue, '#0000ff') + }) +}) \ No newline at end of file diff --git a/test/fixtures/commonjs/main.cjs b/test/fixtures/commonjs/main.cjs new file mode 100644 index 0000000..c41a0a9 --- /dev/null +++ b/test/fixtures/commonjs/main.cjs @@ -0,0 +1,2 @@ +exports.red = '#ff0000'; +exports.blue = '#0000ff' \ No newline at end of file diff --git a/test/run-tests.js b/test/run-tests.js index fd0a200..645297f 100644 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -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(); From 43b05fbc15dd270dfe695332cd7e58e1c9f35928 Mon Sep 17 00:00:00 2001 From: joeldenning Date: Tue, 4 Jan 2022 16:02:24 -0700 Subject: [PATCH 2/2] Self review --- lib/node-loader-babel.js | 6 +++--- test/commonjs.test.js | 19 +++++++++---------- test/fixtures/commonjs/main.cjs | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/node-loader-babel.js b/lib/node-loader-babel.js index 88c7984..a90c9ce 100644 --- a/lib/node-loader-babel.js +++ b/lib/node-loader-babel.js @@ -19,18 +19,18 @@ function isBabelConfigFile(filename) { // The formats that babel can compile // It cannot compile wasm/json -const supportedModuleFormats = ['module', 'commonjs'] +const supportedModuleFormats = ["module", "commonjs"]; export async function load(url, context, defaultLoad) { if (useLoader(url)) { const { source, format } = await defaultLoad(url, context, defaultLoad); - // NodeJS implementation of defaultLoad returns a source of `null` for CommonJS modules. + // 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} + return { source, format }; } const filename = urlModule.fileURLToPath(url); diff --git a/test/commonjs.test.js b/test/commonjs.test.js index 6b64f61..2878582 100644 --- a/test/commonjs.test.js +++ b/test/commonjs.test.js @@ -1,16 +1,15 @@ import assert from "assert"; -describe('babel compilation of commonjs files', () => { +describe("babel compilation of commonjs files", () => { it(`can transform commonjs modules`, async () => { - const commonJSModule = await import('./fixtures/commonjs/main.cjs'); - console.log(commonJSModule) + const commonJSModule = await import("./fixtures/commonjs/main.cjs"); assert.deepEqual(commonJSModule.default, { - red: '#ff0000', - blue: '#0000ff' - }) + red: "#ff0000", + blue: "#0000ff", + }); - assert.deepEqual(commonJSModule.red, '#ff0000') - assert.deepEqual(commonJSModule.blue, '#0000ff') - }) -}) \ No newline at end of file + assert.deepEqual(commonJSModule.red, "#ff0000"); + assert.deepEqual(commonJSModule.blue, "#0000ff"); + }); +}); diff --git a/test/fixtures/commonjs/main.cjs b/test/fixtures/commonjs/main.cjs index c41a0a9..ca47018 100644 --- a/test/fixtures/commonjs/main.cjs +++ b/test/fixtures/commonjs/main.cjs @@ -1,2 +1,2 @@ -exports.red = '#ff0000'; -exports.blue = '#0000ff' \ No newline at end of file +exports.red = "#ff0000"; +exports.blue = "#0000ff";