Skip to content

Commit a7ca120

Browse files
committed
build(i18n): minify i18n files after build
1 parent 5762cde commit a7ca120

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
"name": "pawcode Development"
1616
},
1717
"scripts": {
18-
"postversion": "pnpm run build:ngsw && pnpm run build:sitemap",
1918
"build": "ng build",
19+
"postbuild": "node ./scripts/minify-i18n.js",
2020
"build:ngsw": "node ./scripts/update-ngsw.js",
2121
"build:sitemap": "node ./scripts/update-sitemap.js",
22+
"chromatic": "chromatic --build-script-name storybook:build --exit-zero-on-changes",
2223
"preinstall": "npx only-allow pnpm",
2324
"lint": "ng lint",
2425
"ng": "ng",
@@ -28,9 +29,9 @@
2829
"start": "ng serve",
2930
"storybook": "ng run rainbow-palette:storybook",
3031
"storybook:build": "ng run rainbow-palette:build-storybook",
31-
"chromatic": "chromatic --build-script-name storybook:build --exit-zero-on-changes",
3232
"test": "ng test",
3333
"test:ci": "ng test --no-watch --no-progress --browsers=ChromeHeadless",
34+
"postversion": "pnpm run build:ngsw && pnpm run build:sitemap",
3435
"watch": "ng build --watch --configuration development"
3536
},
3637
"dependencies": {

scripts/minify-i18n.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
// List of supported languages
5+
const languages = ['en', 'de'];
6+
7+
/**
8+
* Minify a translation file
9+
*/
10+
function minifyTranslation(language) {
11+
// Read the file size before minification
12+
const file = `../dist/rainbow-palette/browser/assets/i18n/${language}.json`;
13+
const before = fs.statSync(path.resolve(__dirname, file)).size;
14+
15+
// Minify the file
16+
const content = fs.readFileSync(path.resolve(__dirname, file), 'utf8');
17+
const minified = JSON.stringify(JSON.parse(content));
18+
fs.writeFileSync(path.resolve(__dirname, file), minified);
19+
20+
// Read the file size after minification
21+
const after = fs.statSync(path.resolve(__dirname, file)).size;
22+
23+
// Log the results
24+
const beforeKB = (before / 1024).toFixed(2);
25+
const afterKB = (after / 1024).toFixed(2);
26+
const saved = (100 - (after / before) * 100).toFixed(2);
27+
console.log(`Minified "${language}.json": ${beforeKB} kB -> ${afterKB} kB (-${saved}%)`);
28+
}
29+
30+
// Minify all translation files
31+
languages.forEach(minifyTranslation);

0 commit comments

Comments
 (0)