diff --git a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 992627c6374df0..3c45287017fb44 100644 --- a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -95,3 +95,7 @@ test('import optimize-excluded package that imports optimized-included package', test('import aliased package with colon', async () => { expect(await page.textContent('.url')).toBe('vitejs.dev') }) + +test('variable names are reused in different scripts', async () => { + expect(await page.textContent('.reused-variable-names')).toBe('reused') +}) diff --git a/packages/playground/optimize-deps/index.html b/packages/playground/optimize-deps/index.html index e53e3375e79768..fc1b83a6b50c65 100644 --- a/packages/playground/optimize-deps/index.html +++ b/packages/playground/optimize-deps/index.html @@ -59,6 +59,15 @@

Nested include

Alias with colon

URL:
+

Reused variable names

+
This should show reused:
+ + + - function text(el, text) { - document.querySelector(el).textContent = text - } + + diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index f1112bd38db423..473673ce93a861 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -35,8 +35,6 @@ const debug = createDebugger('vite:deps') const htmlTypesRE = /\.(html|vue|svelte|astro)$/ -const setupRE = / blocks in vue (setup only) or svelte files, since // seemingly unused imports are dropped by esbuild when transpiling TS which @@ -193,7 +191,7 @@ function esbuildScanPlugin( return { name: 'vite:dep-scan', setup(build) { - const localScripts: Record = {} + const scripts: Record = {} // external urls build.onResolve({ filter: externalRE }, ({ path }) => ({ @@ -212,12 +210,12 @@ function esbuildScanPlugin( return { // strip prefix to get valid filesystem path so esbuild can resolve imports in the file path: path.replace(virtualModulePrefix, ''), - namespace: 'local-script' + namespace: 'script' } }) - build.onLoad({ filter: /.*/, namespace: 'local-script' }, ({ path }) => { - return localScripts[path] + build.onLoad({ filter: /.*/, namespace: 'script' }, ({ path }) => { + return scripts[path] }) // html types: extract script contents ----------------------------------- @@ -245,7 +243,7 @@ function esbuildScanPlugin( const regex = isHtml ? scriptModuleRE : scriptRE regex.lastIndex = 0 let js = '' - let loader: Loader = 'js' + let scriptId = 0 let match: RegExpExecArray | null while ((match = regex.exec(raw))) { const [, openTag, content] = match @@ -266,6 +264,7 @@ function esbuildScanPlugin( ) { continue } + let loader: Loader = 'js' if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') { loader = lang } @@ -274,39 +273,59 @@ function esbuildScanPlugin( const src = srcMatch[1] || srcMatch[2] || srcMatch[3] js += `import ${JSON.stringify(src)}\n` } else if (content.trim()) { - // There can be module scripts (`