diff --git a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts index f3ae6e8147b0f4..174dc77c81aaf3 100644 --- a/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -91,3 +91,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 8451d968d2745b..65b6bf1dcf5473 100644 --- a/packages/playground/optimize-deps/index.html +++ b/packages/playground/optimize-deps/index.html @@ -56,6 +56,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 280776b2e7d38d..5532294de11edd 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,7 @@ function esbuildScanPlugin( return { name: 'vite:dep-scan', setup(build) { - const localScripts: Record = {} + const scripts: Record = {} // external urls build.onResolve({ filter: externalRE }, ({ path }) => ({ @@ -208,12 +205,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 ----------------------------------- @@ -235,7 +232,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 @@ -256,6 +253,7 @@ function esbuildScanPlugin( ) { continue } + let loader: Loader = 'js' if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') { loader = lang } @@ -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 (`