diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 3458b9f3d51d8e..94105bc58a5db8 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -387,10 +387,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { s: start, e: end, ss: expStart, + se: expEnd, d: dynamicIndex, // #2083 User may use escape path, // so use imports[index].n to get the unescaped string - n: specifier + n: specifier, + a: assertIndex } = imports[index] const rawUrl = source.slice(start, end) @@ -427,6 +429,11 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { const isDynamicImport = dynamicIndex > -1 + // strip import assertions as we can process them ourselves + if (!isDynamicImport && assertIndex > -1) { + str().remove(end + 1, expEnd) + } + // static import or valid string in dynamic import // If resolvable, let's resolve it if (specifier) { diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 18a33b6d61ce74..c431ec2d1ef31a 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -225,11 +225,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { ss: expStart, se: expEnd, n: specifier, - d: dynamicIndex + d: dynamicIndex, + a: assertIndex } = imports[index] const isDynamicImport = dynamicIndex > -1 + // strip import assertions as we can process them ourselves + if (!isDynamicImport && assertIndex > -1) { + str().remove(end + 1, expEnd) + } + if (isDynamicImport && insertPreload) { needPreloadHelper = true str().prependLeft(expStart, `${preloadMethod}(() => `) diff --git a/playground/import-assertion/__tests__/import-assertion.spec.ts b/playground/import-assertion/__tests__/import-assertion.spec.ts new file mode 100644 index 00000000000000..796011926f0e7d --- /dev/null +++ b/playground/import-assertion/__tests__/import-assertion.spec.ts @@ -0,0 +1,10 @@ +import { expect, test } from 'vitest' +import { page } from '~utils' + +test('from source code', async () => { + expect(await page.textContent('.src'), 'bar') +}) + +test('from dependency', async () => { + expect(await page.textContent('.dep'), 'world') +}) diff --git a/playground/import-assertion/data.json b/playground/import-assertion/data.json new file mode 100644 index 00000000000000..c8c4105eb57cda --- /dev/null +++ b/playground/import-assertion/data.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/playground/import-assertion/import-assertion-dep/data.json b/playground/import-assertion/import-assertion-dep/data.json new file mode 100644 index 00000000000000..f2a886f39de7d3 --- /dev/null +++ b/playground/import-assertion/import-assertion-dep/data.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/playground/import-assertion/import-assertion-dep/index.js b/playground/import-assertion/import-assertion-dep/index.js new file mode 100644 index 00000000000000..a6dd471aa74a76 --- /dev/null +++ b/playground/import-assertion/import-assertion-dep/index.js @@ -0,0 +1,3 @@ +import json from './data.json' assert { type: 'json' } + +export const hello = json.hello diff --git a/playground/import-assertion/import-assertion-dep/package.json b/playground/import-assertion/import-assertion-dep/package.json new file mode 100644 index 00000000000000..6f701dc444bd67 --- /dev/null +++ b/playground/import-assertion/import-assertion-dep/package.json @@ -0,0 +1,7 @@ +{ + "name": "@vitejs/import-assertion-dep", + "private": true, + "version": "0.0.0", + "type": "module", + "exports": "./index.js" +} diff --git a/playground/import-assertion/index.html b/playground/import-assertion/index.html new file mode 100644 index 00000000000000..54b576a17ba66f --- /dev/null +++ b/playground/import-assertion/index.html @@ -0,0 +1,19 @@ +