vite
vite copied to clipboard
Align Vite's import methods with the "Import Attributes" spec
Description
Currently Vite allows importing .css files by appending the suffix: ?inline
or ?url
after after the .css file.
Currently Vite allows importing .json files as named exports: import { field } from './example.json'
(https://vitejs.dev/guide/features.html#json)
Currently Vite allows importing assets by appending the suffix: ?raw
(https://vitejs.dev/guide/assets.html#explicit-url-imports)
JavaScript is getting closer to finish the "Import Attributes", its TC39 stage3 proposal (https://github.com/tc39/proposal-import-attributes).
The following is going to be possible:
import styles from './styles.css' with { type: 'css' };
(people using it as example: https://web.dev/articles/css-module-scripts)
import data from './data.json' with { type: 'json' };
(people using it as example: https://v8.dev/features/import-assertions)
import html from './index.html' with type { type: 'text' };
(people using it as example: https://bun.sh/blog/bun-v1.1.5#new-import-any-file-as-text)
import config from './configFile' with { type: 'toml' };
Suggested solution
Some of the import methods being promoted by Vite can already become closer to satisfy the TC39 proposal. Please, consider aligning your import methods to TC39 proposal from now on.
I would like to see mentioned in Vite's documentation, for example:
// import the entire object
import json from './example.json' with { type: 'json' };
instead of:
// import the entire object
import json from './example.json'
Alternative
No response
Additional context
No response
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Vite doesn't quite support import attributes yet, which is tracked at https://github.com/vitejs/vite/issues/14674. But after that we could may consider re-shaping the API to not rely on query params, so leaving this open for now.
Also consider exposing a API to plugins to register certain types that don't ship by default, so for example a yaml parser could register type: 'yaml'
. To support binary formats, I would suggest vite to pass a UInt8Array
to the plugin.