feat(compiler-sfc): avoid exposing imports not used in template

close #3183
This commit is contained in:
Evan You
2021-07-22 12:04:46 -04:00
parent db3f57a392
commit 5a3ccfd914
3 changed files with 52 additions and 3 deletions

View File

@@ -12,7 +12,11 @@ import {
TextRange
} from './parse'
import { parse as _parse, ParserOptions, ParserPlugin } from '@babel/parser'
import { babelParserDefaultPlugins, generateCodeFrame } from '@vue/shared'
import {
babelParserDefaultPlugins,
generateCodeFrame,
hyphenate
} from '@vue/shared'
import {
Node,
Declaration,
@@ -105,6 +109,7 @@ interface ImportBinding {
source: string
rangeNode: Node
isFromSetup: boolean
isUsedInTemplate: boolean
}
interface VariableBinding {
@@ -312,12 +317,21 @@ export function compileScript(
if (source === 'vue' && imported) {
userImportAlias[imported] = local
}
let isUsedInTemplate = true
if (sfc.template && !sfc.template.src) {
isUsedInTemplate = new RegExp(
`\\b(?:${local}|${hyphenate(local)})\\b`
).test(sfc.template.content)
}
userImports[local] = {
isType,
imported: imported || 'default',
source,
rangeNode,
isFromSetup
isFromSetup,
isUsedInTemplate
}
}
@@ -1279,7 +1293,7 @@ export function compileScript(
// return bindings from setup
const allBindings: Record<string, any> = { ...setupBindings }
for (const key in userImports) {
if (!userImports[key].isType) {
if (!userImports[key].isType && userImports[key].isUsedInTemplate) {
allBindings[key] = true
}
}