Skip to content

fix(runtime): Refactor logical client exports to be ESM/CJS agnostic #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,18 @@ export class EnhancerGenerator {

if (this.needsLogicalClient) {
prismaTypesFixed = true;
resultPrismaTypeImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`;
resultPrismaTypeImport = LOGICAL_CLIENT_GENERATION_PATH;
const result = await this.generateLogicalPrisma();
dmmf = result.dmmf;
}

// reexport PrismaClient types (original or fixed)
const modelsDts = this.project.createSourceFile(
path.join(this.outDir, 'models.d.ts'),
const modelsTs = this.project.createSourceFile(
path.join(this.outDir, 'models.ts'),
`export * from '${resultPrismaTypeImport}';`,
{ overwrite: true }
);
await modelsDts.save();

// reexport values from the original PrismaClient (enums, etc.)
fs.writeFileSync(path.join(this.outDir, 'models.js'), `module.exports = require('${prismaImport}');`);
this.saveSourceFile(modelsTs);

const authDecl = getAuthDecl(getDataModelAndTypeDefs(this.model));
const authTypes = authDecl ? generateAuthType(this.model, authDecl) : '';
Expand Down Expand Up @@ -177,7 +174,7 @@ ${
return {
dmmf,
newPrismaClientDtsPath: prismaTypesFixed
? path.resolve(this.outDir, LOGICAL_CLIENT_GENERATION_PATH, 'index-fixed.d.ts')
? path.resolve(this.outDir, LOGICAL_CLIENT_GENERATION_PATH, 'index.d.ts')
: undefined,
};
}
Expand Down Expand Up @@ -457,7 +454,7 @@ export type Enhanced<Client> =
}

private async processClientTypes(prismaClientDir: string) {
// make necessary updates to the generated `index.d.ts` file and save it as `index-fixed.d.ts`
// make necessary updates to the generated `index.d.ts` file and overwrite it
const project = new Project();
const sf = project.addSourceFileAtPath(path.join(prismaClientDir, 'index.d.ts'));

Expand All @@ -472,8 +469,7 @@ export type Enhanced<Client> =
}
});

// transform index.d.ts and save it into a new file (better perf than in-line editing)

// transform index.d.ts and write it into a new file (better perf than in-line editing)
const sfNew = project.createSourceFile(path.join(prismaClientDir, 'index-fixed.d.ts'), undefined, {
overwrite: true,
});
Expand All @@ -483,6 +479,9 @@ export type Enhanced<Client> =
this.generateExtraTypes(sfNew);

sfNew.formatText();

// Save the transformed file over the original
await sfNew.move(sf.getFilePath(), { overwrite: true });
await sfNew.save();
}

Expand Down
Loading