Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions packages/vite/src/node/__tests__/scan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,35 @@ describe('optimizer-scan:script-test', () => {
})

test('imports regex should work', () => {
const shouldMatchArray = [
`import 'vue'`,
`import { foo } from 'vue'`,
`import foo from 'vue'`,
`;import foo from 'vue'`,
` import foo from 'vue'`,
`import { foo
const shouldMatchArray: Array<[string, string]> = [
[`import 'vue'`, `'vue'`],
[`import { foo } from 'vue'`, `'vue'`],
[`import foo from 'vue'`, `'vue'`],
[`;import foo from 'vue'`, `'vue'`],
[` import foo from 'vue'`, `'vue'`],
[
`import { foo
} from 'vue'`,
`import bar, { foo } from 'vue'`,
`import foo from 'vue';`,
`*/ import foo from 'vue';`,
`import foo from 'vue';//comment`,
`import foo from 'vue';/*comment
`'vue'`,
],
[`import bar, { foo } from 'vue'`, `'vue'`],
[`import foo from 'vue';`, `'vue'`],
[`*/ import foo from 'vue';`, `'vue'`],
[`import foo from 'vue';//comment`, `'vue'`],
[
`import foo from 'vue';/*comment
*/`,
// Skipped, false negatives with current regex
// `import typescript from 'typescript'`,
// import type, {foo} from 'vue'
`'vue'`,
],
// #23471: bindings starting with `type` are not `import type` statements
[`import typescript from 'typescript'`, `'typescript'`],
[`import typeorm from 'typeorm'`, `'typeorm'`],
[`import types from 'types'`, `'types'`],
]

shouldMatchArray.forEach((str) => {
shouldMatchArray.forEach(([str, expected]) => {
importsRE.lastIndex = 0
expect(importsRE.exec(str)![1]).toEqual("'vue'")
expect(importsRE.exec(str)![1]).toEqual(expected)
})

const shouldFailArray = [
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 @@ -111,7 +111,7 @@ const htmlTypesRE = /\.(?:html|vue|svelte|astro|imba)$/
// since even missed imports can be caught at runtime, and false positives will
// simply be ignored.
export const importsRE: RegExp =
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type\b)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm

export function scanImports(environment: ScanEnvironment): {
cancel: () => Promise<void>
Expand Down
Loading