Skip to content

Commit 3e94dd0

Browse files
committed
AG-42200 reduce bundle size
Squashed commit of the following: commit 92334ee Merge: 76e9cc9 7fc93da Author: Slava Leleka <v.leleka@adguard.com> Date: Wed May 14 12:16:05 2025 -0400 Merge branch 'master' into fix/AG-42200 commit 76e9cc9 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue May 13 16:57:38 2025 -0400 fix indent_level to reduce bundle size commit 75266af Author: Slava Leleka <v.leleka@adguard.com> Date: Tue May 13 16:57:00 2025 -0400 minify injections during build
1 parent 7fc93da commit 3e94dd0

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ const scriptletsConfig = {
151151
format: {
152152
comments: false,
153153
beautify: true,
154+
indent_level: 2,
154155
},
155156
},
156157
),

scripts/build-funcs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ const getScriptletFunctionsString = async () => {
4242

4343
const scriptletsFunctions = Object.values(scriptletsList);
4444

45-
const scriptletsStrings = scriptletsFunctions.map((scriptlet) => {
46-
const scriptletWithDeps = attachDependencies(scriptlet);
45+
const scriptletsStrings = await Promise.all(scriptletsFunctions.map(async (scriptlet) => {
46+
const scriptletWithDeps = await attachDependencies(scriptlet);
4747
const scriptletWithCall = addCall(scriptlet, scriptletWithDeps);
4848
return wrapInFunc(scriptlet.name, scriptletWithCall);
49-
});
49+
}));
5050

5151
const scriptletsString = scriptletsStrings.join('\n');
5252

scripts/build-redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const getRedirectCode = async (source) => {
125125
throw new Error(`Was unable to find redirect by name: ${source.name}`);
126126
}
127127

128-
let result = attachDependencies(redirect);
128+
let result = await attachDependencies(redirect);
129129
result = addCall(redirect, result);
130130

131131
// redirect code for different sources is checked in tests

src/helpers/injector.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1+
import { minify } from 'terser';
2+
13
import { type Redirect, type Scriptlet } from '../../types/types';
24
import { type Source } from '../scriptlets';
35

46
/**
5-
* Concat dependencies to scriptlet code
7+
* Concat dependencies to scriptlet code.
8+
*
9+
* Dependencies are minified using Terser while preserving their names.
610
*
711
* @param scriptlet scriptlet or redirect function
812
* @returns string view of scriptlet with attached dependencies
913
*/
10-
export function attachDependencies(scriptlet: Scriptlet | Redirect): string {
14+
export async function attachDependencies(scriptlet: Scriptlet | Redirect): Promise<string> {
1115
const { injections = [] } = scriptlet;
12-
return injections.reduce((accum, dep) => {
13-
return `${accum}\n${dep.toString()}`;
16+
17+
// Minify each dependency while preserving its name
18+
const minifiedDeps = await Promise.all(injections.map(async (dep) => {
19+
try {
20+
const depStr = dep.toString();
21+
const result = await minify(depStr, {
22+
compress: true,
23+
mangle: {
24+
// injection functions should be accessible by the same name
25+
// so we preserve their names
26+
keep_fnames: true,
27+
},
28+
});
29+
// Fallback to original if minification fails
30+
return result.code || depStr;
31+
} catch (e) {
32+
// If minification fails, return the original code
33+
return dep.toString();
34+
}
35+
}));
36+
37+
// Combine the minified dependencies with the scriptlet code
38+
return minifiedDeps.reduce((acc: string, depCode: string) => {
39+
return `${acc}\n${depCode}`;
1440
}, scriptlet.toString());
1541
}
1642

0 commit comments

Comments
 (0)