|
| 1 | +/*----------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE file in the project root for license information. |
| 4 | + *-----------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +const { execSync } = require('child_process'); |
| 7 | +const esbuild = require('esbuild'); |
| 8 | +const { esmAliasPlugin, esbuildProblemMatcherPlugin, nativeNodeModulesPlugin } = require('./esbuild.plugins.cjs'); |
| 9 | +const { srcDir, outDir } = require('./esbuild.settings.cjs'); |
| 10 | +const { cp } = require('node:fs/promises'); |
| 11 | +const path = require('path'); |
| 12 | +const { sync } = require('fast-glob'); |
| 13 | + |
| 14 | +const production = process.argv.includes('--production'); |
| 15 | +const isWatch = process.argv.includes('--watch'); |
| 16 | + |
| 17 | +// eslint-disable no-console |
| 18 | + |
| 19 | +const baseConfig = { |
| 20 | + bundle: true, |
| 21 | + target: 'chrome108', |
| 22 | + minify: production, |
| 23 | + sourcemap: !production, |
| 24 | + logLevel: 'warning', |
| 25 | + logOverride: { |
| 26 | + 'equals-negative-zero': 'silent' |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +async function copyJsonFiles() { |
| 31 | + const jsonFiles = sync('src/**/*.json', { absolute: false }); |
| 32 | + for (const file of jsonFiles) { |
| 33 | + const dest = path.join('out', file); |
| 34 | + await cp(file, dest, { recursive: false, force: true }); |
| 35 | + } |
| 36 | + await cp('package.json', 'out/package.json'); |
| 37 | +} |
| 38 | + |
| 39 | +async function buildExtension() { |
| 40 | + // Only run type-checking on non-watch builds |
| 41 | + if (!isWatch) { |
| 42 | + console.log('🔍 Running TypeScript type-checking...'); |
| 43 | + try { |
| 44 | + execSync('tsc --noEmit -p tsconfig.json', { stdio: 'inherit' }); |
| 45 | + } catch (err) { |
| 46 | + console.error('❌ TypeScript type-checking failed.'); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + console.log(`📦 Building the extension for ${production ? 'Production' : 'Development'}...`); |
| 52 | + |
| 53 | + if (production) { |
| 54 | + // Build the extension.js (single bundle for production) |
| 55 | + const extConfig = { |
| 56 | + ...baseConfig, |
| 57 | + platform: 'node', |
| 58 | + format: 'cjs', |
| 59 | + entryPoints: [`./${srcDir}/extension.ts`], |
| 60 | + outfile: `${outDir}/${srcDir}/extension.js`, |
| 61 | + external: ['vscode', 'shelljs', 'jsonc-parser'], |
| 62 | + plugins: [ |
| 63 | + nativeNodeModulesPlugin(), |
| 64 | + esbuildProblemMatcherPlugin(production) |
| 65 | + ] |
| 66 | + }; |
| 67 | + await esbuild.build(extConfig); |
| 68 | + await copyJsonFiles(); |
| 69 | + console.log('✅ Extension build completed'); |
| 70 | + } else { |
| 71 | + // Build the Extension for development (individual files) |
| 72 | + const srcFiles = sync(`${srcDir}/**/*.{js,ts}`, { absolute: false }); |
| 73 | + const devExtConfig = { |
| 74 | + ...baseConfig, |
| 75 | + platform: 'node', |
| 76 | + format: 'cjs', |
| 77 | + entryPoints: srcFiles.map(f => `./${f}`), |
| 78 | + outbase: srcDir, |
| 79 | + outdir: `${outDir}/${srcDir}`, |
| 80 | + external: ['vscode', 'shelljs', 'jsonc-parser', '@aws-sdk/client-s3'], |
| 81 | + plugins: [ |
| 82 | + esmAliasPlugin(), |
| 83 | + nativeNodeModulesPlugin(), |
| 84 | + esbuildProblemMatcherPlugin(production) |
| 85 | + ] |
| 86 | + }; |
| 87 | + |
| 88 | + if (isWatch) { |
| 89 | + try { |
| 90 | + const ctx = await esbuild.context({ |
| 91 | + ...devExtConfig, |
| 92 | + plugins: [ |
| 93 | + ...devExtConfig.plugins, |
| 94 | + { |
| 95 | + name: 'rebuild-hook', |
| 96 | + setup(build) { |
| 97 | + build.onEnd(result => { |
| 98 | + if (result.errors.length === 0) { |
| 99 | + console.log('🔁 Extension rebuild succeeded'); |
| 100 | + copyJsonFiles().catch(err => |
| 101 | + console.error('❌ Failed to copy JSON files after rebuild:', err) |
| 102 | + ); |
| 103 | + } else { |
| 104 | + console.error('❌ Extension rebuild errors:', result.errors); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + ] |
| 110 | + }); |
| 111 | + await ctx.watch(); |
| 112 | + await copyJsonFiles().catch(err => { |
| 113 | + console.error('❌ Failed to copy JSON files on initial watch setup:', err); |
| 114 | + }); |
| 115 | + console.log('👀 Watching the extension...'); |
| 116 | + |
| 117 | + // Keep the process alive even if there are errors |
| 118 | + // esbuild will continue watching and rebuild on changes |
| 119 | + await new Promise(() => {}); // Never resolves - keeps watch running |
| 120 | + } catch (err) { |
| 121 | + console.error('❌ Failed to start extension watcher:', err); |
| 122 | + console.error('⚠️ Watch mode failed to start. Please fix errors and restart.'); |
| 123 | + process.exit(1); |
| 124 | + } |
| 125 | + } else { |
| 126 | + await esbuild.build(devExtConfig); |
| 127 | + await copyJsonFiles(); |
| 128 | + console.log('✅ Extension build completed'); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +if (require.main === module) { |
| 134 | + buildExtension().catch(err => { |
| 135 | + console.error('❌ Extension build failed:', err); |
| 136 | + process.exit(1); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +module.exports = { buildExtension }; |
0 commit comments