diff --git a/.babelrc b/.babelrc index 98a72d625d..6d38204aa4 100644 --- a/.babelrc +++ b/.babelrc @@ -19,7 +19,8 @@ "presets": [["@babel/preset-env", { "modules": "commonjs" }]] }, "mjs": { - "presets": [["@babel/preset-env", { "modules": false }]] + "presets": [["@babel/preset-env", { "modules": false }]], + "plugins": ['./resources/add-extension-to-import-paths'] } } }, diff --git a/resources/add-extension-to-import-paths.js b/resources/add-extension-to-import-paths.js new file mode 100644 index 0000000000..cfa6ae62d6 --- /dev/null +++ b/resources/add-extension-to-import-paths.js @@ -0,0 +1,43 @@ +// @noflow + +'use strict'; + +/** + * Adds extension to all paths imported inside MJS files + * + * Transforms: + * + * import { foo } from './bar'; + * export { foo } from './bar'; + * + * to: + * + * import { foo } from './bar.mjs'; + * export { foo } from './bar.mjs'; + * + */ +module.exports = function addExtensionToImportPaths(context) { + const { types } = context; + + return { + visitor: { + ImportDeclaration: replaceImportPath, + ExportNamedDeclaration: replaceImportPath, + }, + }; + + function replaceImportPath(path) { + // bail if the declaration doesn't have a source, e.g. "export { foo };" + if (!path.node.source) { + return; + } + + const source = path.node.source.value; + if (source.startsWith('./') || source.startsWith('../')) { + if (!source.endsWith('.mjs')) { + const newSourceNode = types.stringLiteral(source + '.mjs'); + path.get('source').replaceWith(newSourceNode); + } + } + } +};