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
ESM support in cli #7159
Comments
All instances of |
Here is how mocha supports both It's important for mocha to attempt As far as I know, Having said that, if you experiment and find out that I'm wrong, please let me know. Perhaps my information is out-of-date. |
@cspotcode It looks good. can typeorm use this fallback in PlatformTools? |
Do you have more context? What is the goal?
…On Tue, Dec 15, 2020, 8:22 AM Deokseong Kim ***@***.***> wrote:
@cspotcode <https://github.com/cspotcode> It looks good. can typeorm use
this fallback in PlatformTools?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7159 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC35OBKWKKVLLBB5XSWKXDSU5PKDANCNFSM4UOP5PIA>
.
|
Disregard my last comment; I thought this was in a different issue. Are you asking if typeorm can use the same technique as mocha? I think so yes. But I don't know anything about platformtools, so I don't know what it tries to do. |
@cspotcode oh right sorry. i thought you are in typeorm I'll try mocha way and maybe can make pr next week or new year |
I think you could just detect the package type and conditionally use import or require. |
The age of its deprecation is brought up a lot. I understand why, but it's the only way to do certain things. If you want to do the equivalent of |
I see. What type of libraries/projects are using Perhaps the best option would be to use |
To get a sense for all the different The big ones are things like ts-node, @babel/register, @swc/register, stuff like that. |
Can you at least include a flag to configure this easily? |
For this one, a dynamic import can be AFAICT a drop-in replacement, since it is already in an async context and auto-support of cjs is induced by one of the possible extensions. It would worth to add support to .mjs extension though once migrating to ESM.In one of my programs, I use ts-node to load ts files on the fly imported via dynamic imports. I don't remember where did I saw the piece of doc talking about this behavior but this is possible in cjs (not sure for ESM interop though).
typeorm/src/util/DirectoryExportedClassesLoader.ts Lines 36 to 41 in 749511d
This one is a bit trickier since this method is called in multiple places, some of them being in a sync context. I think I'm gonna fork and try to tweak a bit. |
So, for what I've seen so far, there is some heavy tooling rework to do to make it happen. For instance, functional tests include ts files compiled as cjs to test cjs imports, so reworking tsconfig to build package as both esm & cjs would require some weird inclusion patterns matching those files. Or, other option, write them by hand as cjs/esm to avoid ts transpilation altogether. I've bypassed the problem by modifying my build tool chains to use default imports and destructure them on the fly. I somewhat doubt that the maintainers will accept easily the PR required to make it happen, since it would change a lot of things. If a maintainer is passing by: have you tried anything about this ? |
When TypeORM tries to load an entity file or a connection config file, it will determine what is the appropriate module system to use for the file and then `import` or `require` it as it sees fit. Closes: typeorm#7516 Closes: typeorm#7159
About:
I would suggest using |
* feat: support importing TypeORM in esm projects Closes: #6974 Closes: #6941 * bugfix: generate index.mjs directly out of commonjs exports The new implementation generates ESM exports directly out of the commonjs exports, and provides a default export to maintain compatability with existing `import`s of the commonjs implementation * feat: support loading ESM entity and connection config files When TypeORM tries to load an entity file or a connection config file, it will determine what is the appropriate module system to use for the file and then `import` or `require` it as it sees fit. Closes: #7516 Closes: #7159 * fix: adapt ImportUtils.importOrRequireFile tests to older version of nodejs * fix: improved importOrRequireFile implementation * feat: add solution to circular dependency issue in ESM projects * docs: added FAQ regarding ESM projects * chore: add `"type": "commonjs"` to package.json * style * docs: improve `ts-node` usage examples for CLI commands in ESM projects * feat: add support for generating an ESM base project * refactor: renamed `Related` type to `Relation` * docs: added a section in the Getting Started guide regarding the `Relation` wrapper type in ESM projects * docs: improved documentation of the `Relation` type * docs: improved documentation of the `Relation` type * docs: added ESM support to the list of TypeORM features
Feature Description
The Problem
node 14 is now officially support ESM module, and ts-node is trying to support esm too.
https://nodejs.org/api/esm.html
TypeStrong/ts-node#1007
ESM module needs to set
"type": "module"
in package.json, but this option break typeorm cli because typeorm userequire
to import ts fileshttps://github.com/typeorm/typeorm/blob/master/src/connection/ConnectionOptionsReader.ts#L110
https://github.com/typeorm/typeorm/blob/master/src/util/DirectoryExportedClassesLoader.ts#L41
The Solution
use
import()
instead ofrequire()
maybe package compiling environment needs to update.
Considered Alternatives
Additional Context
Relevant Database Driver(s)
aurora-data-api
aurora-data-api-pg
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: