Skip to content

Commit

Permalink
fix: serve source map file inside linked pkg (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 authored Jun 18, 2020
1 parent b63a146 commit f28f5b6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/node/server/serverPluginModuleResolve.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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$/, '')
Expand All @@ -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}". ` +
Expand Down

0 comments on commit f28f5b6

Please sign in to comment.