| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* There is https://www.npmjs.com/package/esbuild-plugin-compress but it does |
| 3 |
* not work together with our PO plugin, they are incompatible due to requiring |
| 4 |
* different values for `write:`. We may be able to change our plugins to work |
| 5 |
* with `write: false`, but this is easy enough to implement ourselves. |
| 6 |
*/ |
| 7 |
|
| 8 |
import child_process from 'node:child_process'; |
| 9 |
import fs from 'node:fs'; |
| 10 |
import path from 'node:path'; |
| 11 |
import util from 'node:util'; |
| 12 |
|
| 13 |
const NAME = 'cockpitCompressPlugin'; |
| 14 |
|
| 15 |
const exec = util.promisify(child_process.execFile); |
| 16 |
|
| 17 |
const getAllFiles = function(dirPath, arrayOfFiles) { |
| 18 |
const files = fs.readdirSync(dirPath); |
| 19 |
|
| 20 |
arrayOfFiles = arrayOfFiles || []; |
| 21 |
|
| 22 |
files.forEach(function(file) { |
| 23 |
if (fs.statSync(dirPath + "/" + file).isDirectory()) { |
| 24 |
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles); |
| 25 |
} else { |
| 26 |
arrayOfFiles.push(path.join(dirPath, "/", file)); |
| 27 |
} |
| 28 |
}); |
| 29 |
|
| 30 |
return arrayOfFiles; |
| 31 |
}; |
| 32 |
|
| 33 |
export const cockpitCompressPlugin = ({ subdir = '', exclude = null } = {}) => ({ |
| 34 |
name: NAME, |
| 35 |
setup(build) { |
| 36 |
build.onEnd(async () => { |
| 37 |
const gzipPromises = []; |
| 38 |
const path = "./dist/" + subdir; |
| 39 |
|
| 40 |
for await (const dirent of getAllFiles(path)) { |
| 41 |
if (exclude && exclude.test(dirent)) |
| 42 |
continue; |
| 43 |
if (dirent.endsWith('.js') || dirent.endsWith('.css')) { |
| 44 |
gzipPromises.push(exec('gzip', ['-n9', dirent])); |
| 45 |
} |
| 46 |
} |
| 47 |
await Promise.all(gzipPromises); |
| 48 |
return null; |
| 49 |
}); |
| 50 |
} |
| 51 |
}); |