fix: strip import attributes from dynamic imports in dev mode#22023
Open
teee32 wants to merge 1 commit intovitejs:mainfrom
Open
fix: strip import attributes from dynamic imports in dev mode#22023teee32 wants to merge 1 commit intovitejs:mainfrom
teee32 wants to merge 1 commit intovitejs:mainfrom
Conversation
Import attributes (e.g. `{ with: { type: 'json' } }`) were already
stripped from static imports during dev, but dynamic imports were
explicitly excluded. This caused:
- Firefox: SyntaxError (import attributes not yet supported)
- Chrome: MIME type mismatch (expects application/json due to type assertion,
but Vite serves transformed JS)
Extend the existing attribute stripping to also cover dynamic imports,
adjusting the removal range for the different syntax structure (second
argument inside parentheses vs trailing clause on static import).
Closes vitejs#19095
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Dynamic imports with import attributes (e.g.
import('./data.json', { with: { type: 'json' } })) cause runtime errors in dev mode:SyntaxError: missing ) after argument list— Firefox does not yet support import attributes syntaxFailed to load module script: Expected a JSON module script but the server responded with a MIME type of "text/javascript"Vite already strips import attributes from static imports during dev (since it processes typed imports internally), but dynamic imports were explicitly excluded by the
!isDynamicImportguard atimportAnalysis.ts:523. This PR extends the attribute stripping to also cover dynamic imports, adjusting the removal range for the parenthesized syntax.Fixes #19095
Alternatives considered
application/jsonMIME type when import attributes are present — this would fix Chrome but not Firefox, and would conflict with Vite's JSON-to-JS transform pipeline.Reviewer notes
The core change is small (one condition change + one new branch in
importAnalysis.ts). The key insight is that for dynamic imports, import attributes are the second argument insideimport(specifier, options), so the removal range is[end, expEnd - 1)(after specifier, before closing paren), vs[end + 1, expEnd)for static imports.dynamic import with import attributesin JSON playground, serve-only)