Skip to content

Commit f27be9f

Browse files
authored
feat: expose minify configuration field into build (#888)
* docs: add minify docs * feat: add minify configuration into build * test: fix test
1 parent 7b855a4 commit f27be9f

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
description: Learn how to configure code minification
3+
---
4+
5+
# Minify
6+
7+
Brisa automatically optimizes your production builds by minifying JavaScript and CSS bundles. This reduces file sizes by removing whitespace, shortening variable names, and applying other safe optimizations.
8+
9+
10+
**brisa.config.ts**:
11+
12+
```ts {4}
13+
import type { Configuration } from "brisa";
14+
15+
export default {
16+
minify: true // or false to disable
17+
} satisfies Configuration;
18+
```
19+
20+
## Default Behavior (`minify` config field not specified)
21+
22+
- **Development mode** (`brisa dev`): Minification is automatically disabled for better debugging
23+
- **Production mode** (`brisa build`): Minification is automatically enabled

packages/brisa/src/types/index.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,27 @@ export type Configuration = {
12171217
*/
12181218
assetPrefix?: string;
12191219

1220+
/**
1221+
* Description:
1222+
*
1223+
* The `minify` config property controls whether the output code should be minified.
1224+
* When enabled, this removes whitespace, shortens variable names, and performs other
1225+
* optimizations to reduce the bundle size.
1226+
*
1227+
* The default value is `true` in production mode and `false` in development mode.
1228+
*
1229+
* Example:
1230+
*
1231+
* ```ts
1232+
* minify: true // enable minification
1233+
* ```
1234+
*
1235+
* Docs:
1236+
*
1237+
* - [How to use `minify`](https://brisa.build/building-your-application/configuring/minify)
1238+
*/
1239+
minify?: boolean;
1240+
12201241
/**
12211242
* Description:
12221243
*

packages/brisa/src/utils/client-build/layout-build/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const i18nCode = 3653;
2222
const brisaSize = 5738; // TODO: Reduce this size :/
2323
const webComponents = 1453;
2424
const unsuspenseSize = 213;
25-
const rpcSize = 2499; // TODO: Reduce this size
25+
const rpcSize = 2490; // TODO: Reduce this size
2626
const lazyRPCSize = 4332; // TODO: Reduce this size
2727
// lazyRPC is loaded after user interaction (action, link),
2828
// so it's not included in the initial size

packages/brisa/src/utils/compile-files/index.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,63 @@ describe('utils', () => {
540540
expect(logOutput).toContain(expected);
541541
});
542542

543+
it('should compile an app with minify=false #840', async () => {
544+
const SRC_DIR = path.join(FIXTURES, 'with-suspense-in-layout');
545+
const BUILD_DIR = path.join(SRC_DIR, 'out');
546+
const PAGES_DIR = path.join(BUILD_DIR, 'pages');
547+
const ASSETS_DIR = path.join(BUILD_DIR, 'public');
548+
const TYPES = path.join(BUILD_DIR, '_brisa', 'types.ts');
549+
const pagesClientPath = path.join(BUILD_DIR, 'pages-client');
550+
const constants = getConstants();
551+
globalThis.mockConstants = {
552+
...constants,
553+
PAGES_DIR,
554+
BUILD_DIR,
555+
IS_PRODUCTION: true,
556+
IS_DEVELOPMENT: false,
557+
SRC_DIR,
558+
ASSETS_DIR,
559+
CONFIG: {
560+
minify: false,
561+
}
562+
};
563+
564+
mockConsoleLog.mockImplementation(() => {});
565+
566+
const { success, logs } = await compileFiles();
567+
568+
expect(logs).toBeEmpty();
569+
expect(success).toBe(true);
570+
571+
const files = fs
572+
.readdirSync(BUILD_DIR)
573+
.toSorted((a, b) => a.localeCompare(b));
574+
575+
const info = constants.LOG_PREFIX.INFO;
576+
577+
const logOutput = minifyText(
578+
mockConsoleLog.mock.calls
579+
.flat()
580+
.join('\n')
581+
.replace(/chunk-\S*/g, 'chunk-hash'),
582+
);
583+
584+
// Without minify is 720 B and 1 kB instead of 452 B & 717 B (check the previous suspense test)
585+
const expected = minifyText(`
586+
${info}
587+
${info}Route | JS server | JS client (gz)
588+
${info}----------------------------------------------
589+
${info}λ /pages/index | 720 B | ${greenLog('186 B')}
590+
${info}Δ /layout | 1 kB |
591+
${info}
592+
${info}λ Server entry-points
593+
${info}Δ Layout
594+
${info}Φ JS shared by all
595+
${info}
596+
`);
597+
expect(logOutput).toContain(expected);
598+
});
599+
543600
it('should compile an app with a i18n client keys in the layout and not in the page', async () => {
544601
const SRC_DIR = path.join(FIXTURES, 'with-i18nkeys-in-layout');
545602
const BUILD_DIR = path.join(SRC_DIR, 'out');

packages/brisa/src/utils/compile-files/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default async function compileFiles() {
7777
sourcemap: IS_PRODUCTION ? undefined : 'inline',
7878
root: SRC_DIR,
7979
target: isBun ? 'bun' : 'node',
80-
minify: IS_PRODUCTION,
80+
minify: CONFIG.minify ?? IS_PRODUCTION,
8181
// splitting: false -> necessary to analyze the server pages
8282
// for the client build. FIXME: improve this to analyze each
8383
// server page including the chunks that the page needs.

packages/www/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ export default {
209209
text: 'Output Adapter',
210210
link: '/building-your-application/configuring/output-adapter',
211211
},
212+
{
213+
text: 'Minify',
214+
link: '/building-your-application/configuring/minify',
215+
},
212216
{
213217
text: 'Integrations',
214218
link: '/building-your-application/configuring/integrations',

0 commit comments

Comments
 (0)