mirror of
https://github.com/Redocly/redoc.git
synced 2025-11-05 02:07:31 +03:00
Co-authored-by: Roman Hotsiy <gotsijroman@gmail.com> Co-authored-by: Alex Varchuk <olexandr.varchuk@gmail.com> Co-authored-by: Oprysk Vyacheslav <vyacheslav@redocly.com> Co-authored-by: Ivan Kropyvnytskyi <130547411+ivankropyvnytskyi@users.noreply.github.com> Co-authored-by: Yevhen Pylyp <yevhen.pylyp@redocly.com> Co-authored-by: Vladyslav Makarenko <vladyslav.makarenko@redocly.com> Co-authored-by: Yevhenii Medviediev <yevhenii.medviediev@redocly.com> Co-authored-by: Oleksii Horbachevskyi <oleksii.horbachevskyi@redocly.com> Co-authored-by: volodymyr-rutskyi <rutskyi.v@gmail.com> Co-authored-by: Adam Altman <adam@redoc.ly> Co-authored-by: Andrew Tatomyr <andrew.tatomyr@redocly.com> Co-authored-by: Anastasiia Derymarko <anastasiia@redocly.com> Co-authored-by: Roman Marshevskyy <roman.marshevskyy@redoc.ly> Co-authored-by: Lorna Mitchell <lorna.mitchell@redocly.com> Co-authored-by: Taylor Krusen <taylor.krusen@redocly.com>
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
#!/usr/bin/env node
|
|
import esbuild from 'esbuild';
|
|
import fs from 'fs/promises';
|
|
|
|
async function* getFiles(path = `./`) {
|
|
const entries = await fs.readdir(path, { withFileTypes: true });
|
|
|
|
for (let file of entries) {
|
|
if (file.isDirectory()) {
|
|
yield* getFiles(`${path}${file.name}/`);
|
|
} else {
|
|
yield path + file.name;
|
|
}
|
|
}
|
|
}
|
|
|
|
const minifyOptions = {
|
|
format: process.argv.indexOf('--cjs') > -1 ? 'cjs' : 'esm',
|
|
minify: true,
|
|
target: 'ESNext',
|
|
platform: 'node',
|
|
// if not set esbuild output crashes when used in node,
|
|
// see https://github.com/nodejs/node/issues/45016
|
|
};
|
|
|
|
async function minifyFile(filePath) {
|
|
const fileContents = await fs.readFile(filePath, 'utf-8');
|
|
const transformed = await esbuild.transform(fileContents, {
|
|
...minifyOptions,
|
|
format:
|
|
filePath.endsWith('.cjs') || filePath.includes('/compiled/') ? 'cjs' : minifyOptions.format,
|
|
});
|
|
return await fs.writeFile(filePath, transformed.code);
|
|
}
|
|
|
|
async function minifyDir(dir) {
|
|
if (!dir.endsWith('/')) dir = dir + '/';
|
|
let promises = [];
|
|
|
|
await esbuild.initialize();
|
|
|
|
try {
|
|
for await (const filePath of getFiles(dir)) {
|
|
if (filePath.endsWith('.js') || filePath.endsWith('.mjs')) {
|
|
promises.push(minifyFile(filePath));
|
|
}
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
} catch (e) {
|
|
console.error('Could not minify files:', e);
|
|
}
|
|
}
|
|
|
|
await minifyDir(process.argv[2]);
|