Skip to content

Commit

Permalink
Add extension fix to ESM files
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov committed Oct 14, 2023
1 parent 8bec972 commit 8e1c6d3
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions drizzle-orm/scripts/fix-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ function fixImportPath(importPath: string, file: string, ext: string) {

const cjsFiles = await glob('dist.new/**/*.{cjs,d.cts}');

for (const file of cjsFiles) {
await Promise.all(cjsFiles.map(async (file) => {
const code = parse(await fs.readFile(file, 'utf8'), { parser });

visit(code, {
visitImportDeclaration(path) {
path.value.source.value = fixImportPath(path.value.source.value, file, '.cjs');
return false;
this.traverse(path);
},
visitExportAllDeclaration(path) {
path.value.source.value = fixImportPath(path.value.source.value, file, '.cjs');
return false;
this.traverse(path);
},
visitExportNamedDeclaration(path) {
if (path.value.source) {
path.value.source.value = fixImportPath(path.value.source.value, file, '.cjs');
}
return false;
this.traverse(path);
},
visitCallExpression(path) {
if (path.value.callee.type === 'Identifier' && path.value.callee.name === 'require') {
Expand All @@ -57,35 +57,33 @@ for (const file of cjsFiles) {
});

await fs.writeFile(file, print(code).code);
}
}));

const esmFiles = await glob('dist.new/**/*.{js,d.ts}');

for (const file of esmFiles) {
await Promise.all(esmFiles.map(async (file) => {
const code = parse(await fs.readFile(file, 'utf8'), { parser });

// console.log(code);

visit(code, {
visitImportDeclaration(path) {
path.value.source.value = resolvePathAlias(path.value.source.value, file);
path.value.source.value = fixImportPath(path.value.source.value, file, '.js');
this.traverse(path);
},
visitExportAllDeclaration(path) {
path.value.source.value = resolvePathAlias(path.value.source.value, file);
path.value.source.value = fixImportPath(path.value.source.value, file, '.js');
this.traverse(path);
},
visitExportNamedDeclaration(path) {
if (path.value.source) {
path.value.source.value = resolvePathAlias(path.value.source.value, file);
path.value.source.value = fixImportPath(path.value.source.value, file, '.js');
}
this.traverse(path);
},
visitTSImportType(path) {
path.value.argument.value = resolvePathAlias(path.value.argument.value, file);
path.value.argument.value = fixImportPath(path.value.argument.value, file, '.js');
this.traverse(path);
},
});

await fs.writeFile(file, print(code).code);
}
}));

0 comments on commit 8e1c6d3

Please sign in to comment.