Skip to content

Commit

Permalink
Attempt to fix node.js integration by tweaking imports with babel
Browse files Browse the repository at this point in the history
Fixes: matrix-org#4287
Signed-off-by: Johannes Marbach <[email protected]>
  • Loading branch information
Johennes committed Aug 22, 2024
1 parent ee94e93 commit dd72086
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require("fs");
const path = require("path");

module.exports = {
sourceMaps: true,
presets: [
Expand All @@ -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);
},
},
};
}])
],
};

0 comments on commit dd72086

Please sign in to comment.