Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scan): do not match 'export default' in comments #4602

Merged
merged 3 commits into from Aug 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -41,6 +41,7 @@ export const importsRE =
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm

const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
const singleCommentsRegex = /\/\/.*/g
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved

export async function scanImports(config: ResolvedConfig): Promise<{
deps: Record<string, string>
Expand Down Expand Up @@ -235,6 +236,10 @@ function esbuildScanPlugin(
js += content + '\n'
}
}
// empty singleline & multiline comments to avoid matching comments
const code = js
.replace(multilineCommentsRE, '/* */')
.replace(singleCommentsRegex, '')

if (
loader.startsWith('ts') &&
Expand All @@ -247,8 +252,6 @@ function esbuildScanPlugin(
// the solution is to add `import 'x'` for every source to force
// esbuild to keep crawling due to potential side effects.
let m
// empty multiline comments to avoid matching commented out imports
const code = js.replace(multilineCommentsRE, '/* */')
while ((m = importsRE.exec(code)) != null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === importsRE.lastIndex) {
Expand All @@ -258,11 +261,11 @@ function esbuildScanPlugin(
}
}

if (!js.includes(`export default`)) {
if (!code.includes(`export default`)) {
js += `\nexport default {}`
}

if (js.includes('import.meta.glob')) {
if (code.includes('import.meta.glob')) {
return {
// transformGlob already transforms to js
loader: 'js',
Expand Down