Skip to content

Commit

Permalink
fix(scan): correctly resolve virtual modules (#5711)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 17, 2021
1 parent 9666446 commit 01f9b16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
39 changes: 16 additions & 23 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
dataUrlRE,
multilineCommentsRE,
singlelineCommentsRE,
virtualModuleRE
virtualModuleRE,
virtualModulePrefix
} from '../utils'
import {
createPluginContainer,
Expand Down Expand Up @@ -189,7 +190,7 @@ function esbuildScanPlugin(
return {
name: 'vite:dep-scan',
setup(build) {
const moduleScripts: Record<string, OnLoadResult> = {}
const localScripts: Record<string, OnLoadResult> = {}

// external urls
build.onResolve({ filter: externalRE }, ({ path }) => ({
Expand All @@ -203,18 +204,18 @@ function esbuildScanPlugin(
external: true
}))

build.onResolve(
{ filter: virtualModuleRE },
async ({ path, importer }) => {
return {
path: await resolve(
path.substring('virtual-module:'.length),
importer
),
namespace: 'html'
}
// local scripts (`<script>` in Svelte and `<script setup>` in Vue)
build.onResolve({ filter: virtualModuleRE }, ({ path }) => {
return {
// strip prefix to get valid filesystem path so esbuild can resolve imports in the file
path: path.replace(virtualModulePrefix, ''),
namespace: 'local-script'
}
)
})

build.onLoad({ filter: /.*/, namespace: 'local-script' }, ({ path }) => {
return localScripts[path]
})

// html types: extract script contents -----------------------------------
build.onResolve({ filter: htmlTypesRE }, async ({ path, importer }) => {
Expand All @@ -224,13 +225,6 @@ function esbuildScanPlugin(
}
})

build.onLoad(
{ filter: virtualModuleRE, namespace: 'html' },
async ({ path }) => {
return moduleScripts[path]
}
)

// extract scripts inside HTML-like files and treat it as a js module
build.onLoad(
{ filter: htmlTypesRE, namespace: 'html' },
Expand Down Expand Up @@ -282,12 +276,11 @@ function esbuildScanPlugin(
(path.endsWith('.vue') && setupRE.test(raw)) ||
(path.endsWith('.svelte') && context !== 'module')
) {
const id = `virtual-module:${path}`
moduleScripts[id] = {
localScripts[path] = {
loader,
contents: content
}
js += `import '${id}';\n`
js += `import '${virtualModulePrefix}${path}';\n`
} else {
js += content + '\n'
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ export const isExternalUrl = (url: string): boolean => externalRE.test(url)
export const dataUrlRE = /^\s*data:/i
export const isDataUrl = (url: string): boolean => dataUrlRE.test(url)

export const virtualModuleRE = /virtual-module:.*/
export const virtualModuleRE = /^virtual-module:.*/
export const virtualModulePrefix = 'virtual-module:'

const knownJsSrcRE = /\.((j|t)sx?|mjs|vue|marko|svelte|astro)($|\?)/
export const isJSRequest = (url: string): boolean => {
Expand Down

0 comments on commit 01f9b16

Please sign in to comment.