-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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(imports): support for import attributes in Vite #17485
Conversation
Run & review this pull request in StackBlitz Codeflow. |
Hi! lint job fails since |
That's weird, the ESLint config is already using the TypeScript parser, which does support import attributes. See: https://x.com/bradzacher/status/1772901399518408775 I just used it myself, without any config: https://github.com/refined-github/shorten-repo-url/pull/48/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R34 |
I know! But i dont really know how fix it. I get this message if I remove it form D:\repos\vite\playground\resolve\import-attributes.js
1:74 error Parsing error: ';' expected BTW, no matter if I add or not ';', it doesn't desappear. Maybe it is caused by pnpm resolution to typescript 5.2.2 and import attributes are added in 5.3 Lines 133 to 135 in 61357f6
|
@fregante yep! Typescript supports import attributes in v5.3 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html |
I just realized:
That's not what I'd want though, if the import attributes are removed they will be broken. I'm using Vite to bundle a library that imports a third-party JSON file. Here I'm using Vite for tree-shaking, without bundling external dependencies. |
Well, import attribute removal is not about vite (only), rollup output removes attributes no matter what you do https://stackblitz.com/edit/rollup-import-attributes?file=rollup.config.js In that sense, I updated the NOTE: Remember, vite uses esbuild for development, but at build time, rollup bundles our code. If you want a transpiler option you can opt by esbuild, whin bundle option https://esbuild.github.io/getting-started/#bundling-for-the-browser |
Thanks @redfox-mx! That mostly worked for me |
Hi @sheremet-va, nice to meet you. I update this pr to support import atributes into vite system, now covers hooks (not only virtual modules) and module graph integration. I tried to write a test to cover import attributes proposal and read more about how @bluwy propose changes in #12140, so now import attributes metadata is preserved into module node. Have a nice day n.n |
Maybe I don't really know if it is part of the plans for the vite team. So, I close this PR. Maybe in the future when environment API or vite 6 will be ready. Idk |
I am actually invested in supporting this, I've bumped the priority a bit higher to discuss this in the next team meeting |
I also wanted to bring this up and the PR seems nice. Thanks @redfox-mx for the effort! One thing I was curious about is, what would happen when there are multiple imports for the same file but with different import attirbutes like this? import repro1 from "./repro.js" with { custom: "x" };
import repro2 from "./repro.js" with { custom: "y" }; I was checking rollup and it shows a warning (rollup repl):
From the spec (especially since the change of tc39/proposal-import-attributes#131 about the cache key), it looks like this is allowed and it's more like a bundler side limitation. With vite/rollup plugin, it feels somewhat natural for people to try to transform differently based on import attributes even for the same file. Though this might not be an issue in practice, I wanted to check what other thinks about this use case. |
That's one of the main reason why I'm conflicted with adding import attributes support in Vite currently. The right way is to allow specifying different attributes for the same import specifier, making it part of the cache key. But Rollup doesn't support that at the moment, I think it's a remnant from past import assertions support where assertions should always be the same for an import specifier. (And I think this is noted as a Rollup limitation somewhere). So if we support this in Vite now, it might not work in build. |
Yeah, if rollup doesn't support this, I am afraid we can't support it either 😞 I wonder if rolldown is better on this front. |
Hello 👋 thanks you for the feedback. I didn't think about use multiple import attributes for the same file. But maybe we can make it work with some "hack". Currently (at least 4 months ago) vite drops import attributes at importAnalysis plugins (that was the first step to add "support") rewriting imports. So, in theory, We can rewrite import specifier to add a custom query that allows create a different module node to be resolved with no conflict. I mean import repro1 from "./repro.js" with { custom: "x" };
import repro2 from "./repro.js" with { custom: "y", foo: "baz" }; Can be transformed to something like (at the same time we keep information into module graph) import repro1 from "./repro.js?import-attribute=custom:x";
import repro2 from "./repro.js?import-attribute=custom:y,foo:baz"; Ps: Ps2: I'm still learning English, please be patient if I don't really explain me at all. 🙏 |
Ps3: Happy viteconf! |
I think this makes sense, and ideally we'd have to do something similar internally as the cache key too. But would this work properly with plugins receiving the |
Import attributes is stage 4 now!!! 🎊 |
For anyone following, we had discussed the required implementation a bit and sapphi opened an issue in Rollup (link above) that will be required by Vite in order to correctly implement the support. |
For context: I opened an RFC for proper import attributes support #18534 The scope of impact is actually a bit larger than what this PR covers, but I think when we settle on the direction this PR can still be a good starting point. |
Description
Fixes #14674, #12140
Adds support for import attributes by parsing them in the import analysis plugin.
Additional context
How this works:
Build phase
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
PS: this pr solve some errors of #15654