Skip to content

Commit daf7838

Browse files
hannoeruantfu
andauthored
fix(scan): avoid crawling type only import (#2810)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 5dfd0e8 commit daf7838

File tree

7 files changed

+24
-1
lines changed

7 files changed

+24
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface Foo {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "dep-import-type",
3+
"version": "0.0.0",
4+
"main": "index.js"
5+
}

packages/playground/ssr-vue/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@vitejs/plugin-vue-jsx": "^1.1.2",
2121
"@vue/compiler-sfc": "^3.0.8",
2222
"@vue/server-renderer": "^3.0.6",
23+
"dep-import-type": "link:./dep-import-type",
2324
"compression": "^1.7.4",
2425
"cross-env": "^7.0.3",
2526
"express": "^4.17.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<p>import type should be removed without side-effect</p>
3+
</template>
4+
5+
<script setup lang="ts">
6+
import type { Foo } from 'dep-import-type/deep'
7+
8+
const msg: Foo = {}
9+
</script>

packages/playground/ssr-vue/src/pages/Home.vue

+3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
<button @click="state.count++">count is: {{ state.count }}</button>
77
<Foo />
88
<p class="virtual">msg from virtual module: {{ foo.msg }}</p>
9+
10+
<ImportType/>
911
</template>
1012

1113
<script setup>
1214
import foo from '@foo'
1315
import { reactive, defineAsyncComponent } from 'vue'
16+
import ImportType from '../components/ImportType.vue'
1417
const Foo = defineAsyncComponent(() =>
1518
import('../components/Foo').then((mod) => mod.Foo)
1619
)

packages/vite/src/node/optimizer/scan.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const htmlTypesRE = /\.(html|vue|svelte)$/
3838
// use Acorn because it's slow. Luckily this doesn't have to be bullet proof
3939
// since even missed imports can be caught at runtime, and false positives will
4040
// simply be ignored.
41-
const importsRE = /\bimport(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')/gm
41+
const importsRE = /\bimport(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')/gm
4242

4343
export async function scanImports(
4444
config: ResolvedConfig

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,10 @@ delegate@^3.1.2:
26522652
version "0.0.0"
26532653
uid ""
26542654

2655+
"dep-import-type@link:./packages/playground/ssr-vue/dep-import-type":
2656+
version "0.0.0"
2657+
uid ""
2658+
26552659
"dep-linked-include@link:./packages/playground/optimize-deps/dep-linked-include":
26562660
version "0.0.0"
26572661
uid ""

0 commit comments

Comments
 (0)