Skip to content

Commit

Permalink
feat: set meta.filename for modules loaded from disk
Browse files Browse the repository at this point in the history
This provides an efficient heuristic for knowing if a module is virtual.
  • Loading branch information
aleclarson committed Oct 28, 2021
1 parent 7e9b058 commit 9ee56ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugins/loadFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export function loadFallbackPlugin(): Plugin {
return {
name: 'vite:load-fallback',
async load(id) {
let filename: string
let code: string
try {
// if we don't add `await` here, we couldn't catch the error in readFile
return await fs.readFile(cleanUrl(id), 'utf-8')
code = await fs.readFile((filename = cleanUrl(id)), 'utf-8')
} catch (e) {
return fs.readFile(id, 'utf-8')
// Try unclean `id` to handle rare case where the file path
// contains the # character.
code = await fs.readFile((filename = id), 'utf-8')
}
return { code, meta: { filename } }
}
}
}
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ async function doTransform(
const mod = await moduleGraph.ensureEntryFromUrl(url)
ensureWatchedFile(watcher, mod.file, root)

// add meta.filename if loaded from disk
if (loadResult == null) {
mod.meta = { ...mod.meta, filename: mod.file }
}

// transform
const transformStart = isDebug ? performance.now() : 0
const transformResult = await pluginContainer.transform(code, id, {
Expand Down

0 comments on commit 9ee56ab

Please sign in to comment.