gulp: init, bundle src/bin/* scripts to dist/

This commit is contained in:
2025-01-04 17:03:50 +08:00
parent 6d7fa4aa09
commit c9002f4405
4 changed files with 2838 additions and 1 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
out
node_modules
dist/

80
gulpfile.js Normal file
View File

@@ -0,0 +1,80 @@
import { readdir } from 'fs/promises';
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import * as gulp from 'gulp';
import { rm } from 'fs/promises';
// Get the current file's directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Define input and output directories
const SRC_DIR = path.join(__dirname, 'src');
const BIN_DIR = path.join(SRC_DIR, 'bin');
const DIST_DIR = path.join(__dirname, 'dist');
// Function to bundle a single file using esbuild
async function buildFile(entryFile) {
const outputFile = path.join(
DIST_DIR,
path.basename(entryFile).replace(/\.ts$/, '.mjs')
);
console.log(`Building: ${entryFile}${outputFile}`);
// Define the esbuild command and arguments
const command = 'esbuild';
const args = [
entryFile,
'--bundle',
'--platform=node',
'--format=esm',
'--minify',
'--outfile=' + outputFile,
];
// Execute the esbuild command as a child process
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Build failed for ${entryFile} with exit code ${code}`));
}
});
});
}
// Get all files in the src/bin directory
async function getEntryFiles() {
const files = await readdir(BIN_DIR);
return files.map(file => path.join(BIN_DIR, file));
}
// Build all entry files
async function buildAll() {
const entryFiles = await getEntryFiles();
for (const entry of entryFiles) {
await buildFile(entry);
}
}
// Clean the output directory
async function clean() {
try {
console.log(`Cleaning dist directory: ${DIST_DIR}`);
await rm(DIST_DIR, { recursive: true, force: true });
} catch (err) {
console.error('Error cleaning dist directory:', err.message);
}
}
// Watch for file changes and rebuild
export function watch() {
console.log('Watching for changes...');
gulp.watch(SRC_DIR + '/**/*', gulp.series(clean, buildAll));
}
// Export tasks for Gulp
export default gulp.series(clean, buildAll); // Default task

2753
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,11 +4,13 @@
"devDependencies": {
"@types/node": "^20.14.2",
"@types/yargs": "^17.0.33",
"gulp": "^5.0.0",
"typescript": "^5.5.2"
},
"scripts": {
"watch": "tsc -w",
"compile": "tsc"
"compile": "tsc",
"gulp": "gulp"
},
"dependencies": {
"yargs": "^17.7.2"