diff --git a/src/node/server/serverPluginModuleResolve.ts b/src/node/server/serverPluginModuleResolve.ts index 6e804a80748fe4..9689f4ae830d24 100644 --- a/src/node/server/serverPluginModuleResolve.ts +++ b/src/node/server/serverPluginModuleResolve.ts @@ -1,5 +1,6 @@ import path from 'path' import chalk from 'chalk' +import fs from 'fs-extra' import { ServerPlugin } from '.' import { resolveVue, cachedRead } from '../utils' import { URL } from 'url' @@ -56,9 +57,11 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, resolver }) => { const referer = ctx.get('referer') let importer: string | undefined + // this is a map file request from browser dev tool + const isMapFile = ctx.path.endsWith('.map') if (referer) { importer = new URL(referer).pathname - } else if (ctx.path.endsWith('.map')) { + } else if (isMapFile) { // for some reason Chrome doesn't provide referer for source map requests. // do our best to reverse-infer the importer. importer = ctx.path.replace(/\.map$/, '') @@ -70,6 +73,18 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, resolver }) => { return serve(id, nodeModulePath, 'node_modules') } + if (isMapFile && importer) { + // the resolveNodeModuleFile doesn't work with linked pkg + // our last try: infer from the dir of importer + const inferMapPath = path.join( + path.dirname(importerFilePath), + path.basename(ctx.path) + ) + if (fs.existsSync(inferMapPath)) { + return serve(id, inferMapPath, 'map file in linked pkg') + } + } + console.error( chalk.red( `[vite] Failed to resolve module import "${id}". ` +