diff --git a/docs/canary/concepts/builder.md b/docs/canary/concepts/builder.md index e9c3dd1c..33a2d82f 100644 --- a/docs/canary/concepts/builder.md +++ b/docs/canary/concepts/builder.md @@ -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; + }; }) ``` diff --git a/src/dev/builder.ts b/src/dev/builder.ts index 68f22470..f826ff8e 100644 --- a/src/dev/builder.ts +++ b/src/dev/builder.ts @@ -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 & { +export type ResolvedBuildConfig = Required> & { 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 { ignore: options?.ignore ?? [TEST_FILE_PATTERN], mode: "production", buildId: BUILD_ID, + sourceMap: options?.sourceMap, }; } @@ -304,6 +312,7 @@ export class Builder { entryPoints, jsxImportSource, denoJsonPath: denoJson, + sourceMap: this.config.sourceMap, }); const prefix = `/_fresh/js/${BUILD_ID}/`; diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index f7718d05..882c1e97 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -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 = () =>

hello

`, + "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"); diff --git a/src/dev/esbuild.ts b/src/dev/esbuild.ts index 114aee03..496972aa 100644 --- a/src/dev/esbuild.ts +++ b/src/dev/esbuild.ts @@ -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; 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",