diff --git a/babel.config.cjs b/babel.config.cjs index 57d061ffcd6..005bab1a954 100644 --- a/babel.config.cjs +++ b/babel.config.cjs @@ -1,3 +1,6 @@ +const fs = require("fs"); +const path = require("path"); + module.exports = { sourceMaps: true, presets: [ @@ -22,5 +25,35 @@ module.exports = { "@babel/plugin-transform-object-rest-spread", "@babel/plugin-syntax-dynamic-import", "@babel/plugin-transform-runtime", + // Fix up imports & exports so that Node.js doesn't choke on them + ...(process.env.NODE_ENV === "test" ? [] : [function appendJsExtensionToImports() { + function fixImportOrExport(target, state) { + if (!target.node.source) { + return; + } + + const source = target.node.source.value; + + if (source && source.startsWith(".") && !source.endsWith(".js")) { + const fullPath = path.join(path.dirname(state.file.opts.filename), source); + if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) { + target.node.source.value += "/index.js"; + } else { + target.node.source.value += ".js"; + } + } + } + + return { + visitor: { + ImportDeclaration(target, state) { + fixImportOrExport(target, state); + }, + ExportDeclaration(target, state) { + fixImportOrExport(target, state); + }, + }, + }; + }]) ], };