Skip to content

Commit c58d887

Browse files
authored
fix(runtime): Refactor logical client exports to be ESM/CJS agnostic (#2078)
1 parent 309013b commit c58d887

File tree

1 file changed

+10
-11
lines changed
  • packages/schema/src/plugins/enhancer/enhance

1 file changed

+10
-11
lines changed

packages/schema/src/plugins/enhancer/enhance/index.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,18 @@ export class EnhancerGenerator {
113113

114114
if (this.needsLogicalClient) {
115115
prismaTypesFixed = true;
116-
resultPrismaTypeImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`;
116+
resultPrismaTypeImport = LOGICAL_CLIENT_GENERATION_PATH;
117117
const result = await this.generateLogicalPrisma();
118118
dmmf = result.dmmf;
119119
}
120120

121121
// reexport PrismaClient types (original or fixed)
122-
const modelsDts = this.project.createSourceFile(
123-
path.join(this.outDir, 'models.d.ts'),
122+
const modelsTs = this.project.createSourceFile(
123+
path.join(this.outDir, 'models.ts'),
124124
`export * from '${resultPrismaTypeImport}';`,
125125
{ overwrite: true }
126126
);
127-
await modelsDts.save();
128-
129-
// reexport values from the original PrismaClient (enums, etc.)
130-
fs.writeFileSync(path.join(this.outDir, 'models.js'), `module.exports = require('${prismaImport}');`);
127+
this.saveSourceFile(modelsTs);
131128

132129
const authDecl = getAuthDecl(getDataModelAndTypeDefs(this.model));
133130
const authTypes = authDecl ? generateAuthType(this.model, authDecl) : '';
@@ -176,7 +173,7 @@ ${
176173
return {
177174
dmmf,
178175
newPrismaClientDtsPath: prismaTypesFixed
179-
? path.resolve(this.outDir, LOGICAL_CLIENT_GENERATION_PATH, 'index-fixed.d.ts')
176+
? path.resolve(this.outDir, LOGICAL_CLIENT_GENERATION_PATH, 'index.d.ts')
180177
: undefined,
181178
};
182179
}
@@ -456,7 +453,7 @@ export type Enhanced<Client> =
456453
}
457454

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

@@ -471,8 +468,7 @@ export type Enhanced<Client> =
471468
}
472469
});
473470

474-
// transform index.d.ts and save it into a new file (better perf than in-line editing)
475-
471+
// transform index.d.ts and write it into a new file (better perf than in-line editing)
476472
const sfNew = project.createSourceFile(path.join(prismaClientDir, 'index-fixed.d.ts'), undefined, {
477473
overwrite: true,
478474
});
@@ -482,6 +478,9 @@ export type Enhanced<Client> =
482478
this.generateExtraTypes(sfNew);
483479

484480
sfNew.formatText();
481+
482+
// Save the transformed file over the original
483+
await sfNew.move(sf.getFilePath(), { overwrite: true });
485484
await sfNew.save();
486485
}
487486

0 commit comments

Comments
 (0)