-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
base: production
Are you sure you want to change the base?
Conversation
}, | ||
"scripts": { | ||
"build": "rm -rf lib && yarn build:ts", | ||
"build": "rm -rf lib && yarn build:ts && echo '{\"type\":\"module\"}' > lib/esm/package.json", |
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.
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 ;)
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.
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.
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.
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.
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.
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]}}); |
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.
it's preferable to add extension explicitly no ?
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.
#!/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?
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.
The target is to generate the code for esm without an additional plugin. So I prefer to not register a loader.
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.
Maybe trying to have the same code base to generate cjs and esm code is an error 🤔
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.
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";
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.
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[]
);
}
Hello @vangie 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:
|
d0c3b48
to
92001c5
Compare
OK, I have amended the git msg and add the missing yarn.lock file |
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 |
Information
Todos