1+ const { execSync } = require ( 'child_process' ) ;
2+ const esbuild = require ( 'esbuild' ) ;
3+ const { esmAliasPlugin, esbuildProblemMatcherPlugin, nativeNodeModulesPlugin, svgrPlugin, verbosePlugin } = require ( './esbuild.plugins.cjs' ) ;
4+ const { webviews, srcDir, outDir } = require ( './esbuild.settings.cjs' ) ;
5+ const { sassPlugin } = require ( 'esbuild-sass-plugin' ) ;
6+ const { cp, mkdir, readFile, stat } = require ( 'node:fs/promises' ) ;
7+ const path = require ( 'path' ) ;
8+ const { sync } = require ( 'fast-glob' ) ;
9+
10+
11+ const production = process . argv . includes ( '--production' ) ;
12+
13+ // eslint-disable no-console
14+
15+ // Run type-checking
16+
17+ /// Verify the extension
18+ try {
19+ // execSync('tsc --noEmit', { stdio: 'inherit' });
20+ execSync ( 'tsc --noEmit -p tsconfig.json' , { stdio : 'inherit' } ) ;
21+ } catch ( err ) {
22+ console . error ( '❌ TypeScript type-checking failed.' ) ;
23+ process . exit ( 1 ) ;
24+ }
25+
26+ // Verify the WebViews
27+ try {
28+ execSync ( 'tsc --noEmit -p ./src/webview/tsconfig.json' , { stdio : 'inherit' } ) ;
29+ } catch ( err ) {
30+ console . error ( '❌ TypeScript type-checking failed.' ) ;
31+ process . exit ( 1 ) ;
32+ }
33+
34+
35+ console . log ( `esbuild: building for production: ${ production ? 'Yes' : 'No' } ` ) ;
36+
37+ const baseConfig = {
38+ bundle : true ,
39+ target : 'chrome108' ,
40+ minify : production ,
41+ sourcemap : ! production ,
42+ logLevel : 'warning' ,
43+ } ;
44+
45+ async function build ( ) {
46+ if ( production ) {
47+ // Build the extension.js
48+ const extConfig = {
49+ ...baseConfig ,
50+ platform : 'node' ,
51+ format : 'cjs' ,
52+ entryPoints : [ `./${ srcDir } /extension.ts` ] ,
53+ outfile : `${ outDir } /${ srcDir } /extension.js` ,
54+ external : [ 'vscode' , 'shelljs' , 'jsonc-parser' ] ,
55+ plugins : [
56+ nativeNodeModulesPlugin ( ) ,
57+ esbuildProblemMatcherPlugin ( production ) // this one is to be added to the end of plugins array
58+ ]
59+ } ;
60+ await esbuild . build ( extConfig ) ;
61+ console . log ( '✅ Extension build completed' ) ;
62+
63+ // Build the Webviews
64+ const webviewsConfig = {
65+ ...baseConfig ,
66+ platform : 'browser' ,
67+ format : 'esm' ,
68+ entryPoints : [ ...webviews . map ( webview => `./${ srcDir } /webview/${ webview } /app/index.tsx` ) ] ,
69+ outdir : `${ outDir } ` ,
70+ loader : {
71+ '.png' : 'file' ,
72+ } ,
73+ plugins : [
74+ sassPlugin ( ) ,
75+ svgrPlugin ( { plugins : [ '@svgr/plugin-jsx' ] } ) ,
76+ esbuildProblemMatcherPlugin ( production ) // this one is to be added to the end of plugins array
77+ ]
78+ } ;
79+ await esbuild . build ( webviewsConfig ) ;
80+ console . log ( '✅ Webview build completed' ) ;
81+ } else {
82+ // Build the Extension for development
83+ const srcFiles = sync ( `${ srcDir } /**/*.{js,ts}` , { absolute : false } ) ;
84+ const devExtConfig = {
85+ ...baseConfig ,
86+ platform : 'node' ,
87+ format : 'cjs' ,
88+ entryPoints : srcFiles . map ( f => `./${ f } ` ) ,
89+ outbase : srcDir ,
90+ outdir : `${ outDir } /${ srcDir } ` ,
91+ external : [ 'vscode' , 'shelljs' , 'jsonc-parser' , '@aws-sdk/client-s3' ] ,
92+ plugins : [
93+ // verbosePlugin(),
94+ esmAliasPlugin ( ) ,
95+ nativeNodeModulesPlugin ( ) ,
96+ esbuildProblemMatcherPlugin ( production ) // this one is to be added to the end of plugins array
97+ ]
98+ } ;
99+
100+ await esbuild . build ( devExtConfig ) ;
101+ console . log ( '✅ Extension build completed' ) ;
102+
103+ const jsonFiles = sync ( 'src/**/*.json' , { absolute : false } ) ;
104+ for ( const file of jsonFiles ) {
105+ const dest = path . join ( 'out' , file ) ;
106+ await cp ( file , dest , { recursive : false , force : true } ) ;
107+ }
108+ await cp ( 'package.json' , 'out/package.json' ) ;
109+
110+ const devWebViewConfig = {
111+ ...baseConfig ,
112+ platform : 'browser' ,
113+ format : 'esm' ,
114+ entryPoints : [ ...webviews . map ( webview => `./${ srcDir } /webview/${ webview } /app/index.tsx` ) ] ,
115+ outdir : `${ outDir } ` ,
116+ loader : {
117+ '.png' : 'file' ,
118+ } ,
119+ plugins : [
120+ // verbosePlugin(),
121+ sassPlugin ( ) ,
122+ svgrPlugin ( { plugins : [ '@svgr/plugin-jsx' ] } ) ,
123+ esbuildProblemMatcherPlugin ( production ) // this one is to be added to the end of plugins array
124+ ]
125+ } ;
126+ await esbuild . build ( devWebViewConfig ) ;
127+ console . log ( '✅ Webview build completed' ) ;
128+
129+ // Match everything under test/, not just *.test.ts
130+ // const testFiles = sync('test/**/*.{ts,tsx}', { absolute: true });
131+ // const testConfig = {
132+ // ...baseConfig,
133+ // platform: 'node',
134+ // format: 'cjs',
135+ // entryPoints: testFiles,
136+ // outdir: `${outDir}/test`,
137+ // outbase: 'test', // preserves folder structure
138+ // sourcemap: true,
139+ // // target: 'es2022',
140+ // external: [ 'vscode', 'shelljs', 'jsonc-parser', 'mocha', '@aws-sdk/client-s3', 'monocart-coverage-reports' ],
141+ // plugins: [
142+ // // verbosePlugin(),
143+ // esmAliasPlugin(),
144+ // nativeNodeModulesPlugin()
145+ // ]
146+ // }
147+ // await esbuild.build(testConfig);
148+ // console.log('✅ Tests build completed');
149+ }
150+
151+ async function dirExists ( path ) {
152+ try {
153+ if ( ( await stat ( path ) ) . isDirectory ( ) ) return true ;
154+ } catch { /* Ignore */ }
155+ return false ;
156+ }
157+
158+ // Copy webview's 'index.html's to the output webview dirs
159+ await Promise . all ( [
160+ ...webviews . map ( async webview => {
161+ const targetDir = path . join ( __dirname , '..' , `${ outDir } /${ webview } /app` ) ;
162+ if ( ! dirExists ( targetDir ) ) {
163+ await mkdir ( targetDir , { recursive : true , mode : 0o750 } ) ;
164+ }
165+ sync ( path . resolve ( __dirname , '..' , `${ srcDir } /webview/${ webview } /app/index.html` ) ) . map ( async srcFile => {
166+ await cp ( srcFile , path . join ( targetDir , `${ path . basename ( srcFile ) } ` ) ) ;
167+ } ) ;
168+ } )
169+ ] ) ;
170+ }
171+
172+ build ( ) . catch ( err => {
173+ console . error ( '❌ Build failed:' , err ) ;
174+ process . exit ( 1 ) ;
175+ } ) ;
0 commit comments