-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for utilizing npm package in ESM environments.
- Loading branch information
Showing
7 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const recast = require('recast'); | ||
|
||
//list of external packages that require '.js' extensions | ||
const packagesRequiringJsExtension = [ | ||
'protobufjs/minimal', | ||
//add other package paths as needed | ||
]; | ||
|
||
function shouldAppendJsExtension(source) { | ||
//check if the path has an extension already | ||
if (path.extname(source)) { | ||
return false; | ||
} | ||
|
||
//check if the path is relative | ||
if (source.startsWith('./') || source.startsWith('../')) { | ||
return true; | ||
} | ||
|
||
//check if the path is in the whitelist of external packages | ||
return packagesRequiringJsExtension.some(pkg => source === pkg || source.startsWith(`${pkg}/`)); | ||
} | ||
|
||
|
||
function processFile(filePath) { | ||
const code = fs.readFileSync(filePath, 'utf8'); | ||
const ast = recast.parse(code, { | ||
parser: require('recast/parsers/babel'), // Use Babel parser | ||
}); | ||
|
||
let modified = false; | ||
|
||
recast.types.visit(ast, { | ||
visitImportDeclaration(pathNode) { | ||
const source = pathNode.node.source.value; | ||
if (shouldAppendJsExtension(source)) { | ||
pathNode.node.source.value = `${source}.js`; | ||
modified = true; | ||
} | ||
this.traverse(pathNode); | ||
}, | ||
visitExportNamedDeclaration(pathNode) { | ||
if (pathNode.node.source && pathNode.node.source.value) { | ||
const source = pathNode.node.source.value; | ||
if (shouldAppendJsExtension(source)) { | ||
pathNode.node.source.value = `${source}.js`; | ||
modified = true; | ||
} | ||
} | ||
this.traverse(pathNode); | ||
}, | ||
visitExportAllDeclaration(pathNode) { | ||
if (pathNode.node.source && pathNode.node.source.value) { | ||
const source = pathNode.node.source.value; | ||
if (shouldAppendJsExtension(source)) { | ||
pathNode.node.source.value = `${source}.js`; | ||
modified = true; | ||
} | ||
} | ||
this.traverse(pathNode); | ||
}, | ||
}); | ||
|
||
if (modified) { | ||
const output = recast.print(ast).code; | ||
fs.writeFileSync(filePath, output, 'utf8'); | ||
console.log(`Updated import/export paths in: ${filePath}`); | ||
} | ||
} | ||
|
||
|
||
function traverseDir(dir) { | ||
fs.readdirSync(dir).forEach((file) => { | ||
const fullPath = path.join(dir, file); | ||
const stat = fs.statSync(fullPath); | ||
|
||
if (stat.isDirectory()) { | ||
traverseDir(fullPath); | ||
} else if (stat.isFile() && path.extname(fullPath) === '.js') { | ||
processFile(fullPath); | ||
} | ||
}); | ||
} | ||
|
||
function main() { | ||
const esmDir = path.resolve(__dirname, './dist/esm'); | ||
|
||
if (!fs.existsSync(esmDir)) { | ||
console.error(`Directory not found: ${esmDir}`); | ||
process.exit(1); | ||
} | ||
|
||
traverseDir(esmDir); | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"esModuleInterop": true, | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"outDir": "dist/cjs", | ||
"moduleResolution": "Node", | ||
"declaration": true, | ||
"declarationDir": "dist/types" | ||
}, | ||
"files": ["src/index.ts"], | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "**/*.test.js"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"target": "ES2022", | ||
"moduleResolution": "Node", | ||
"esModuleInterop": true, | ||
"outDir": "dist/esm", | ||
"declaration": true, | ||
"declarationDir": "dist/types" | ||
}, | ||
"files": ["src/index.ts"], | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "**/*.test.js"] | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"declaration": true | ||
|