|
| 1 | +const { basename } = require('path'); |
| 2 | +//const { esbuildPluginDecorator } = require('esbuild-plugin-decorator'); |
| 3 | +const { |
| 4 | + realpathSync, readFileSync, writeFileSync, renameSync, existsSync |
| 5 | +} = require('fs'); |
| 6 | +const { spawnSync, execSync } = require('child_process'); |
| 7 | +const esbuild = require('esbuild'); |
| 8 | + |
| 9 | +const buildConfig = { |
| 10 | + basePath: `${__dirname}/..`, |
| 11 | + bundle: true, |
| 12 | + constants: {}, |
| 13 | + entry: 'src/index.ts', |
| 14 | + format: 'cjs', |
| 15 | + minify: false, |
| 16 | + outdir: 'dist', |
| 17 | + platform: { |
| 18 | + name: 'node', |
| 19 | + version: 18, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +const pkg = require(realpathSync(`${buildConfig.basePath}/package.json`, { encoding: 'utf-8' })); |
| 24 | + |
| 25 | +class Builder { |
| 26 | + config = { |
| 27 | + binaries: false, |
| 28 | + production: false, |
| 29 | + verbose: false, |
| 30 | + }; |
| 31 | + |
| 32 | + write(msg) { |
| 33 | + process.stdout.write(`${msg}`.toString()); |
| 34 | + } |
| 35 | + |
| 36 | + writeln(msg) { |
| 37 | + this.write(`${msg}\n`); |
| 38 | + } |
| 39 | + |
| 40 | + async compile() { |
| 41 | + const result = await esbuild.build({ |
| 42 | + absWorkingDir: buildConfig.basePath, |
| 43 | + allowOverwrite: true, |
| 44 | + bundle: buildConfig.bundle, |
| 45 | + define: { |
| 46 | + __APP_VERSION__: `'${pkg.version}'`, |
| 47 | + __COMPILED_AT__: `'${new Date().toUTCString()}'`, |
| 48 | + ...buildConfig.constants, |
| 49 | + }, |
| 50 | + entryPoints: [ buildConfig.entry ], |
| 51 | + format: buildConfig.format, |
| 52 | + logLevel: 'silent', |
| 53 | + metafile: true, |
| 54 | + minify: buildConfig.minify, |
| 55 | + outdir: buildConfig.outdir, |
| 56 | + platform: buildConfig.platform.name, |
| 57 | + experimentalOptimizeImports: true, |
| 58 | + plugins: [ |
| 59 | + // esbuildPluginDecorator({ |
| 60 | + // compiler: 'tsc', |
| 61 | + // tsconfigPath: `${buildConfig.basePath}/tsconfig.json`, |
| 62 | + // }), |
| 63 | + ], |
| 64 | + target: `${buildConfig.platform.name}${buildConfig.platform.version}`, |
| 65 | + }); |
| 66 | + |
| 67 | + return new Promise(resolve => resolve(result)); |
| 68 | + } |
| 69 | + |
| 70 | + sizeForDisplay(bytes) { |
| 71 | + return `${`${bytes / 1024}`.slice(0, 4)} kb`; |
| 72 | + } |
| 73 | + |
| 74 | + reportCompileResults(results) { |
| 75 | + results.errors.forEach(errorMsg => this.writeln(`* Error: ${errorMsg}`)); |
| 76 | + results.warnings.forEach(msg => this.writeln(`* Warning: ${msg}`)); |
| 77 | + |
| 78 | + Object.keys(results.metafile.outputs).forEach(fn => { |
| 79 | + this.writeln(`* » created '${fn}' (${this.sizeForDisplay(results.metafile.outputs[fn].bytes)})`); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + processArgv() { |
| 84 | + const argMap = { |
| 85 | + '--binaries': { name: 'binaries', value: true }, |
| 86 | + '--prod': { name: 'production', value: true }, |
| 87 | + '--production': { name: 'production', value: true }, |
| 88 | + '--verbose': { name: 'verbose', value: true }, |
| 89 | + '-p': { name: 'production', value: true }, |
| 90 | + '-v': { name: 'verbose', value: true }, |
| 91 | + }; |
| 92 | + |
| 93 | + process.argv |
| 94 | + .slice(2) |
| 95 | + .map(arg => { |
| 96 | + const hasMappedArg = typeof argMap[arg] === 'undefined'; |
| 97 | + return hasMappedArg ? { name: arg.replace(/^-+/, ''), value: true } : argMap[arg]; |
| 98 | + }) |
| 99 | + .forEach(data => (this.config[data.name] = data.value)); |
| 100 | + |
| 101 | + console.log(this.config); |
| 102 | + } |
| 103 | + |
| 104 | + convertToProductionFile() { |
| 105 | + const filename = basename(buildConfig.entry).replace(/\.ts$/, '.js'); |
| 106 | + const newFilename = pkg.name; |
| 107 | + const binaryFilename = `${buildConfig.outdir}/${filename.replace(/.js$/, '')}`; |
| 108 | + const contents = readFileSync(`${buildConfig.outdir}/${filename}`, { encoding: 'utf-8' }); |
| 109 | + |
| 110 | + spawnSync('chmod', [ '+x', `${buildConfig.outdir}/${filename}` ], { stdio: 'ignore' }); |
| 111 | + writeFileSync(`${buildConfig.outdir}/${filename}`, `#!/usr/bin/node\n\n${contents}`, { encoding: 'utf-8' }); |
| 112 | + renameSync(`${buildConfig.outdir}/${filename}`, `${buildConfig.outdir}/${newFilename}`); |
| 113 | + |
| 114 | + if (existsSync(binaryFilename)) { |
| 115 | + renameSync(binaryFilename, `${buildConfig.outdir}/${newFilename}-${pkg.version}`); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + compileBinaries() { |
| 120 | + const platforms = [ 'linux', 'macos', 'win' ]; |
| 121 | + const filename = basename(buildConfig.entry).replace(/\.ts$/, '.js'); |
| 122 | + const targets = platforms.map(t => `node16-${t.toLowerCase()}-x64`); |
| 123 | + const cmd = `npx pkg --compress Brotli --targets ${targets.join(',')} --out-path ${buildConfig.outdir} ${buildConfig.outdir}/${filename}`; |
| 124 | + |
| 125 | + execSync(cmd, { stdio: 'inherit' }); |
| 126 | + |
| 127 | + const origBinaries = platforms |
| 128 | + .map(os => `${buildConfig.outdir}/${filename.replace(/.js$/, '')}-${os}`) |
| 129 | + .map(fn => (fn.includes('-win') ? `${fn}.exe` : fn)); |
| 130 | + |
| 131 | + const newBinaries = platforms |
| 132 | + .map(os => `${buildConfig.outdir}/${pkg.name}-v${pkg.version}-${os}-x64`) |
| 133 | + .map(fn => (fn.includes('-win') ? `${fn}.exe` : fn)); |
| 134 | + |
| 135 | + for (const originalFn of origBinaries) { |
| 136 | + const newFn = newBinaries[origBinaries.indexOf(originalFn)]; |
| 137 | + renameSync(originalFn, newFn); |
| 138 | + spawnSync('chmod', [ '+x', newFn ], { stdio: 'ignore' }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + async run() { |
| 143 | + this.processArgv(); |
| 144 | + |
| 145 | + if (this.config.verbose) { |
| 146 | + this.writeln(`* Using esbuild v${esbuild.version}.`); |
| 147 | + } |
| 148 | + |
| 149 | + this.write(`* Compiling application...${this.config.verbose ? '\n' : ''}`); |
| 150 | + |
| 151 | + const startedTs = new Date().getTime(); |
| 152 | + const results = await this.compile(); |
| 153 | + |
| 154 | + if (this.config.binaries) { |
| 155 | + this.compileBinaries(); |
| 156 | + } |
| 157 | + |
| 158 | + const finishedTs = new Date().getTime(); |
| 159 | + |
| 160 | + if (this.config.verbose) { |
| 161 | + this.reportCompileResults(results); |
| 162 | + } |
| 163 | + |
| 164 | + this.writeln(`${this.config.verbose ? '* D' : 'd'}one. (${finishedTs - startedTs} ms)`); |
| 165 | + |
| 166 | + if (this.config.production) { |
| 167 | + this.convertToProductionFile(); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +new Builder().run(); |
0 commit comments