-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ssr): load sourcemaps alongside modules (#11780)
Co-authored-by: Justice Almanzar <[email protected]>
- Loading branch information
1 parent
4538bfe
commit be95050
Showing
5 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function error() { | ||
throw new Error('e') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { createServer } from 'vite' | ||
|
||
const isSourceMapEnabled = process.argv[2] === 'true' | ||
process.setSourceMapsEnabled(isSourceMapEnabled) | ||
console.log('# sourcemaps enabled:', isSourceMapEnabled) | ||
|
||
const version = (() => { | ||
const m = process.version.match(/^v(\d+)\.(\d+)\.\d+$/) | ||
if (!m) throw new Error(`Failed to parse version: ${process.version}`) | ||
|
||
return { major: +m[1], minor: +m[2] } | ||
})() | ||
|
||
// https://github.com/nodejs/node/pull/43428 | ||
const isFunctionSourceMapSupported = | ||
(version.major === 16 && version.minor >= 17) || | ||
(version.major === 18 && version.minor >= 6) || | ||
version.major >= 19 | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
const isTest = process.env.VITEST | ||
|
||
const vite = await createServer({ | ||
root: __dirname, | ||
logLevel: isTest ? 'error' : 'info', | ||
server: { | ||
middlewareMode: true, | ||
}, | ||
appType: 'custom', | ||
}) | ||
|
||
const mod = await vite.ssrLoadModule('/src/error.js') | ||
try { | ||
mod.error() | ||
} catch (e) { | ||
// this should not be called | ||
// when sourcemap support for `new Function` is supported and sourcemap is enabled | ||
// because the stacktrace is already rewritten by Node.js | ||
if (!(isSourceMapEnabled && isFunctionSourceMapSupported)) { | ||
vite.ssrFixStacktrace(e) | ||
} | ||
console.log(e) | ||
} | ||
|
||
await vite.close() |