-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
fix(runtime): Refactor logical client exports to be ESM/CJS agnostic #2078
base: dev
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughThe changes update the logical client generation in the enhancer module. In the file Changes
Sequence Diagram(s)sequenceDiagram
participant EG as EnhancerGenerator
participant FS as FileSystem
EG->>FS: Import Prisma type from "/index" (was "/index-fixed")
EG->>FS: Create models.ts file (was models.d.ts)
EG->>FS: Save Prisma client types to "index.d.ts" (overwriting previous file)
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (5)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/schema/src/cli/index.ts (1)
128-128
: Good addition of the --esm flagThe new command line option provides a clear way for users to specify ESM output format when needed, which directly addresses the issue for projects with
"type": "module"
in their package.json.One potential enhancement to consider (not required): could the ESM setting be automatically inferred from the package.json
"type": "module"
setting? This would reduce the need for manual flag specification, though as mentioned in the PR description, this might be challenging in monorepo setups.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/schema/src/cli/index.ts
(1 hunks)packages/schema/src/plugins/enhancer/enhance/index.ts
(2 hunks)packages/sdk/src/types.ts
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/schema/src/plugins/enhancer/enhance/index.ts (1)
packages/schema/build/bundle.js (1)
fs
(4-4)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: OSSAR-Scan
🔇 Additional comments (2)
packages/schema/src/plugins/enhancer/enhance/index.ts (1)
131-136
: Great implementation of conditional ESM/CommonJS exports.The code elegantly handles both module formats based on the
esm
option, which correctly addresses the issue with projects using"type": "module"
in their package.json.packages/sdk/src/types.ts (1)
45-48
: Well-implemented ESM support in plugin optionsThe addition of the
esm
property toPluginOptions
is a clean approach to indicate module format preference. The property has a clear name, purpose, and is properly marked as optional to maintain backward compatibility.
@@ -338,6 +343,7 @@ export type Enhanced<Client> = | |||
overrideClientGenerationPath: prismaClientOutDir, | |||
mode: 'logical', | |||
customAttributesAsComments: true, | |||
isEsm: this.options.isEsm, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Property name inconsistency.
There's a mismatch between property names: isEsm
is used here but esm
is used in the PluginOptions type and elsewhere in this file. This could cause confusion or bugs.
Apply this change to maintain naming consistency:
- isEsm: this.options.isEsm,
+ isEsm: this.options.esm,
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
isEsm: this.options.isEsm, | |
isEsm: this.options.esm, |
Hi @diesal11 , I feel the entire approach is quite hacky now ... The original idea was to only reexport types from logical prisma client at "@zenstackhq/runtime/models", which was why previously only models.d.ts was generated. Then we found to import enums there you need value exports as well. For better safety, the "models.js" file reexports from the original prisma client, just to avoid accidentally importing duplicated copies of values from different prisma locations ... I feel maybe it's an overkill. If we simply generate a "models.ts" to just reexport everything from logical prisma, the esm/cjs problem would just go away as it'll be compiled according to user's project settings. What do you think? |
6710a10
to
0fab3e6
Compare
Yes I agree this isn't an ideal approach. I've had another look and I believe what you're suggesting is possible, it would also require saving Initially I wasn't confident touching the index-fixed.d.ts file as I wasn't sure about the reason this was saved separately. But I've quickly tested this solution in my side project by manually applying the changes and it appears to work without issue. It looks like most of the value exports from prisma client are just enum like objects that map to strings. So it looks like it should be fine. Thanks for the feedback! I've updated the PR to implement this, let me chase down test failures :) |
0fab3e6
to
017d66e
Compare
When
zenstack generate
is run in a project with"type": "module"
in it's package.json, startup fails with the following error:Sidenote: Should this simply be inferred by reading package.json? I wasn't certain that would catch some cases in a monorepo.