Skip to content

Commit 6710a10

Browse files
committed
fix(runtime): Add --esm flag to generate action, to export ".zenstack/models" in ESM format
1 parent d2a4ec0 commit 6710a10

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

packages/schema/src/cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export function createProgram() {
125125
.addOption(new Option('--without-plugins <plugins...>', 'exclude specific plugins'))
126126
.addOption(new Option('--no-default-plugins', 'do not run default plugins'))
127127
.addOption(new Option('--no-compile', 'do not compile the output of core plugins'))
128+
.addOption(new Option('--esm', 'generate output for ESM instead of CommonJS'))
128129
.addOption(noVersionCheckOption)
129130
.addOption(noDependencyCheckOption)
130131
.addOption(offlineOption)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,12 @@ export class EnhancerGenerator {
128128
await modelsDts.save();
129129

130130
// reexport values from the original PrismaClient (enums, etc.)
131-
fs.writeFileSync(path.join(this.outDir, 'models.js'), `module.exports = require('${prismaImport}');`);
131+
const modelsBody =
132+
this.options.esm ?? false
133+
? `export * from '${prismaImport}';`
134+
: `module.exports = require('${prismaImport}');`;
135+
136+
fs.writeFileSync(path.join(this.outDir, 'models.js'), modelsBody);
132137

133138
const authDecl = getAuthDecl(getDataModelAndTypeDefs(this.model));
134139
const authTypes = authDecl ? generateAuthType(this.model, authDecl) : '';
@@ -338,6 +343,7 @@ export type Enhanced<Client> =
338343
overrideClientGenerationPath: prismaClientOutDir,
339344
mode: 'logical',
340345
customAttributesAsComments: true,
346+
isEsm: this.options.isEsm,
341347
});
342348

343349
// reverse direction of shortNameMap and store for future lookup

packages/sdk/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export type PluginOptions = {
4242
* @private
4343
*/
4444
shortNameMap?: Map<string, string>;
45+
/**
46+
* Is the plugin generating into an ESM project?
47+
*/
48+
esm?: boolean;
4549
} & PluginDeclaredOptions;
4650

4751
/**

0 commit comments

Comments
 (0)