Add SSR bundle minify config

This commit is contained in:
2026-10-17 15:03:30 +01:00
parent 8a981c854a
commit ed788dff97
10 changed files with 108 additions and 8 deletions
+1
View File
@@ -991,6 +991,7 @@ export default config;
| `sitemap` | `BunextSitemapConfig` | — | Options for `bunext sitemap` — see [Sitemap](#sitemap) | | `sitemap` | `BunextSitemapConfig` | — | Options for `bunext sitemap` — see [Sitemap](#sitemap) |
| `robots` | `BunextRobotsConfig \| false` | — | robots.txt options for `bunext sitemap`, or `false` to skip it — see [robots.txt](#robotstxt) | | `robots` | `BunextRobotsConfig \| false` | — | robots.txt options for `bunext sitemap`, or `false` to skip it — see [robots.txt](#robotstxt) |
| `disable_seo` | `boolean` | `false` | Keep the whole site out of search engines (e.g. preview deployments) — see [Disabling SEO](#disabling-seo) | | `disable_seo` | `boolean` | `false` | Keep the whole site out of search engines (e.g. preview deployments) — see [Disabling SEO](#disabling-seo) |
| `minify_ssr_bundle` | `boolean` | `true` | Minify the SSR bundle. SSR output is served from local disk, not downloaded by browsers, so set `false` to cut build memory on constrained hosts. Never minified in dev |
### Middleware ### Middleware
+5 -2
View File
@@ -12,7 +12,8 @@ import { evictDeletedModules } from "../../utils/import-fresh";
import { writeFileSync } from "fs"; import { writeFileSync } from "fs";
import path from "path"; import path from "path";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import { logBuildErrorsOnce, } from "../../utils/build-error-log"; import { logBuildErrorsOnce } from "../../utils/build-error-log";
import shouldMinifySSR from "../../utils/should-minify-ssr";
const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames(); const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames();
// Current virtual entry contents. The SSR virtual plugin resolves its // Current virtual entry contents. The SSR virtual plugin resolves its
// onLoad contents lazily from this map on every rebuild, so edits to // onLoad contents lazily from this map on every rebuild, so edits to
@@ -124,7 +125,9 @@ async function doSSRRebuild(params) {
entryPoints, entryPoints,
outdir: BUNX_CWD_MODULE_CACHE_DIR, outdir: BUNX_CWD_MODULE_CACHE_DIR,
bundle: true, bundle: true,
minify: !dev, minify: shouldMinifySSR({
minify_ssr_bundle: config.minify_ssr_bundle,
}),
format: "esm", format: "esm",
target: "esnext", target: "esnext",
platform: "node", platform: "node",
+7
View File
@@ -114,6 +114,13 @@ export type BunextConfig = {
* aborted and answered with a 500. Defaults to 30000. * aborted and answered with a 500. Defaults to 30000.
*/ */
ssr_render_timeout_ms?: number; ssr_render_timeout_ms?: number;
/**
* Minify the SSR bundle. The SSR output is read off local disk by the
* server and is never downloaded by a browser, so minifying it only costs
* build memory. Set to `false` on memory-constrained hosts. Never minified
* in dev; defaults to `true` in production.
*/
minify_ssr_bundle?: boolean;
}; };
export type BunextConfigMiddlewareParams = { export type BunextConfigMiddlewareParams = {
req: Request; req: Request;
+17
View File
@@ -0,0 +1,17 @@
type Params = {
/**
* The value of `minify_ssr_bundle` in `bunext.config.ts`
* (`global.BUNEXT_CONFIG.minify_ssr_bundle`).
*/
minify_ssr_bundle?: boolean;
};
/**
* Whether the SSR bundle should be minified.
*
* The SSR output is read off local disk by the server and is never downloaded
* by a browser, so minifying it only costs build memory. Dev is never minified.
* In production the bundle is minified unless `minify_ssr_bundle` is explicitly
* set to `false` in `bunext.config.ts`.
*/
export default function shouldMinifySSR(params?: Params): boolean;
export {};
+14
View File
@@ -0,0 +1,14 @@
import isDevelopment from "./is-development";
/**
* Whether the SSR bundle should be minified.
*
* The SSR output is read off local disk by the server and is never downloaded
* by a browser, so minifying it only costs build memory. Dev is never minified.
* In production the bundle is minified unless `minify_ssr_bundle` is explicitly
* set to `false` in `bunext.config.ts`.
*/
export default function shouldMinifySSR(params = {}) {
if (isDevelopment())
return false;
return params.minify_ssr_bundle !== false;
}
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"version": "1.3.3", "version": "1.3.4",
"main": "dist/index.js", "main": "dist/index.js",
"module": "index.ts", "module": "index.ts",
"dependencies": { "dependencies": {
@@ -0,0 +1,28 @@
import { describe, it, expect, afterEach } from "bun:test";
import shouldMinifySSR from "../../utils/should-minify-ssr";
const originalEnv = process.env.NODE_ENV;
afterEach(() => {
process.env.NODE_ENV = originalEnv;
});
describe("shouldMinifySSR", () => {
it("never minifies in development", () => {
process.env.NODE_ENV = "development";
expect(shouldMinifySSR()).toBe(false);
expect(shouldMinifySSR({ minify_ssr_bundle: true })).toBe(false);
expect(shouldMinifySSR({ minify_ssr_bundle: false })).toBe(false);
});
it("minifies in production by default", () => {
process.env.NODE_ENV = "production";
expect(shouldMinifySSR()).toBe(true);
expect(shouldMinifySSR({ minify_ssr_bundle: true })).toBe(true);
});
it("skips minification when minify_ssr_bundle is false", () => {
process.env.NODE_ENV = "production";
expect(shouldMinifySSR({ minify_ssr_bundle: false })).toBe(false);
});
});
@@ -13,9 +13,8 @@ import { evictDeletedModules } from "../../utils/import-fresh";
import { writeFileSync } from "fs"; import { writeFileSync } from "fs";
import path from "path"; import path from "path";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import { import { logBuildErrorsOnce } from "../../utils/build-error-log";
logBuildErrorsOnce, import shouldMinifySSR from "../../utils/should-minify-ssr";
} from "../../utils/build-error-log";
const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames(); const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames();
@@ -174,7 +173,9 @@ async function doSSRRebuild(params?: RebuildParams) {
entryPoints, entryPoints,
outdir: BUNX_CWD_MODULE_CACHE_DIR, outdir: BUNX_CWD_MODULE_CACHE_DIR,
bundle: true, bundle: true,
minify: !dev, minify: shouldMinifySSR({
minify_ssr_bundle: config.minify_ssr_bundle,
}),
format: "esm", format: "esm",
target: "esnext", target: "esnext",
platform: "node", platform: "node",
@@ -244,4 +245,4 @@ async function doSSRRebuild(params?: RebuildParams) {
); );
} }
} }
} }
+7
View File
@@ -131,6 +131,13 @@ export type BunextConfig = {
* aborted and answered with a 500. Defaults to 30000. * aborted and answered with a 500. Defaults to 30000.
*/ */
ssr_render_timeout_ms?: number; ssr_render_timeout_ms?: number;
/**
* Minify the SSR bundle. The SSR output is read off local disk by the
* server and is never downloaded by a browser, so minifying it only costs
* build memory. Set to `false` on memory-constrained hosts. Never minified
* in dev; defaults to `true` in production.
*/
minify_ssr_bundle?: boolean;
}; };
export type BunextConfigMiddlewareParams = { export type BunextConfigMiddlewareParams = {
+22
View File
@@ -0,0 +1,22 @@
import isDevelopment from "./is-development";
type Params = {
/**
* The value of `minify_ssr_bundle` in `bunext.config.ts`
* (`global.BUNEXT_CONFIG.minify_ssr_bundle`).
*/
minify_ssr_bundle?: boolean;
};
/**
* Whether the SSR bundle should be minified.
*
* The SSR output is read off local disk by the server and is never downloaded
* by a browser, so minifying it only costs build memory. Dev is never minified.
* In production the bundle is minified unless `minify_ssr_bundle` is explicitly
* set to `false` in `bunext.config.ts`.
*/
export default function shouldMinifySSR(params: Params = {}): boolean {
if (isDevelopment()) return false;
return params.minify_ssr_bundle !== false;
}