Skip to content

Commit 39330f4

Browse files
authored
fix(optimizer): don't skip imports whose binding starts with type (#23540)
1 parent bd97903 commit 39330f4

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎packages/vite/src/node/__tests__/scan.spec.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,40 @@ describe('optimizer-scan:script-test', () => {
8585
})
8686

8787
test('imports regex should work', () => {
88-
const shouldMatchArray = [
89-
`import 'vue'`,
90-
`import { foo } from 'vue'`,
91-
`import foo from 'vue'`,
92-
`;import foo from 'vue'`,
93-
` import foo from 'vue'`,
94-
`import { foo
88+
const shouldMatchArray: [code: string, expected: string][] = [
89+
[`import 'vue'`, `'vue'`],
90+
[`import { foo } from 'vue'`, `'vue'`],
91+
[`import foo from 'vue'`, `'vue'`],
92+
[`;import foo from 'vue'`, `'vue'`],
93+
[` import foo from 'vue'`, `'vue'`],
94+
[
95+
`import { foo
9596
} from 'vue'`,
96-
`import bar, { foo } from 'vue'`,
97-
`import foo from 'vue';`,
98-
`*/ import foo from 'vue';`,
99-
`import foo from 'vue';//comment`,
100-
`import foo from 'vue';/*comment
97+
`'vue'`,
98+
],
99+
[`import bar, { foo } from 'vue'`, `'vue'`],
100+
[`import foo from 'vue';`, `'vue'`],
101+
[`*/ import foo from 'vue';`, `'vue'`],
102+
[`import foo from 'vue';//comment`, `'vue'`],
103+
[
104+
`import foo from 'vue';/*comment
101105
*/`,
102-
// Skipped, false negatives with current regex
103-
// `import typescript from 'typescript'`,
104-
// import type, {foo} from 'vue'
106+
`'vue'`,
107+
],
108+
// https://github.com/vitejs/vite/issues/23471
109+
// bindings starting with "type" should not be treated as type-only imports
110+
[`import typescript from 'typescript'`, `'typescript'`],
111+
[`import typeorm from 'typeorm'`, `'typeorm'`],
112+
[`import types from 'types'`, `'types'`],
113+
// still a known false negative: a default binding literally named `type`
114+
// (`import type, {foo} from 'vue'`, `import type from 'vue'`) is a valid
115+
// value import, but the word boundary cannot tell it apart from the
116+
// `import type` modifier. Missed deps are discovered again at runtime.
105117
]
106118

107-
shouldMatchArray.forEach((str) => {
119+
shouldMatchArray.forEach(([str, expected]) => {
108120
importsRE.lastIndex = 0
109-
expect(importsRE.exec(str)![1]).toEqual("'vue'")
121+
expect(importsRE.exec(str)![1]).toEqual(expected)
110122
})
111123

112124
const shouldFailArray = [

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const htmlTypesRE = /\.(?:html|vue|svelte|astro|imba)$/
111111
// since even missed imports can be caught at runtime, and false positives will
112112
// simply be ignored.
113113
export const importsRE: RegExp =
114-
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
114+
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type\b)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
115115

116116
export function scanImports(environment: ScanEnvironment): {
117117
cancel: () => Promise<void>

0 commit comments

Comments
 (0)