Skip to content
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

feat(prisma): enable prisma generator support for ESM #2700

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

vangie
Copy link
Contributor

@vangie vangie commented May 8, 2024

Information

Type Breaking change
Feature No

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

packages/orm/prisma/package.json Outdated Show resolved Hide resolved
import { argv } from "node:process";

// @ts-ignore
register('extensionless', import.meta.url, {data: {argv1: argv[1]}});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's preferable to add extension explicitly no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#!/usr/bin/env node --import extensionless/register

import "./cli/generator";

The modification above simplifies the originally committed code. However, it requires installing the 'extensionless' module in my own project. I'm not sure how to improve this further. Any suggestions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The target is to generate the code for esm without an additional plugin. So I prefer to not register a loader.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe trying to have the same code base to generate cjs and esm code is an error 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using top level yarn build import statement contains .js extension. but js files emit by prisma generator is missing the js extension.

lib/esm/.schema/index.js

export * from "./interfaces";
export * from "./models";
export * from "./services/PrismaService";
export * from "./repositories";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I add a .js suffix to all these import statements? such as following ts file

tsed/packages/orm/prisma/src/generator/utils/generateIndex.ts

export function generateIndex(project: Project, baseDirPath: string, hasEnum: boolean) {
  const indexFile = path.resolve(baseDirPath, "index.ts");

  project.createSourceFile(indexFile, undefined, {overwrite: true}).addExportDeclarations(
    [
      {
        moduleSpecifier: "./interfaces"
      },
      hasEnum && {
        moduleSpecifier: "./enums"
      },
      {
        moduleSpecifier: "./models"
      },
      {
        moduleSpecifier: "./services/PrismaService"
      },
      {
        moduleSpecifier: "./repositories"
      }
    ].filter(Boolean) as any[]
  );
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vangie done. I fixed all path. You can run locally the appropriate npm scripts "generate:postgres:esm"

If you see something wrong tell me :)

@Romakita
Copy link
Collaborator

Romakita commented May 8, 2024

Hello @vangie
Thanks a lot for this PR. You've missed to commit the yarn.lock file ;)

Also, the repo follow a angular commit convention and use semantic release. Can you remove emoji from your commit message please (isn't supported by semantic release).

The commit message must be:

feat(prisma): enable prisma generator support for ESM

@vangie vangie force-pushed the vangie/prisma-generator-esm branch from d0c3b48 to 92001c5 Compare May 9, 2024 02:00
@vangie
Copy link
Contributor Author

vangie commented May 9, 2024

Hello @vangie Thanks a lot for this PR. You've missed to commit the yarn.lock file ;)

Also, the repo follow a angular commit convention and use semantic release. Can you remove emoji from your commit message please (isn't supported by semantic release).

The commit message must be:

feat(prisma): enable prisma generator support for ESM

OK, I have amended the git msg and add the missing yarn.lock file

@vangie vangie changed the title ⭐ feat: enable prisma generator support for ESM feat(prisma): enable prisma generator support for ESM May 9, 2024
@Tomdrouv1
Copy link

@vangie @Romakita any update for this PR, it would be amazing to add it for the next update, I'm working on a project that I want to turn into full esm support but I can't because of schemas not generated in esm.

Thanks for the work !

@Romakita
Copy link
Collaborator

I think publish a code that support commonjs / esm for this plugin is complicated. I started the v8 branch (ESM only). Maybe it's more appropriate to add this PR to the v8 roadmap.

The branch is almost ready. There are small issue related to GQL and prisma module.

Another solution is to duplicate the code inside the tsed/prisma module (one for cjs and one esm) and use exports field to use the appropriate code convention.

See you

@Romakita Romakita force-pushed the vangie/prisma-generator-esm branch 2 times, most recently from 16aca2f to 65476b2 Compare May 31, 2024 08:55
@Romakita
Copy link
Collaborator

@vangie I pushed some commit to unlock your dev ;)

Now you can generate the code for ESM and CJS.

  • I removed extensionless. I prefer to manage extension each time it's necessary in our code.
  • I removed the condition to not generate decorator when there is a recursive import, I don't now why you trying to fix that because, you have to generate decorators in any case !

See you

@Romakita Romakita force-pushed the vangie/prisma-generator-esm branch from dbd41ba to b35a99f Compare June 1, 2024 17:05
@Romakita Romakita force-pushed the vangie/prisma-generator-esm branch from b35a99f to 61442f0 Compare June 1, 2024 17:10
@Romakita Romakita force-pushed the vangie/prisma-generator-esm branch from 2533ce2 to 2552db1 Compare June 1, 2024 17:42
@Romakita
Copy link
Collaborator

Romakita commented Jun 1, 2024

@vangie @Tomdrouv1 The PR seems to be ready ;) can you review the changes ?

@Romakita Romakita merged commit 2d6a23c into tsedio:production Jun 4, 2024
15 checks passed
@Romakita
Copy link
Collaborator

Romakita commented Jun 4, 2024

🎉 This PR is included in version 7.71.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants