-
-
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
Support Import Assertions #4934
Comments
Also note that typescript is planning to include import assertions in 4.5. |
This comment was marked as outdated.
This comment was marked as outdated.
TypeScript has merged import assertion support, so it should be coming relatively soon! There has been a lot of community interest in using import assertions in Astro. Would love to support this effort! |
@natemoo-re Feel free to start a PR that moves alongside the main branch 🙂 |
Would love to see this added as well. Is there a workaround for the time being? |
🔴 FYI; this issue is participating to quite a few problems, such as surprise crashes late in the release cycle (upon starting in staging or prod for some) .
practical consequence for quite a few people with a project on a PaaS like heroku, built with vite, importing json files, and built/tested on non latest-node-LTS-enabled CI server: surprise crash when landing in staging or prod for those without staging. |
Is there anything specific Vite has to cover for import assertions? We've updated our dependencies (esbuild, es-module-lexer, etc) to already support import assertions. So right now they should be left as is, as Vite doesn't do anything special with them yet. If there are issues related to it, it would be great to provide a repro to investigate. |
@rvion one mitigating strategy to prevent these kinds of issues is to use something like nvm or https://volta.sh/ to be sure that everyone is using the same node version, and also you should check that your CI/CD is locked to a specific node version. |
I'm also interested on seeing this supported by vite, advising users to remain out of date of the environment updates shouldn't be the answer. As a temporary workaround I'd suggest to pre process your JSON files and convert them into JS modules to avoid the asserts error on build time. |
@bluwy it appears to be related to babel
|
@bluwy Here's a minimal example that reproduces the error |
Thanks for the example @jimboHenris. It looks like you need to install export default defineConfig({
plugins: [
react({
babel: {
plugins: ['@babel/plugin-syntax-import-assertions'],
},
}),
],
}); For it to work. Perhaps we can add the plugin to But I'm also seeing an error which Vite is returning Javascript when importing |
Hey @bluwy , In order to replicate, I forked the Vite repo here and added an import assertion to the react-ssr playground example. If you clone the repo I linked, cd into the playground/ssr-react directory, then npm run dev and try to open the webpage the error should occur. Please let me know if I can provide any more information that would be helpful when debugging. |
Been thinking about this, and I wonder what it really takes to "support import assertions". On one hand, Vite can preserve the import assertions as is, and return the correct MIME type for it. But that only works in Chrome in dev mode only. In builds, Rollup doesn't support it yet as Acorn (the parser it uses) only implements stage 4 specs. On the other hand, Vite can "support" it too by stripping off any usage of import assertions as the behaviour would essentially be the same when browser & nodejs support become widespread. I'm leaning towards the second option for now, but I also wonder why one would reach out to import assertions in the first place if they know what Vite does under the hood. |
Do you mean why would someone use import assertions if they're using Vite? The actual use case here is importing a library which uses import assertions into your Vite project. And import assertions were a security patch added to Node 17 and back-ported to Node 16. |
I just tried a dependency that uses import assertions and it worked for me, provided that it's being optimized by esbuild. If it's excluded from optimization via I'm not sure if this is an issue, but maybe it would make sense for Vite to strip it out then if there aren't consequences. |
My use case is actually loading config files on server start. Instead of a complicated and brittle workaround using |
Any chance of this making it into 3.0? |
I submitted #8937 to apply the strip approach, which essentially removes the |
It was fixed when Vite beta was prebundling in build too, but now it's not. You can bring it back by specifying |
This doesn't seem to work anymore either. So for now I will grab back to older versions until this is working. { optimizeDeps.disabled: false } |
I found below code could work for me. You can use dynamic import for json file. import("xxx/test.json", {
assert: {
type: "json"
}
}) |
I'm looking to import css as a CSSStyleSheet() using assert imports. Theres If the import-assert plugin is added
If the import-assert plugin is added
Is there a way to get css assert imports working for the time being until Vite officially supports assert? |
Importing json files does not work for me :/ getting this error import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
babel: {
plugins: ['@babel/plugin-syntax-import-assertions'],
},
}),
],
base: './',
optimizeDeps: {
disabled: false,
},
}); EDIT: I am also getting this error. The path is './quiz_data/test_quiz.json' which i believe is the correct format
|
It looks like import assertions inside Vue single file components are not yet available, either? I am getting:
|
It's working in |
Hm, for me it does not work in |
It works with
|
@IanVS that could be it, I worked on this project a while ago and it seems I got it working in the end by using the eager option const all_res = import.meta.glob('../quiz_data/*.json', { eager: true }); This meant I couldn't use a template literal as the path as they don't work with the eager option. |
Rollup v3.3.0 add support for import assertions. |
https://github.com/tc39/proposal-import-assertions
Vite should support import-assertions because it has entered stage 3.
The text was updated successfully, but these errors were encountered: