Skip to content

Commit

Permalink
Add compress script to apply gzip to assets not packed by webpack
Browse files Browse the repository at this point in the history
Signed-off-by: 1000TurquoisePogs <[email protected]>
  • Loading branch information
1000TurquoisePogs committed Jan 24, 2025
1 parent 157d4b3 commit d49a563
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the Zlux App Manager will be documented in this file.

## `3.2.0`

- Enhacement: Zowe v3 Desktop assets are now compressed so that the Desktop can load faster on slow internet. (#658)

## `3.0.0`

- Enhancement: Updated the Desktop Angular version from 12 to 18. This makes V2 Angular apps incompatible with V3. Iframe and React is unaffected.
Expand Down
41 changes: 41 additions & 0 deletions virtual-desktop/gzip.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { execSync } from 'node:child_process';

const GZIP_SIZE_MIN = 50000; //50 KB
const GZIP_ATTEMPT_MAX = 1000000000; //1 GB. Don't try compressing large files, these likely are incompressible binary.
const KEEP_RATIO = 0.85; // Items not compressed below this will not be kept as compressed

//const COMPRESSIBLE_TYPES = ['.js', '.css', '.html', '.json', '.xml', '.xlf',

function recurse(directory) {
const listing = fs.readdirSync(directory, {withFileTypes: true});
listing.forEach((dirent)=> {
let itemPath = path.join(directory, dirent.name);
if (dirent.isDirectory()) {
recurse(itemPath);
} else {
let stat = fs.statSync(itemPath);
if ((stat.size >= GZIP_SIZE_MIN) && (stat.size <= GZIP_ATTEMPT_MAX)) {
execSync('gzip -k '+dirent.name, {cwd: directory});
let itemPathGz = path.join(directory, dirent.name+'.gz');
let statGz = fs.statSync(itemPathGz);
//keep only one file: compressed file is kept if meaningfully smaller.
if ((stat.size*KEEP_RATIO) < statGz.size) {
fs.unlinkSync(itemPathGz);
} else {
fs.unlinkSync(itemPath);
}
}
}
});
}

try {
const listing = fs.readdirSync('web');
} catch (e) {
console.log('Could not read web directory, run build first.');
process.exit(1);
}

recurse('web');
1 change: 1 addition & 0 deletions virtual-desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"start": "ng build --watch",
"build": "ng build && npm run copy",
"build:externals": "exit 0",
"compress": "node gzip.mjs",
"copy": "cpx node_modules/requirejs/require.js web/",
"i18n": "ng-xi18n -p tsconfig.ngx-i18n.json --i18nFormat=xlf --outFile=messages.xlf && xliffmerge -p xliffmerge.json",
"lint": "tslint -c tslint.json \"src/**/*.ts\""
Expand Down

0 comments on commit d49a563

Please sign in to comment.