vite icon indicating copy to clipboard operation
vite copied to clipboard

Align Vite's import methods with the "Import Attributes" spec

Open darkgreentree opened this issue 10 months ago • 2 comments

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

darkgreentree avatar Apr 27 '24 19:04 darkgreentree

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.

bluwy avatar Apr 29 '24 10:04 bluwy

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.

silverwind avatar May 22 '24 20:05 silverwind