Skip to content

Commit f5c0f16

Browse files
fix(vite): source mapping locations (#3624)
Mappings were wrong for a few reasons: - Missing source map generation in one babel transform - setting `compact: false` causes vite to generate incorrect mappings in `vite:import-analysis` - stack traces need to be mapped by vite Fixes #3609
1 parent 399dde7 commit f5c0f16

File tree

13 files changed

+81
-7
lines changed

13 files changed

+81
-7
lines changed

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"vendor": true,
23
"nodeModulesDir": "manual",
34
"workspace": [
45
"./packages/*",
@@ -8,7 +9,7 @@
89
"demo": "deno task --cwd=packages/plugin-vite demo",
910
"demo:build": "deno task --cwd=packages/plugin-vite demo:build",
1011
"demo:start": "deno task --cwd=packages/plugin-vite demo:start",
11-
"test": "deno test -A --parallel",
12+
"test": "deno test -A",
1213
"www": "deno task --cwd=www dev",
1314
"build-www": "deno task --cwd=www build",
1415
"screenshot": "deno run -A www/utils/screenshot.ts",

packages/fresh/src/app.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ export let setBuildCache: <State>(
154154
cache: BuildCache<State>,
155155
mode: "development" | "production",
156156
) => void;
157+
export let setErrorInterceptor: <State>(
158+
app: App<State>,
159+
fn: (err: unknown) => void,
160+
) => void;
161+
162+
const NOOP = () => {};
157163

158164
/**
159165
* Create an application instance that passes the incoming `Request`
@@ -162,6 +168,7 @@ export let setBuildCache: <State>(
162168
export class App<State> {
163169
#getBuildCache: () => BuildCache<State> | null = () => null;
164170
#commands: Command<State>[] = [];
171+
#onError: (err: unknown) => void = NOOP;
165172

166173
static {
167174
getBuildCache = (app) => app.#getBuildCache();
@@ -170,6 +177,9 @@ export class App<State> {
170177
app.config.mode = mode;
171178
app.#getBuildCache = () => cache;
172179
};
180+
setErrorInterceptor = (app, fn) => {
181+
app.#onError = fn;
182+
};
173183
}
174184

175185
/**
@@ -432,7 +442,7 @@ export class App<State> {
432442
try {
433443
if (handlers.length === 0) return await next();
434444

435-
const result = await runMiddlewares(handlers, ctx);
445+
const result = await runMiddlewares(handlers, ctx, this.#onError);
436446
if (!(result instanceof Response)) {
437447
throw new Error(
438448
`Expected a "Response" instance to be returned, but got: ${result}`,

packages/fresh/src/internals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "@std/path";
22

3-
export { setBuildCache } from "./app.ts";
3+
export { setBuildCache, setErrorInterceptor } from "./app.ts";
44
export { IslandPreparer, ProdBuildCache } from "./build_cache.ts";
55
export { path };
66
export { ASSET_CACHE_BUST_KEY } from "./constants.ts";

packages/fresh/src/middlewares/mod.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export type MaybeLazyMiddleware<State> = (
9191
export async function runMiddlewares<State>(
9292
middlewares: MaybeLazyMiddleware<State>[],
9393
ctx: Context<State>,
94+
onError?: (err: unknown) => void,
9495
): Promise<Response> {
9596
return await tracer.startActiveSpan("middlewares", {
9697
attributes: { "fresh.middleware.count": middlewares.length },
@@ -117,7 +118,13 @@ export async function runMiddlewares<State>(
117118

118119
return result;
119120
} catch (err) {
120-
ctx.error = err;
121+
if (ctx.error !== err) {
122+
ctx.error = err;
123+
124+
if (onError !== undefined) {
125+
onError(err);
126+
}
127+
}
121128
throw err;
122129
} finally {
123130
internals.app = prevApp;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { define } from "../utils.ts";
2+
3+
export default define.page((props) => {
4+
if (props.error instanceof Error) {
5+
return <pre id="err">{String(props.error?.stack)}</pre>;
6+
}
7+
8+
return <pre id="err">{String(props.error)}</pre>;
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { define } from "../../utils.ts";
2+
3+
export const handler = define.handlers({
4+
GET() {
5+
throw new Error("FAIL");
6+
},
7+
});

packages/plugin-vite/demo/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createDefine } from "@fresh/core";
2+
3+
// deno-lint-ignore no-explicit-any
4+
export const define = createDefine<any>();

packages/plugin-vite/src/mod.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
180180
return;
181181
}
182182

183+
// Ignore this warnings
184+
if (warning.code === "THIS_IS_UNDEFINED") {
185+
return;
186+
}
187+
188+
// Ignore falsy source map errors
189+
if (warning.code === "SOURCEMAP_ERROR") {
190+
return;
191+
}
192+
183193
return handler(warning);
184194
},
185195
input: {

packages/plugin-vite/src/plugins/deno.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ function babelTransform(
384384
const result = babel.transformSync(code, {
385385
filename: id,
386386
babelrc: false,
387-
sourceMaps: "inline",
387+
sourceMaps: "both",
388388
presets: presets,
389389
plugins: [httpAbsolute(url)],
390-
compact: true,
390+
compact: false,
391391
});
392392

393393
if (result !== null && result.code) {

packages/plugin-vite/src/plugins/dev_server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export function devServer(): Plugin[] {
7272
try {
7373
const mod = await server.ssrLoadModule("fresh:server_entry");
7474
const req = createRequest(nodeReq, nodeRes);
75+
mod.setErrorInterceptor((err: unknown) => {
76+
if (err instanceof Error) {
77+
server.ssrFixStacktrace(err);
78+
}
79+
});
80+
7581
const res = (await mod.default.fetch(req)) as Response;
7682

7783
// Collect css eagerly to avoid FOUC. This is a workaround for
@@ -101,6 +107,9 @@ export function devServer(): Plugin[] {
101107

102108
await sendResponse(nodeRes, res);
103109
} catch (err) {
110+
if (err instanceof Error) {
111+
server.ssrFixStacktrace(err);
112+
}
104113
return next(err);
105114
}
106115
});

0 commit comments

Comments
 (0)