Skip to content

Commit 5a3ccfd

Browse files
committed
feat(compiler-sfc): avoid exposing imports not used in template
close #3183
1 parent db3f57a commit 5a3ccfd

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ return { x }
204204
}"
205205
`;
206206
207+
exports[`SFC compile <script setup> imports imports not used in <template> should not be exposed 1`] = `
208+
"import { defineComponent as _defineComponent } from 'vue'
209+
import { FooBar, FooBaz, FooQux, vMyDir, x, y } from './x'
210+
211+
export default _defineComponent({
212+
setup(__props, { expose }) {
213+
expose()
214+
215+
const fooBar: FooBar = 1
216+
217+
return { fooBar, FooBaz, FooQux, vMyDir, x }
218+
}
219+
220+
})"
221+
`;
222+
207223
exports[`SFC compile <script setup> imports should allow defineProps/Emit at the start of imports 1`] = `
208224
"import { ref } from 'vue'
209225

packages/compiler-sfc/__tests__/compileScript.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ defineExpose({ foo: 123 })
209209
content.lastIndexOf(`import { x }`)
210210
)
211211
})
212+
213+
test('imports not used in <template> should not be exposed', () => {
214+
const { content } = compile(`
215+
<script setup lang="ts">
216+
import { FooBar, FooBaz, FooQux, vMyDir, x, y } from './x'
217+
const fooBar: FooBar = 1
218+
</script>
219+
<template>
220+
<FooBaz v-my-dir>{{ x }} {{ yy }}</FooBaz>
221+
<foo-qux/>
222+
</template>
223+
`)
224+
assertCode(content)
225+
// FooBaz: used as PascalCase component
226+
// FooQux: used as kebab-case component
227+
// vMyDir: used as directive v-my-dir
228+
// x: used in interpolation
229+
expect(content).toMatch(`return { fooBar, FooBaz, FooQux, vMyDir, x }`)
230+
})
212231
})
213232

214233
describe('inlineTemplate mode', () => {

packages/compiler-sfc/src/compileScript.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
TextRange
1313
} from './parse'
1414
import { parse as _parse, ParserOptions, ParserPlugin } from '@babel/parser'
15-
import { babelParserDefaultPlugins, generateCodeFrame } from '@vue/shared'
15+
import {
16+
babelParserDefaultPlugins,
17+
generateCodeFrame,
18+
hyphenate
19+
} from '@vue/shared'
1620
import {
1721
Node,
1822
Declaration,
@@ -105,6 +109,7 @@ interface ImportBinding {
105109
source: string
106110
rangeNode: Node
107111
isFromSetup: boolean
112+
isUsedInTemplate: boolean
108113
}
109114

110115
interface VariableBinding {
@@ -312,12 +317,21 @@ export function compileScript(
312317
if (source === 'vue' && imported) {
313318
userImportAlias[imported] = local
314319
}
320+
321+
let isUsedInTemplate = true
322+
if (sfc.template && !sfc.template.src) {
323+
isUsedInTemplate = new RegExp(
324+
`\\b(?:${local}|${hyphenate(local)})\\b`
325+
).test(sfc.template.content)
326+
}
327+
315328
userImports[local] = {
316329
isType,
317330
imported: imported || 'default',
318331
source,
319332
rangeNode,
320-
isFromSetup
333+
isFromSetup,
334+
isUsedInTemplate
321335
}
322336
}
323337

@@ -1279,7 +1293,7 @@ export function compileScript(
12791293
// return bindings from setup
12801294
const allBindings: Record<string, any> = { ...setupBindings }
12811295
for (const key in userImports) {
1282-
if (!userImports[key].isType) {
1296+
if (!userImports[key].isType && userImports[key].isUsedInTemplate) {
12831297
allBindings[key] = true
12841298
}
12851299
}

0 commit comments

Comments
 (0)