[RFC] Proper Import Attributes support #18534
Replies: 3 comments 2 replies
-
Great discussion. What I have to say is probably already part of your current plan, but it's worth adding that it would be good to prioritize accepting the standard |
Beta Was this translation helpful? Give feedback.
-
About moving from import txt from './foo.txt?raw' to import txt from './foo.txt' with { type: 'raw' } I think this is good from an authoring POV. We'll still need to use the We can probably have |
Beta Was this translation helpful? Give feedback.
-
This would be a huge benefit to the quality of experience that me and my team get from working with Vite and Vitest. Hopefully it is something that can come to life very soon! Getting in position to both support native and non-standard types will prepare Vite to support the specification of future attributes type (like |
Beta Was this translation helpful? Give feedback.
-
Context: Import Attributes is now stage 4: https://github.com/tc39/proposal-import-attributes
Related PR / issues:
There are few different aspects regarding properly import attributes support in Vite:
PluginContainer
)resolveId
Rollup Behavior Alignment
Currently in Rollup, support for import attributes is somewhat lacking. Only the
resolveId
hook receives parsed attributes via the 3rd options argument:load
andtransform
do not have access to this information via arguments.In Vite we already augment
resolveId
,load
, andtransform
hooks with an extraoptions
argument to pass the SSR flag (relevant docs):In Rolldown, we should provide the
attributes
property viaoptions
inresolveId
,load
, andtransform
so we can do:This information should also be made available to equivalent hooks in the Rust plugin API.
ID uniqueness
Should we include serialized attributes as part of the module ID? Internally this is definitely a yes as this is required to ensure unique id -> module mapping, the question is more about the public module IDs provided to plugin hooks:
There are multiple issues if we include serialized attributes in ids provided to hooks:
resolveId
for bare specifiersThe potential breakage seems to be too costly.
The downside of not including attributes in ids is this makes ids no longer unique. A practical case where this can cause issues is plugins that use ids as keys for custom caching behavior. However, this should be rare in practice and the impact surface is much smaller than including attributes.
Proposed actions:
ids
passed to hooks - keep them working like before.resolveId
, add the following attributes to theoptions
object:importerAttributes: JsonValue
fullId: string
fullImporterId: string
load
, add a 2ndoptions
argument in the shape of{ attributes: JsonValue, fullId: string }
transform
, add the following to the 3rdoptions
argument:{ attributes: JsonValue, fullId: string }
With this, the only possible breakage would be users using
id
as custom cache keys. The migration is also relatively simple: useoptions.fullId
as the key instead. The serialization format of attributes infullId
is now also free from constraints as long as they are guaranteed to be unique.The naming of
fullId
is open to suggestions.Migrating Vite magic queries to import attributes
In Vite, many built-in features and plugins rely on query strings in module IDs for this type of metadata, for example:
?raw
?url
?inline
(for CSS & Workers)?worker
/?sharedworker
(for Workers)?init
(for WebAssembly)With import attributes being standardized, I think we should eventually deprecate query string usage and move towards import attributes.
Examples
Raw Text
Before
After
Asset URL
Before
After
Inline CSS
Before
After
Worker Import
Before
After
@vitejs/plugin-vue
block importsBefore
After
type
namespaceIn the above examples, we are using
type
with values that map to the current built-in features of Vite. According to the spec:So in theory, it is possible for future platform specs to make use of the
type
field - e.g. native CSS Modules. It is also possible for different JavaScript runtimes / environments to provide different handling of import attributes.We have a few options here:
type
. E.g.as
with { 'vite-type': 'inline-css' }
orwith { type: 'vite-inline-css' }
.Option (2) has the benefit of being more explicit about these being Vite features, but is much more verbose.
Beta Was this translation helpful? Give feedback.
All reactions