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:
@@ -42,6 +42,13 @@ const builder = new Builder({
|
|||||||
routeDir?: string;
|
routeDir?: string;
|
||||||
// File paths which should be ignored
|
// File paths which should be ignored
|
||||||
ignore?: RegExp[];
|
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
@@ -2,7 +2,7 @@ import { App, type ListenOptions, setBuildCache } from "../app.ts";
|
|||||||
import { fsAdapter } from "../fs.ts";
|
import { fsAdapter } from "../fs.ts";
|
||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
import * as colors from "@std/fmt/colors";
|
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 { liveReload } from "./middlewares/live_reload.ts";
|
||||||
import {
|
import {
|
||||||
@@ -79,14 +79,21 @@ export interface BuildOptions {
|
|||||||
* File paths which should be ignored when crawling the file system.
|
* File paths which should be ignored when crawling the file system.
|
||||||
*/
|
*/
|
||||||
ignore?: RegExp[];
|
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.
|
* The final resolved Builder configuration.
|
||||||
*/
|
*/
|
||||||
export type ResolvedBuildConfig = Required<BuildOptions> & {
|
export type ResolvedBuildConfig = Required<Omit<BuildOptions, "sourceMap">> & {
|
||||||
mode: "development" | "production";
|
mode: "development" | "production";
|
||||||
buildId: string;
|
buildId: string;
|
||||||
|
sourceMap?: FreshBundleOptions["sourceMap"];
|
||||||
};
|
};
|
||||||
|
|
||||||
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
|
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],
|
ignore: options?.ignore ?? [TEST_FILE_PATTERN],
|
||||||
mode: "production",
|
mode: "production",
|
||||||
buildId: BUILD_ID,
|
buildId: BUILD_ID,
|
||||||
|
sourceMap: options?.sourceMap,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,6 +312,7 @@ export class Builder<State = any> {
|
|||||||
entryPoints,
|
entryPoints,
|
||||||
jsxImportSource,
|
jsxImportSource,
|
||||||
denoJsonPath: denoJson,
|
denoJsonPath: denoJson,
|
||||||
|
sourceMap: this.config.sourceMap,
|
||||||
});
|
});
|
||||||
|
|
||||||
const prefix = `/_fresh/js/${BUILD_ID}/`;
|
const prefix = `/_fresh/js/${BUILD_ID}/`;
|
||||||
|
|||||||
@@ -278,6 +278,63 @@ export const app = new App().fsRoutes()`,
|
|||||||
sanitizeResources: false,
|
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", () => {
|
Deno.test("specToName", () => {
|
||||||
// HTTP
|
// HTTP
|
||||||
expect(specToName("http://example.com")).toEqual("example");
|
expect(specToName("http://example.com")).toEqual("example");
|
||||||
|
|||||||
+12
-2
@@ -1,5 +1,5 @@
|
|||||||
import { denoPlugin } from "@deno/esbuild-plugin";
|
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";
|
import * as path from "@std/path";
|
||||||
|
|
||||||
export interface FreshBundleOptions {
|
export interface FreshBundleOptions {
|
||||||
@@ -11,6 +11,14 @@ export interface FreshBundleOptions {
|
|||||||
entryPoints: Record<string, string>;
|
entryPoints: Record<string, string>;
|
||||||
target: string | string[];
|
target: string | string[];
|
||||||
jsxImportSource?: 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 {
|
export interface BuildOutput {
|
||||||
@@ -48,7 +56,9 @@ export async function bundleJs(
|
|||||||
bundle: true,
|
bundle: true,
|
||||||
splitting: true,
|
splitting: true,
|
||||||
treeShaking: 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,
|
minify: !options.dev,
|
||||||
logOverride: {
|
logOverride: {
|
||||||
"suspicious-nullish-coalescing": "silent",
|
"suspicious-nullish-coalescing": "silent",
|
||||||
|
|||||||
Reference in New Issue
Block a user