Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support import assertions #8937

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}(() => `)
Expand Down
10 changes: 10 additions & 0 deletions playground/import-assertion/__tests__/import-assertion.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
3 changes: 3 additions & 0 deletions playground/import-assertion/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
3 changes: 3 additions & 0 deletions playground/import-assertion/import-assertion-dep/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
3 changes: 3 additions & 0 deletions playground/import-assertion/import-assertion-dep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import json from './data.json' assert { type: 'json' }

export const hello = json.hello
7 changes: 7 additions & 0 deletions playground/import-assertion/import-assertion-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@vitejs/import-assertion-dep",
"private": true,
"version": "0.0.0",
"type": "module",
"exports": "./index.js"
}
19 changes: 19 additions & 0 deletions playground/import-assertion/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>Import assertion</h1>

<h2>From source code</h2>
<p class="src"></p>

<h2>From dependency</h2>
<p class="dep"></p>

<script type="module">
import * as data from './data.json' assert { type: 'json' }
text('.src', data.foo)

import * as depData from '@vitejs/import-assertion-dep'
text('.dep', depData.hello)

function text(el, text) {
document.querySelector(el).textContent = text
}
</script>
14 changes: 14 additions & 0 deletions playground/import-assertion/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.