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 https://github.com/denoland/fresh/issues/3609
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"vendor": true,
|
||||||
"nodeModulesDir": "manual",
|
"nodeModulesDir": "manual",
|
||||||
"workspace": [
|
"workspace": [
|
||||||
"./packages/*",
|
"./packages/*",
|
||||||
@@ -8,7 +9,7 @@
|
|||||||
"demo": "deno task --cwd=packages/plugin-vite demo",
|
"demo": "deno task --cwd=packages/plugin-vite demo",
|
||||||
"demo:build": "deno task --cwd=packages/plugin-vite demo:build",
|
"demo:build": "deno task --cwd=packages/plugin-vite demo:build",
|
||||||
"demo:start": "deno task --cwd=packages/plugin-vite demo:start",
|
"demo:start": "deno task --cwd=packages/plugin-vite demo:start",
|
||||||
"test": "deno test -A --parallel",
|
"test": "deno test -A",
|
||||||
"www": "deno task --cwd=www dev",
|
"www": "deno task --cwd=www dev",
|
||||||
"build-www": "deno task --cwd=www build",
|
"build-www": "deno task --cwd=www build",
|
||||||
"screenshot": "deno run -A www/utils/screenshot.ts",
|
"screenshot": "deno run -A www/utils/screenshot.ts",
|
||||||
|
|||||||
@@ -154,6 +154,12 @@ export let setBuildCache: <State>(
|
|||||||
cache: BuildCache<State>,
|
cache: BuildCache<State>,
|
||||||
mode: "development" | "production",
|
mode: "development" | "production",
|
||||||
) => void;
|
) => void;
|
||||||
|
export let setErrorInterceptor: <State>(
|
||||||
|
app: App<State>,
|
||||||
|
fn: (err: unknown) => void,
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
const NOOP = () => {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an application instance that passes the incoming `Request`
|
* Create an application instance that passes the incoming `Request`
|
||||||
@@ -162,6 +168,7 @@ export let setBuildCache: <State>(
|
|||||||
export class App<State> {
|
export class App<State> {
|
||||||
#getBuildCache: () => BuildCache<State> | null = () => null;
|
#getBuildCache: () => BuildCache<State> | null = () => null;
|
||||||
#commands: Command<State>[] = [];
|
#commands: Command<State>[] = [];
|
||||||
|
#onError: (err: unknown) => void = NOOP;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
getBuildCache = (app) => app.#getBuildCache();
|
getBuildCache = (app) => app.#getBuildCache();
|
||||||
@@ -170,6 +177,9 @@ export class App<State> {
|
|||||||
app.config.mode = mode;
|
app.config.mode = mode;
|
||||||
app.#getBuildCache = () => cache;
|
app.#getBuildCache = () => cache;
|
||||||
};
|
};
|
||||||
|
setErrorInterceptor = (app, fn) => {
|
||||||
|
app.#onError = fn;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -432,7 +442,7 @@ export class App<State> {
|
|||||||
try {
|
try {
|
||||||
if (handlers.length === 0) return await next();
|
if (handlers.length === 0) return await next();
|
||||||
|
|
||||||
const result = await runMiddlewares(handlers, ctx);
|
const result = await runMiddlewares(handlers, ctx, this.#onError);
|
||||||
if (!(result instanceof Response)) {
|
if (!(result instanceof Response)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Expected a "Response" instance to be returned, but got: ${result}`,
|
`Expected a "Response" instance to be returned, but got: ${result}`,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
|
|
||||||
export { setBuildCache } from "./app.ts";
|
export { setBuildCache, setErrorInterceptor } from "./app.ts";
|
||||||
export { IslandPreparer, ProdBuildCache } from "./build_cache.ts";
|
export { IslandPreparer, ProdBuildCache } from "./build_cache.ts";
|
||||||
export { path };
|
export { path };
|
||||||
export { ASSET_CACHE_BUST_KEY } from "./constants.ts";
|
export { ASSET_CACHE_BUST_KEY } from "./constants.ts";
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ export type MaybeLazyMiddleware<State> = (
|
|||||||
export async function runMiddlewares<State>(
|
export async function runMiddlewares<State>(
|
||||||
middlewares: MaybeLazyMiddleware<State>[],
|
middlewares: MaybeLazyMiddleware<State>[],
|
||||||
ctx: Context<State>,
|
ctx: Context<State>,
|
||||||
|
onError?: (err: unknown) => void,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
return await tracer.startActiveSpan("middlewares", {
|
return await tracer.startActiveSpan("middlewares", {
|
||||||
attributes: { "fresh.middleware.count": middlewares.length },
|
attributes: { "fresh.middleware.count": middlewares.length },
|
||||||
@@ -117,7 +118,13 @@ export async function runMiddlewares<State>(
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ctx.error = err;
|
if (ctx.error !== err) {
|
||||||
|
ctx.error = err;
|
||||||
|
|
||||||
|
if (onError !== undefined) {
|
||||||
|
onError(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
throw err;
|
throw err;
|
||||||
} finally {
|
} finally {
|
||||||
internals.app = prevApp;
|
internals.app = prevApp;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { define } from "../utils.ts";
|
||||||
|
|
||||||
|
export default define.page((props) => {
|
||||||
|
if (props.error instanceof Error) {
|
||||||
|
return <pre id="err">{String(props.error?.stack)}</pre>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <pre id="err">{String(props.error)}</pre>;
|
||||||
|
});
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { define } from "../../utils.ts";
|
||||||
|
|
||||||
|
export const handler = define.handlers({
|
||||||
|
GET() {
|
||||||
|
throw new Error("FAIL");
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { createDefine } from "@fresh/core";
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
export const define = createDefine<any>();
|
||||||
@@ -180,6 +180,16 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignore this warnings
|
||||||
|
if (warning.code === "THIS_IS_UNDEFINED") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore falsy source map errors
|
||||||
|
if (warning.code === "SOURCEMAP_ERROR") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return handler(warning);
|
return handler(warning);
|
||||||
},
|
},
|
||||||
input: {
|
input: {
|
||||||
|
|||||||
@@ -384,10 +384,10 @@ function babelTransform(
|
|||||||
const result = babel.transformSync(code, {
|
const result = babel.transformSync(code, {
|
||||||
filename: id,
|
filename: id,
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
sourceMaps: "inline",
|
sourceMaps: "both",
|
||||||
presets: presets,
|
presets: presets,
|
||||||
plugins: [httpAbsolute(url)],
|
plugins: [httpAbsolute(url)],
|
||||||
compact: true,
|
compact: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result !== null && result.code) {
|
if (result !== null && result.code) {
|
||||||
|
|||||||
@@ -72,6 +72,12 @@ export function devServer(): Plugin[] {
|
|||||||
try {
|
try {
|
||||||
const mod = await server.ssrLoadModule("fresh:server_entry");
|
const mod = await server.ssrLoadModule("fresh:server_entry");
|
||||||
const req = createRequest(nodeReq, nodeRes);
|
const req = createRequest(nodeReq, nodeRes);
|
||||||
|
mod.setErrorInterceptor((err: unknown) => {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
server.ssrFixStacktrace(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const res = (await mod.default.fetch(req)) as Response;
|
const res = (await mod.default.fetch(req)) as Response;
|
||||||
|
|
||||||
// Collect css eagerly to avoid FOUC. This is a workaround for
|
// Collect css eagerly to avoid FOUC. This is a workaround for
|
||||||
@@ -101,6 +107,9 @@ export function devServer(): Plugin[] {
|
|||||||
|
|
||||||
await sendResponse(nodeRes, res);
|
await sendResponse(nodeRes, res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
server.ssrFixStacktrace(err);
|
||||||
|
}
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -50,9 +50,10 @@ export function patches(): Plugin {
|
|||||||
const res = babel.transformSync(code, {
|
const res = babel.transformSync(code, {
|
||||||
filename: id,
|
filename: id,
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
compact: true,
|
compact: false,
|
||||||
plugins,
|
plugins,
|
||||||
presets,
|
presets,
|
||||||
|
sourceMaps: "both",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res?.code) {
|
if (res?.code) {
|
||||||
|
|||||||
@@ -89,7 +89,12 @@ export function registerStaticFile(prepared) {
|
|||||||
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
code = `import "preact/debug";
|
code = `import "preact/debug";
|
||||||
|
import { setErrorInterceptor as internalErrorIntercept } from "fresh/internal";
|
||||||
${code}
|
${code}
|
||||||
|
|
||||||
|
export function setErrorInterceptor(fn) {
|
||||||
|
internalErrorIntercept(app, fn);
|
||||||
|
}
|
||||||
if (import.meta.hot) import.meta.hot.accept();`;
|
if (import.meta.hot) import.meta.hot.accept();`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -523,3 +523,14 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "vite dev - source mapped stack traces",
|
||||||
|
fn: async () => {
|
||||||
|
const res = await fetch(`${demoServer.address()}/tests/throw`);
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toContain("throw.tsx:5:11");
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user