Skip to content

Commit

Permalink
build(i18n): minify i18n files after build
Browse files Browse the repository at this point in the history
  • Loading branch information
pawcoding committed Aug 30, 2024
1 parent 5762cde commit a7ca120
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"name": "pawcode Development"
},
"scripts": {
"postversion": "pnpm run build:ngsw && pnpm run build:sitemap",
"build": "ng build",
"postbuild": "node ./scripts/minify-i18n.js",
"build:ngsw": "node ./scripts/update-ngsw.js",
"build:sitemap": "node ./scripts/update-sitemap.js",
"chromatic": "chromatic --build-script-name storybook:build --exit-zero-on-changes",
"preinstall": "npx only-allow pnpm",
"lint": "ng lint",
"ng": "ng",
Expand All @@ -28,9 +29,9 @@
"start": "ng serve",
"storybook": "ng run rainbow-palette:storybook",
"storybook:build": "ng run rainbow-palette:build-storybook",
"chromatic": "chromatic --build-script-name storybook:build --exit-zero-on-changes",
"test": "ng test",
"test:ci": "ng test --no-watch --no-progress --browsers=ChromeHeadless",
"postversion": "pnpm run build:ngsw && pnpm run build:sitemap",
"watch": "ng build --watch --configuration development"
},
"dependencies": {
Expand Down
31 changes: 31 additions & 0 deletions scripts/minify-i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs');
const path = require('path');

// List of supported languages
const languages = ['en', 'de'];

/**
* Minify a translation file
*/
function minifyTranslation(language) {
// Read the file size before minification
const file = `../dist/rainbow-palette/browser/assets/i18n/${language}.json`;
const before = fs.statSync(path.resolve(__dirname, file)).size;

// Minify the file
const content = fs.readFileSync(path.resolve(__dirname, file), 'utf8');
const minified = JSON.stringify(JSON.parse(content));
fs.writeFileSync(path.resolve(__dirname, file), minified);

// Read the file size after minification
const after = fs.statSync(path.resolve(__dirname, file)).size;

// Log the results
const beforeKB = (before / 1024).toFixed(2);
const afterKB = (after / 1024).toFixed(2);
const saved = (100 - (after / before) * 100).toFixed(2);
console.log(`Minified "${language}.json": ${beforeKB} kB -> ${afterKB} kB (-${saved}%)`);
}

// Minify all translation files
languages.forEach(minifyTranslation);

0 comments on commit a7ca120

Please sign in to comment.