|
| 1 | +const esbuild = require('esbuild'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const path = require('path'); |
| 4 | +require('dotenv').config(); |
| 5 | +const { dependencies } = require('../package.json'); |
| 6 | + |
| 7 | +const production = process.argv.includes('--production'); |
| 8 | +const watch = process.argv.includes('--watch'); |
| 9 | + |
| 10 | +const outDir = 'dist-minified'; |
| 11 | +const define = { |
| 12 | + 'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'), |
| 13 | +}; |
| 14 | + |
| 15 | +const external = [ |
| 16 | + '@nestjs/microservices', |
| 17 | + '@fastify/static', |
| 18 | + // packages with binaries |
| 19 | + ...Object.keys(dependencies), |
| 20 | +]; |
| 21 | + |
| 22 | +async function main() { |
| 23 | + const ctx = await esbuild.context({ |
| 24 | + entryPoints: ['dist/src/main.js'], |
| 25 | + bundle: true, |
| 26 | + format: 'cjs', |
| 27 | + minifyWhitespace: true, |
| 28 | + // if true - some nestjs decorators are not working |
| 29 | + minifyIdentifiers: false, |
| 30 | + sourcemap: !production, |
| 31 | + sourcesContent: false, |
| 32 | + platform: 'node', |
| 33 | + outfile: `${outDir}/main.js`, |
| 34 | + define, |
| 35 | + external, |
| 36 | + logLevel: 'silent', |
| 37 | + plugins: [ |
| 38 | + /* add to the end of plugins array */ |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 40 | + esbuildProblemMatcherPlugin, |
| 41 | + ], |
| 42 | + }); |
| 43 | + if (watch) { |
| 44 | + await ctx.watch(); |
| 45 | + } else { |
| 46 | + await ctx.rebuild(); |
| 47 | + await ctx.dispose(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function copySource(source, destination) { |
| 52 | + try { |
| 53 | + if (fs.pathExistsSync(source)) { |
| 54 | + fs.copySync(source, destination); |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + console.error('✘ [esbuild ERROR copySource]', error); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @type {import('esbuild').Plugin} |
| 63 | + */ |
| 64 | +const esbuildProblemMatcherPlugin = { |
| 65 | + name: 'esbuild-problem-matcher', |
| 66 | + |
| 67 | + setup(build) { |
| 68 | + build.onStart(() => { |
| 69 | + console.debug('[esbuild] build started'); |
| 70 | + }); |
| 71 | + build.onEnd((result) => { |
| 72 | + result.errors.forEach(({ text, location }) => { |
| 73 | + console.error(`✘ [esbuild ERROR] ${text}`); |
| 74 | + console.error(` ${location.file}:${location.line}:${location.column}:`); |
| 75 | + }); |
| 76 | + console.debug('[esbuild] build finished'); |
| 77 | + |
| 78 | + copySource( |
| 79 | + path.resolve(__dirname, 'defaults'), |
| 80 | + path.resolve(__dirname, outDir, 'defaults'), |
| 81 | + ); |
| 82 | + console.debug('[esbuild] copied "defaults" folder'); |
| 83 | + }); |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +main().catch((e) => { |
| 88 | + console.error(e); |
| 89 | + process.exit(1); |
| 90 | +}); |
0 commit comments