Skip to content

Commit

Permalink
fix(scan): avoid crawling type only import (#2810)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
hannoeru and antfu committed Apr 9, 2021
1 parent 5dfd0e8 commit daf7838
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface Foo {}
5 changes: 5 additions & 0 deletions packages/playground/ssr-vue/dep-import-type/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "dep-import-type",
"version": "0.0.0",
"main": "index.js"
}
1 change: 1 addition & 0 deletions packages/playground/ssr-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@vitejs/plugin-vue-jsx": "^1.1.2",
"@vue/compiler-sfc": "^3.0.8",
"@vue/server-renderer": "^3.0.6",
"dep-import-type": "link:./dep-import-type",
"compression": "^1.7.4",
"cross-env": "^7.0.3",
"express": "^4.17.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/playground/ssr-vue/src/components/ImportType.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<p>import type should be removed without side-effect</p>
</template>

<script setup lang="ts">
import type { Foo } from 'dep-import-type/deep'
const msg: Foo = {}
</script>
3 changes: 3 additions & 0 deletions packages/playground/ssr-vue/src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
<button @click="state.count++">count is: {{ state.count }}</button>
<Foo />
<p class="virtual">msg from virtual module: {{ foo.msg }}</p>

<ImportType/>
</template>

<script setup>
import foo from '@foo'
import { reactive, defineAsyncComponent } from 'vue'
import ImportType from '../components/ImportType.vue'
const Foo = defineAsyncComponent(() =>
import('../components/Foo').then((mod) => mod.Foo)
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const htmlTypesRE = /\.(html|vue|svelte)$/
// use Acorn because it's slow. Luckily this doesn't have to be bullet proof
// since even missed imports can be caught at runtime, and false positives will
// simply be ignored.
const importsRE = /\bimport(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')/gm
const importsRE = /\bimport(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')/gm

export async function scanImports(
config: ResolvedConfig
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,10 @@ delegate@^3.1.2:
version "0.0.0"
uid ""

"dep-import-type@link:./packages/playground/ssr-vue/dep-import-type":
version "0.0.0"
uid ""

"dep-linked-include@link:./packages/playground/optimize-deps/dep-linked-include":
version "0.0.0"
uid ""
Expand Down

0 comments on commit daf7838

Please sign in to comment.