From 4c6028d5407340cd31c17fd81fac149e1c70b837 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 21 Nov 2024 16:51:21 +0900 Subject: [PATCH] fix: fix module runner sourcemap --- .../vite/src/node/server/environments/rolldown.ts | 13 ++++++++----- playground/rolldown-dev-ssr/src/entry-server.tsx | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/server/environments/rolldown.ts b/packages/vite/src/node/server/environments/rolldown.ts index 29de3e9f8f45bf..6649375456a757 100644 --- a/packages/vite/src/node/server/environments/rolldown.ts +++ b/packages/vite/src/node/server/environments/rolldown.ts @@ -267,7 +267,10 @@ class RolldownEnvironment extends DevEnvironment { if (this.name === 'client') { ctx.server.ws.send('rolldown:hmr', result) } else { - this.getRunner().evaluate(result[1].toString()) + this.getRunner().evaluate( + result[1].toString(), + path.join(this.outDir, result[0]), + ) } console.timeEnd(`[rolldown:${this.name}:hmr]`) } else { @@ -286,7 +289,7 @@ class RolldownEnvironment extends DevEnvironment { const filepath = path.join(this.outDir, output.fileName) this.runner = new RolldownModuleRunner() const code = fs.readFileSync(filepath, 'utf-8') - this.runner.evaluate(code) + this.runner.evaluate(code, filepath) } return this.runner } @@ -323,13 +326,12 @@ class RolldownModuleRunner { return mod.exports } - evaluate(code: string) { + evaluate(code: string, sourceURL: string) { const context = { self: this.context, ...this.context, } - // TODO: sourcemap not working? - // extract sourcemap + // extract sourcemap and move to the bottom const sourcemap = code.match(/^\/\/# sourceMappingURL=.*/m)?.[0] ?? '' if (sourcemap) { code = code.replace(sourcemap, '') @@ -341,6 +343,7 @@ self.__toCommonJS = __toCommonJS; self.__export = __export; self.__toESM = __toESM; }} +//# sourceURL=${sourceURL} //# sourceMappingSource=rolldown-module-runner ${sourcemap} ` diff --git a/playground/rolldown-dev-ssr/src/entry-server.tsx b/playground/rolldown-dev-ssr/src/entry-server.tsx index 7046f1a9670977..94557f7ebfcfc3 100644 --- a/playground/rolldown-dev-ssr/src/entry-server.tsx +++ b/playground/rolldown-dev-ssr/src/entry-server.tsx @@ -7,7 +7,6 @@ const handler: Connect.SimpleHandleFunction = (req, res) => { const url = new URL(req.url ?? '/', 'https://vite.dev') console.log(`[SSR] ${req.method} ${url.pathname}`) if (url.pathname === '/crash-ssr') { - // TODO: source map and stacktrace throwError() } const ssrHtml = ReactDOMServer.renderToString()