Skip to content

Commit

Permalink
fix: fix esbuild break when importRe matches multiline import (#4054)
Browse files Browse the repository at this point in the history
Co-authored-by: taylorliu <liufeichun@bilibili.com>
  • Loading branch information
dejour and taylorliu committed Jul 1, 2021
1 parent 19abafe commit eb2e41b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion packages/vite/src/node/__tests__/scan.spec.ts
@@ -1,4 +1,4 @@
import { scriptRE, commentRE } from '../optimizer/scan'
import { scriptRE, commentRE, importsRE } from '../optimizer/scan'

describe('optimizer-scan:script-test', () => {
const scriptContent = `import { defineComponent } from 'vue'
Expand Down Expand Up @@ -64,4 +64,36 @@ describe('optimizer-scan:script-test', () => {
expect(tag1).toEqual('<script>')
expect(content1).toEqual('var test = null')
})

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
} 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
*/`
]

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

const shouldFailArray = [
`testMultiline("import", {
body: "ok" });`,
`import type, {foo} from 'vue'`
]
shouldFailArray.forEach((str) => {
expect(importsRE.test(str)).toBe(false)
})
})
})
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -37,8 +37,8 @@ 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(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')/gm
export const importsRE =
/(?:^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')\s*(?:$|;|\/\/|\/\*)/gm

export async function scanImports(config: ResolvedConfig): Promise<{
deps: Record<string, string>
Expand Down

0 comments on commit eb2e41b

Please sign in to comment.