feat: support setting production source maps on builder (#3151)

This adds a new `sourceMap` option on the `Builder` configuration. This
can be used to optionally specify how production source maps should be
generated.

See https://esbuild.github.io/api/#source-maps for more information.
This commit is contained in:
Marvin Hagemeister
2025-07-29 21:20:15 +02:00
committed by GitHub
parent c30e46ec30
commit 9d769f3cf1
4 changed files with 87 additions and 4 deletions
+7
View File
@@ -42,6 +42,13 @@ const builder = new Builder({
routeDir?: string;
// File paths which should be ignored
ignore?: RegExp[];
// Optionally generate production source maps
// See https://esbuild.github.io/api/#source-maps
sourceMap?: {
kind?: boolean | 'linked' | 'inline' | 'external' | 'both';
sourceRoot?: string;
sourcesContent?: boolean;
};
})
```
+11 -2
View File
@@ -2,7 +2,7 @@ import { App, type ListenOptions, setBuildCache } from "../app.ts";
import { fsAdapter } from "../fs.ts";
import * as path from "@std/path";
import * as colors from "@std/fmt/colors";
import { bundleJs } from "./esbuild.ts";
import { bundleJs, type FreshBundleOptions } from "./esbuild.ts";
import { liveReload } from "./middlewares/live_reload.ts";
import {
@@ -79,14 +79,21 @@ export interface BuildOptions {
* File paths which should be ignored when crawling the file system.
*/
ignore?: RegExp[];
/**
* Control if/how production source maps should be handled.
* See https://esbuild.github.io/api/#source-maps for more information.
*/
sourceMap?: FreshBundleOptions["sourceMap"];
}
/**
* The final resolved Builder configuration.
*/
export type ResolvedBuildConfig = Required<BuildOptions> & {
export type ResolvedBuildConfig = Required<Omit<BuildOptions, "sourceMap">> & {
mode: "development" | "production";
buildId: string;
sourceMap?: FreshBundleOptions["sourceMap"];
};
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
@@ -121,6 +128,7 @@ export class Builder<State = any> {
ignore: options?.ignore ?? [TEST_FILE_PATTERN],
mode: "production",
buildId: BUILD_ID,
sourceMap: options?.sourceMap,
};
}
@@ -304,6 +312,7 @@ export class Builder<State = any> {
entryPoints,
jsxImportSource,
denoJsonPath: denoJson,
sourceMap: this.config.sourceMap,
});
const prefix = `/_fresh/js/${BUILD_ID}/`;
+57
View File
@@ -278,6 +278,63 @@ export const app = new App().fsRoutes()`,
sanitizeResources: false,
});
Deno.test({
name: "Builder - source maps",
fn: async () => {
const root = path.join(import.meta.dirname!, "..", "..");
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
const tmp = _tmp.dir;
await writeFiles(tmp, {
"islands/Foo.tsx": `export const Foo = () => <h1>hello</h1>`,
"routes/index.ts": `export const handler = () => new Response("ok")`,
"main.ts": `import { App } from "fresh";
export const app = new App().fsRoutes()`,
});
const builder = new Builder({
root: tmp,
outDir: path.join(tmp, "dist"),
sourceMap: {
kind: "external",
sourceRoot: "foo",
sourcesContent: true,
},
});
await builder.build();
const assetDir = path.join(
builder.config.outDir,
"static",
"_fresh",
"js",
builder.config.buildId,
);
const entries = await Array.fromAsync(Deno.readDir(assetDir));
const map = entries.find((entry) =>
entry.isFile && entry.name.endsWith(".js.map")
);
if (!map) throw new Error(`Sourcemap not found`);
const content = await Deno.readTextFile(path.join(assetDir, map.name));
const json = JSON.parse(content) as {
version: 3;
sources: string[];
sourceRoot?: string;
sourcesContent: string[];
mappings: string;
names?: string[];
};
expect(json.sourcesContent.length).toBeGreaterThan(0);
expect(json.sourcesContent.length).toBeGreaterThan(0);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test("specToName", () => {
// HTTP
expect(specToName("http://example.com")).toEqual("example");
+12 -2
View File
@@ -1,5 +1,5 @@
import { denoPlugin } from "@deno/esbuild-plugin";
import type { Plugin as EsbuildPlugin } from "esbuild";
import type { BuildOptions, Plugin as EsbuildPlugin } from "esbuild";
import * as path from "@std/path";
export interface FreshBundleOptions {
@@ -11,6 +11,14 @@ export interface FreshBundleOptions {
entryPoints: Record<string, string>;
target: string | string[];
jsxImportSource?: string;
sourceMap?: {
/** Documentation: https://esbuild.github.io/api/#sourcemap */
kind: BuildOptions["sourcemap"];
/** Documentation: https://esbuild.github.io/api/#source-root */
sourceRoot?: BuildOptions["sourceRoot"];
/** Documentation: https://esbuild.github.io/api/#sources-content */
sourcesContent?: BuildOptions["sourcesContent"];
};
}
export interface BuildOutput {
@@ -48,7 +56,9 @@ export async function bundleJs(
bundle: true,
splitting: true,
treeShaking: true,
sourcemap: options.dev ? "linked" : false,
sourcemap: options.dev ? "linked" : options.sourceMap?.kind,
sourceRoot: options.dev ? undefined : options.sourceMap?.sourceRoot,
sourcesContent: options.dev ? undefined : options.sourceMap?.sourcesContent,
minify: !options.dev,
logOverride: {
"suspicious-nullish-coalescing": "silent",