|
| 1 | +const esbuild = require('esbuild'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const production = process.argv.includes('--production'); |
| 6 | +const watch = process.argv.includes('--watch'); |
| 7 | + |
| 8 | +/** |
| 9 | + * @type {import('esbuild').Plugin} |
| 10 | + */ |
| 11 | +const esbuildProblemMatcherPlugin = { |
| 12 | + name: 'esbuild-problem-matcher', |
| 13 | + |
| 14 | + setup(build) { |
| 15 | + build.onStart(() => { |
| 16 | + console.log('[watch] test build started'); |
| 17 | + }); |
| 18 | + build.onEnd(result => { |
| 19 | + result.errors.forEach(({ text, location }) => { |
| 20 | + console.error(`✘ [ERROR] ${text}`); |
| 21 | + console.error(` ${location.file}:${location.line}:${location.column}:`); |
| 22 | + }); |
| 23 | + console.log('[watch] test build finished'); |
| 24 | + }); |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * @type {import('esbuild').Plugin} |
| 30 | + */ |
| 31 | +const copyAssetsPlugin = { |
| 32 | + name: 'copy-assets', |
| 33 | + setup(build) { |
| 34 | + build.onEnd(() => { |
| 35 | + const copyDir = (src, dest) => { |
| 36 | + if (!fs.existsSync(dest)) { |
| 37 | + fs.mkdirSync(dest, { recursive: true }); |
| 38 | + } |
| 39 | + |
| 40 | + const entries = fs.readdirSync(src, { withFileTypes: true }); |
| 41 | + |
| 42 | + for (const entry of entries) { |
| 43 | + const srcPath = path.join(src, entry.name); |
| 44 | + const destPath = path.join(dest, entry.name); |
| 45 | + |
| 46 | + if (entry.isDirectory()) { |
| 47 | + copyDir(srcPath, destPath); |
| 48 | + } else { |
| 49 | + fs.copyFileSync(srcPath, destPath); |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + copyDir('test-resources', 'dist/test-resources'); |
| 55 | + }); |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +async function main() { |
| 60 | + // Test build context |
| 61 | + const testCtx = await esbuild.context({ |
| 62 | + entryPoints: ['src/test/**/*.ts'], |
| 63 | + bundle: true, |
| 64 | + format: 'cjs', |
| 65 | + minify: production, |
| 66 | + sourcemap: true, |
| 67 | + sourcesContent: !production, |
| 68 | + platform: 'node', |
| 69 | + outdir: 'dist/test', |
| 70 | + define: { |
| 71 | + 'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'), |
| 72 | + }, |
| 73 | + external: ['vscode', 'mocha'], |
| 74 | + logLevel: 'info', |
| 75 | + plugins: [esbuildProblemMatcherPlugin, copyAssetsPlugin], |
| 76 | + }); |
| 77 | + |
| 78 | + if (watch) { |
| 79 | + await testCtx.watch(); |
| 80 | + } else { |
| 81 | + await testCtx.rebuild(); |
| 82 | + await testCtx.dispose(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +main().catch(e => { |
| 87 | + console.error(e); |
| 88 | + process.exit(1); |
| 89 | +}); |
0 commit comments