Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vite): full source-map support for ssr #3300

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ interface ViteDevServer {
url: string,
options?: { isolated?: boolean }
): Promise<Record<string, any>>
/**
* Fix ssr error stacktrace.
*/
ssrFixStacktrace(e: Error): void
/**
* Start the server.
*/
Expand Down
3 changes: 0 additions & 3 deletions docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ app.use('*', async (req, res) => {
// 6. Send the rendered HTML back.
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
// If an error is caught, let vite fix the stracktrace so it maps back to
// your actual source code.
vite.ssrFixStacktrace(e)
console.error(e)
res.status(500).end(e.message)
}
Expand Down
1 change: 0 additions & 1 deletion packages/playground/ssr-react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async function createServer(

res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
!isProd && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)
}
Expand Down
1 change: 0 additions & 1 deletion packages/playground/ssr-vue/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ async function createServer(

res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { TransformOptions as EsbuildTransformOptions } from 'esbuild'
import { DepOptimizationMetadata, optimizeDeps } from '../optimizer'
import { ssrLoadModule } from '../ssr/ssrModuleLoader'
import { resolveSSRExternal } from '../ssr/ssrExternal'
import { ssrRewriteStacktrace } from '../ssr/ssrStacktrace'
import { createMissingImporterRegisterFn } from '../optimizer/registerMissing'
import { printServerUrls } from '../logger'
import { resolveHostname } from '../utils'
Expand Down Expand Up @@ -248,6 +247,7 @@ export interface ViteDevServer {
ssrLoadModule(url: string): Promise<Record<string, any>>
/**
* Fix ssr error stacktrace
* @deprecated we now rely on source-map support for ssr.
*/
ssrFixStacktrace(e: Error): void
/**
Expand Down Expand Up @@ -357,9 +357,9 @@ export async function createServer(
return ssrLoadModule(url, server)
},
ssrFixStacktrace(e) {
if (e.stack) {
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
}
this.config.logger.warn(
`ViteDevServer.ssrFixStacktrace is deprecated. Use "node --enable-source-maps" or "node -r source-map-support/register" instead.`
)
},
listen(port?: number, isRestart?: boolean) {
return startServer(server, port, isRestart)
Expand Down
10 changes: 1 addition & 9 deletions packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IncomingMessage, ServerResponse } from 'http'
import getEtag from 'etag'
import { SourceMap } from 'rollup'
import { genSourceMapString } from './sourcemap'

const isDebug = process.env.DEBUG

Expand Down Expand Up @@ -43,12 +44,3 @@ export function send(
res.statusCode = 200
return res.end(content)
}

function genSourceMapString(map: SourceMap | string | undefined) {
if (typeof map !== 'string') {
map = JSON.stringify(map)
}
return `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(
map
).toString('base64')}`
}
12 changes: 12 additions & 0 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import path from 'path'
import { SourceMap } from 'rollup'

export async function injectSourcesContent(
map: { sources: string[]; sourcesContent?: string[]; sourceRoot?: string },
Expand All @@ -18,3 +19,14 @@ export async function injectSourcesContent(
})
)
}

export function genSourceMapString(
map: SourceMap | string | undefined
): string {
if (typeof map !== 'string') {
map = JSON.stringify(map)
}
return `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(
map
).toString('base64')}`
}
57 changes: 28 additions & 29 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import vm from 'vm'
import path from 'path'
import { ViteDevServer } from '..'
import { cleanUrl, resolveFrom, unwrapId } from '../utils'
import { ssrRewriteStacktrace } from './ssrStacktrace'
import {
ssrExportAllKey,
ssrModuleExportsKey,
Expand All @@ -11,6 +11,7 @@ import {
ssrDynamicImportKey
} from './ssrTransform'
import { transformRequest } from '../server/transformRequest'
import { genSourceMapString } from '../server/sourcemap'

interface SSRContext {
global: NodeJS.Global
Expand Down Expand Up @@ -123,34 +124,32 @@ async function instantiateModule(
}
}

try {
new Function(
`global`,
ssrModuleExportsKey,
ssrImportMetaKey,
ssrImportKey,
ssrDynamicImportKey,
ssrExportAllKey,
result.code + `\n//# sourceURL=${mod.url}`
)(
context.global,
ssrModule,
ssrImportMeta,
ssrImport,
ssrDynamicImport,
ssrExportAll
)
} catch (e) {
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
server.config.logger.error(
`Error when evaluating SSR module ${url}:\n${e.stack}`,
{
timestamp: true,
clear: server.config.clearScreen
}
)
throw e
}
vm.runInNewContext(
result.code +
genSourceMapString(
result.map
? mod.file
? {
...result.map,
// When we have a file, we can let node handle sourcesContent
sources: [mod.file],
sourcesContent: []
}
: result.map
: undefined
),
{
global: context.global,
[ssrModuleExportsKey]: ssrModule,
[ssrImportMetaKey]: ssrImportMeta,
[ssrImportKey]: ssrImport,
[ssrDynamicImportKey]: ssrDynamicImport,
[ssrExportAllKey]: ssrExportAll
},
{
filename: mod.file || mod.url
}
)

mod.ssrModule = Object.freeze(ssrModule)
return ssrModule
Expand Down
58 changes: 0 additions & 58 deletions packages/vite/src/node/ssr/ssrStacktrace.ts

This file was deleted.