From a39757915364fc8110554a0f1429fd0be0c51153 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 6 Jul 2022 00:57:57 +0800 Subject: [PATCH] feat: support import assertions --- .../vite/src/node/plugins/importAnalysis.ts | 9 ++++++++- .../src/node/plugins/importAnalysisBuild.ts | 8 +++++++- .../__tests__/import-assertion.spec.ts | 10 ++++++++++ playground/import-assertion/data.json | 3 +++ .../import-assertion-dep/data.json | 3 +++ .../import-assertion-dep/index.js | 3 +++ .../import-assertion-dep/package.json | 7 +++++++ playground/import-assertion/index.html | 19 +++++++++++++++++++ playground/import-assertion/package.json | 14 ++++++++++++++ pnpm-lock.yaml | 15 +++++++++++++++ 10 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 playground/import-assertion/__tests__/import-assertion.spec.ts create mode 100644 playground/import-assertion/data.json create mode 100644 playground/import-assertion/import-assertion-dep/data.json create mode 100644 playground/import-assertion/import-assertion-dep/index.js create mode 100644 playground/import-assertion/import-assertion-dep/package.json create mode 100644 playground/import-assertion/index.html create mode 100644 playground/import-assertion/package.json 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 @@ +

Import assertion

+ +

From source code

+

+ +

From dependency

+

+ + diff --git a/playground/import-assertion/package.json b/playground/import-assertion/package.json new file mode 100644 index 00000000000000..68128c59993ffb --- /dev/null +++ b/playground/import-assertion/package.json @@ -0,0 +1,14 @@ +{ + "name": "test-import-assertion", + "private": true, + "version": "0.0.0", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite", + "preview": "vite preview" + }, + "dependencies": { + "@vitejs/import-assertion-dep": "file:./import-assertion-dep" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d63fd7c1ce006c..2135de17c45851 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -511,6 +511,15 @@ importers: playground/html: specifiers: {} + playground/import-assertion: + specifiers: + '@vitejs/import-assertion-dep': file:./import-assertion-dep + dependencies: + '@vitejs/import-assertion-dep': file:playground/import-assertion/import-assertion-dep + + playground/import-assertion/import-assertion-dep: + specifiers: {} + playground/js-sourcemap: specifiers: {} @@ -8808,6 +8817,12 @@ packages: vue: 3.2.37 dev: false + file:playground/import-assertion/import-assertion-dep: + resolution: {directory: playground/import-assertion/import-assertion-dep, type: directory} + name: '@vitejs/import-assertion-dep' + version: 0.0.0 + dev: false + file:playground/json/json-module: resolution: {directory: playground/json/json-module, type: directory} name: json-module