Created
September 20, 2023 20:57
-
-
Save tomfa/a7b0b4897b6319b64bd8e1cb1ba44115 to your computer and use it in GitHub Desktop.
Codemod convert commonJS to esm import path style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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