Skip to content

Commit

Permalink
refactor(scan): use incremental scriptId for each html-like file
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverTsang committed Feb 19, 2022
1 parent 7d52fe8 commit c226e86
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -187,7 +187,7 @@ function esbuildScanPlugin(
name: 'vite:dep-scan',
setup(build) {
const scripts: Record<string, OnLoadResult> = {}
let scriptId = 0
const scriptIdMap: Record<string, number> = {}

// external urls
build.onResolve({ filter: externalRE }, ({ path }) => ({
Expand Down Expand Up @@ -274,7 +274,10 @@ function esbuildScanPlugin(
content +
(loader.startsWith('ts') ? extractImportPaths(content) : '')

const key = `${path}?id=${scriptId++}`
const scriptId = scriptIdMap[path] ?? 1
scriptIdMap[path] = scriptId + 1

const key = `${path}?id=${scriptId}`

if (contents.includes('import.meta.glob')) {
scripts[key] = {
Expand All @@ -294,7 +297,7 @@ function esbuildScanPlugin(
}
}

js += `import ${JSON.stringify(virtualModulePrefix + key)};\n`
js += `import ${JSON.stringify(virtualModulePrefix + key)}\n`
}
}

Expand All @@ -307,7 +310,7 @@ function esbuildScanPlugin(
}

return {
loader,
loader: 'js',
contents: js
}
}
Expand Down

0 comments on commit c226e86

Please sign in to comment.