Skip to content

Commit

Permalink
feat: ssr sourcemap + stacktrace fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 18, 2021
1 parent 1673fb6 commit 6cb04fa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"selfsigned": "^1.10.8",
"sirv": "^1.0.10",
"slash": "^3.0.0",
"source-map": "^0.6.1",
"source-map-support": "^0.5.19",
"strip-ansi": "^6.0.0",
"terser": "^5.5.1",
Expand Down
10 changes: 10 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { TransformOptions as EsbuildTransformOptions } from 'esbuild'
import { DepOptimizationMetadata, optimizeDeps } from '../optimizer'
import { ssrLoadModule } from './ssrModuleLoader'
import { resolveSSRExternal } from '../ssrExternal'
import { ssrRewriteStacktrace } from './ssrStacktrace'

export interface ServerOptions {
host?: string
Expand Down Expand Up @@ -187,6 +188,10 @@ export interface ViteDevServer {
* Load a given URL as an instantiated module for SSR.
*/
ssrLoadModule(url: string): Promise<Record<string, any>>
/**
* Fix ssr error stacktrace
*/
ssrFixStacktrace(e: Error): void
/**
* Start the server.
*/
Expand Down Expand Up @@ -255,6 +260,11 @@ export async function createServer(
}
return ssrLoadModule(url, server)
},
ssrFixStacktrace(e) {
if (e.stack) {
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
}
},
listen(port?: number) {
return startServer(server, port)
},
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import { ViteDevServer } from '..'
import { resolveFrom } from '../utils'
import { ssrRewriteStacktrace } from './ssrStacktrace'
import {
ssrExportAllKey,
ssrModuleExportsKey,
Expand Down Expand Up @@ -92,10 +93,10 @@ export async function ssrLoadModule(
ssrImportKey,
ssrDynamicImportKey,
ssrExportAllKey,
result.code
result.code + `\n//# sourceURL=${mod.url}`
)(ssrModule, ssrImportMeta, ssrImport, ssrDynamicImport, ssrExportAll)
} catch (e) {
// TODO source map
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
server.config.logger.error(
`Error when evaluating SSR module ${url}:\n${e.stack}`,
{
Expand Down
46 changes: 46 additions & 0 deletions packages/vite/src/node/server/ssrStacktrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SourceMapConsumer, RawSourceMap } from 'source-map'
import { ModuleGraph } from './moduleGraph'

export function ssrRewriteStacktrace(stack: string, moduleGraph: ModuleGraph) {
return stack
.split('\n')
.map((line) => {
return line.replace(
/^ {4}at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?)\)?/,
(input, varName, url, line, column) => {
if (!url) return input

const mod = moduleGraph.urlToModuleMap.get(url)
const rawSourceMap = mod?.ssrTransformResult?.map

if (!rawSourceMap) {
return input
}

const consumer = new SourceMapConsumer(
(rawSourceMap as any) as RawSourceMap
)

const pos = consumer.originalPositionFor({
// source map lines generated via new Function() in Node.js is always
// incremented by 2 - no idea why
line: Number(line) - 2,
column: Number(column),
bias: SourceMapConsumer.LEAST_UPPER_BOUND
})

if (!pos.source) {
return input
}

const source = `${pos.source}:${pos.line || 0}:${pos.column || 0}`
if (!varName || varName === 'eval') {
return ` at ${source}`
} else {
return ` at ${varName} (${source})`
}
}
)
})
.join('\n')
}
13 changes: 11 additions & 2 deletions packages/vite/src/node/server/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'estree'
import { extract_names as extractNames } from 'periscopic'
import { walk as eswalk } from 'estree-walker'
import merge from 'merge-source-map'

type Node = _Node & {
start: number
Expand Down Expand Up @@ -157,10 +158,18 @@ export async function ssrTransform(
}
})

let map = s.generateMap({ hires: true })
if (inMap) {
map = merge(inMap, {
...map,
sources: inMap.sources,
sourcesContent: inMap.sourcesContent
}) as SourceMap
}

return {
code: s.toString(),
// TODO handle inMap
map: null, //s.generateMap({ hires: true }),
map,
deps: [...deps]
}
}
Expand Down

0 comments on commit 6cb04fa

Please sign in to comment.