diff --git a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 706998c288fa50..f8f937c628ba68 100644 --- a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -87,3 +87,7 @@ test('import from hidden dir', async () => { test('import optimize-excluded package that imports optimized-included package', async () => { expect(await page.textContent('.nested-include')).toBe('nested-include') }) + +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 dfb274656f1baf..62541e0525a9b4 100644 --- a/packages/playground/optimize-deps/index.html +++ b/packages/playground/optimize-deps/index.html @@ -53,6 +53,15 @@

Dep from hidden dir

Nested include

Module path:
+

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 280776b2e7d38d..f530b40d12764a 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 @@ -149,7 +147,6 @@ export const commentRE = // const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im -const contextRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im function esbuildScanPlugin( config: ResolvedConfig, @@ -189,7 +186,8 @@ function esbuildScanPlugin( return { name: 'vite:dep-scan', setup(build) { - const localScripts: Record = {} + const scripts: Record = {} + let scriptId = 0 // external urls build.onResolve({ filter: externalRE }, ({ path }) => ({ @@ -208,12 +206,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 ----------------------------------- @@ -264,37 +262,40 @@ 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 (`