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

Open
wants to merge 1 commit into
base: production
Choose a base branch
from

Conversation

vangie
Copy link
Contributor

@vangie vangie commented May 8, 2024

Information

Type Breaking change
Feature No

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

},
"scripts": {
"build": "rm -rf lib && yarn build:ts",
"build": "rm -rf lib && yarn build:ts && echo '{\"type\":\"module\"}' > lib/esm/package.json",
Copy link
Collaborator

Choose a reason for hiding this comment

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

you don't need that. the build tool generate the correct package.json with the expected type module when you run the yarn build on the root package.json.
Please remove this change ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remove the 'lib/esm/package.json' file, it will print the following error when I run npx prisma generate

> npx prisma generate

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Error: Generator "tsed-prisma-esm" failed:

(node:78280) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/duwan/Workspaces/babelcloud/tsed/packages/orm/prisma/lib/esm/cli/generator.js:1
import { generatorHandler } from "@prisma/generator-helper";
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at cjsLoader (node:internal/modules/esm/translators:356:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:305:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
Node.js v20.11.1

 ELIFECYCLE  Command failed with exit code 1.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But you don't have to run npx prisma migrate at this step. It's only when the package is published you need that.

Again, if you run yarn build from the root package.json, you see a dist directory. Inside the dist, you will have the tsed prisma package with the generated code (esm/cjs version).
Also, the build add extension to all imported files in the esm code. You don't need extensionless module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I run npx prisma migrate in my owner project which generated by tsed. I have tried top level yarn build, many files generated in dist/prisma folder has .js extension in import statement. but it still print following error if I remove the package.json file in lib/esm.

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Error: Generator "tsed-prisma-esm" failed:

(node:71478) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/duwan/Workspaces/babelcloud/tsed/dist/prisma/lib/esm/cli/generator.js:1
import { generatorHandler } from "@prisma/generator-helper";
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at cjsLoader (node:internal/modules/esm/translators:356:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:305:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
Node.js v20.11.1

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[]
  );
}

@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

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

Successfully merging this pull request may close these issues.

None yet

3 participants