Skip to content

Instantly share code, notes, and snippets.

@tomfa
Created September 20, 2023 20:57
Show Gist options
  • Save tomfa/a7b0b4897b6319b64bd8e1cb1ba44115 to your computer and use it in GitHub Desktop.
Save tomfa/a7b0b4897b6319b64bd8e1cb1ba44115 to your computer and use it in GitHub Desktop.
Codemod convert commonJS to esm import path style
/*
jscodeshift transform: adds the '.js' extension
to all import declarations with relative specifiers:
From './file' to './file.js', and
from '../file' to '../file.js'.
*/
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
return j(fileInfo.source)
.find(j.ImportDeclaration)
.forEach((path) => {
const { source } = path.node;
if (source.value.match(/^\.{1,2}\/(?!\S+(?:\.m?js|\.png|\.myfileextention))/)) {
source.value += '.js';
}
})
.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment