-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Dynamic import of TS files from inside node_modules #1939
Comments
|
Hello @B4nan. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
Thanks for the prompt response!
When I put there the ORM core package, I get this error:
(the file is here: https://unpkg.com/browse/@mikro-orm/core@5.3.1/index.mjs) The package provides hybrid ESM support for named imports via
I get that, but the transformations should happen on a local file, not inside that package, so I was kinda expecting this to work. And then there is the need for Feels a bit weird that the exact same import behaves differently inside node_modules (using absolute path).
I tried to go with stackblitz but for that I need to move things to node_modules, and I dont think I can do that there. Will you be ok with a repo that moves the folder to node_modules in a postinstall hook or something like that? I know its quirky, but this seems to be issue with dependencies, and I dont want to publish some junk because of reproducing things... Or maybe it wont be needed if you say this is expected behaviour :] Will provide something tomorrow if needed. |
It will be ok.
What exactly is the problem? It doesn't understand typescript or mikro imports? If you need to transform files imported from |
Looking at your generated code, I can say that Node doesn't see named imports because they are applied dynamically (I think). You need to use |
Hmm but that index.mjs file works just fine, only once it gets inlined it fails this way. |
Interesting. Please, create a reproduction with this case, I will look into it. |
Here is the repro, its really as simple as this: https://stackblitz.com/edit/vitest-dev-vitest-d33pe8?file=test/basic.test.ts Same code works fine when executed directly, not via vitest. I do have a working ESM project example here (not TS, but thats unrelated). |
Ok, if it works without inlining what is the problem? |
You said the correct solution to the original problem is inlining. This is the module where dynamic import happens. |
This has nothing to do with TypeScript and your original problem though. The problem here is that Vitest doesn't also store all commonjs properties on {
deps: {
inline: [/mikro\/core\/index.mjs/],
external: [/mikro\/core\/index.js/],
}
} |
While this fixes the issue with inlining, it doesnt seem to work for the original use case. I still see the same errors as in the OP. Updated the repro to showcase the actual problem, where I do the dynamic import via MikroORM. Currently it works via eval trick ( https://unpkg.com/browse/@mikro-orm/core@5.3.2-dev.32/dist/utils/Utils.js#L738 (note: this is using latest dev version, dev versions are published with the |
This is why I was confused earlier.
Well, Vitest doesn't support this usage, unfortunately. We need to replace it, as I said before, otherwise it can't go through Vite pipeline.
I don't understand what you mean here 😄 Does it work, or does it not? I can see that you can import |
What I mean is that I literally changed this eval trick with a real So I understand it will need to be changed on my end to support it, but I tried already and it still fails the same. |
Can you give a proper reproduction where Vitest fails for you? You are saying that "when you do something", "something works/doesn't work" - I can't really help, when I don't see code. All previous cases are discussed and explained. If I "change" import to actual import call, I get "No entities were discovered" error, for example. |
By the way, even if you could import TypeScript file, I'm not sure if Vitest can properly transform it, since esbuild doesn't support decorator metadata (I don't know if you use it, but I see you are using decorators): #708 (comment) |
Let me quickly fix the imports and publish new dev version so we can forget about this problem. Btw I dont use reflect-metadata, so no problem with decorators (its pretty much unusable with ESM as it fails to resolve cycles and errors on runtime because of it). If you dont mind, I could invite you to the private repo where I have the full project. It is a tutorial I am working on, so nothing complex. There you could see the app that runs fine (compiled via node as well as TS via ts-node-esm, but same code fails with vitest). I was trying to isolate this, but I see it's not really helpful and only adds confusion (so sorry about that). |
Sure, I am open to it. |
So now in version dev-33 it is a real dynamic import: https://unpkg.com/browse/@mikro-orm/core@5.3.2-dev.33/dist/utils/Utils.js#L712 And in that stack blitz it fails the same. |
Utils is loaded with |
Hmm I see, so this can only work with real ESM projects, and this edit: or it would be still a problem given this function would be called through some other requires anyway? because it will still be triggered from inside some CJS file in the end |
If the file where |
Understood. I went with a different approach, using a global variable with a dynamic import callback, which can be overridden by user via Thanks a lot for your insights. |
Describe the bug
If I do
await import('./user.ts')
from inside a test, it works just fine. If I move this dynamic import into a dependency, it fails with following:In actual test i am using absolute paths to be sure, simplifying here for readability. The contents of the file as simplified for testing to just
export const User: any = {}
.I even tried to create a simple
pkg
folder with package.json and a single file exporting that function. Works fine if its outside of node_modules, once I move it there, it fails this way. Doesnt matter if I useimport from 'pkg'
orimport from '../node_modules/pkg/index.mjs'
.Tried to enable
registerNodeLoader
, that changes the error to following:This is because it is TS file, and it fails on the first TS part. If I remove the type hint (which makes it a valid JS file), it works fine.
Then I finally tried
inline: ['pkg']
and it started working. But this workaround does not work for me in the actual use case, where it breaks the dependency (@mikro-orm/core
).Reproduction
user.ts
file with justexport const AuthorListing: any = {}
.node_modules/pkg
folder withindex.mjs
file:importTest('./user.ts')
from a test file.System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: