Created
May 3, 2020 20:30
-
-
Save eemeli/e72e63a7c2c20699fb3a641cccfd4cb1 to your computer and use it in GitHub Desktop.
A jscodeshift codemod for replacing import & export paths with their full paths, including extensions. Effectively fixes imports for use as ES modules in Node.js.
This file contains hidden or 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
/** | |
* A jscodeshift codemod for replacing import & export paths with their full | |
* paths, including extensions. Effectively fixes imports for use as ES modules | |
* in Node.js. | |
* | |
* Copyright Eemeli Aro <[email protected]> | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted. | |
*/ | |
const { dirname, relative, resolve, sep } = require('path') | |
const useFullFilePath = dir => path => { | |
const { source } = path.value | |
if (source && source.value.startsWith('.')) { | |
const absPath = require.resolve(resolve(dir, source.value)) | |
const relPath = relative(dir, absPath) | |
source.value = relPath.startsWith('.') ? relPath : `.${sep}${relPath}` | |
} | |
} | |
module.exports = function useFullImportFilePaths(fileInfo, api) { | |
const j = api.jscodeshift | |
const root = j(fileInfo.source) | |
const dir = dirname(resolve(fileInfo.path)) | |
const fix = useFullFilePath(dir) | |
for (const d of [ | |
j.ImportDeclaration, | |
j.ExportAllDeclaration, | |
j.ExportNamedDeclaration | |
]) | |
root.find(d).forEach(fix) | |
return root.toSource({ quote: 'single' }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment