1
+ #!/usr/bin/env node
2
+
3
+ const os = require ( 'os' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+ const path = require ( 'path' ) ;
6
+ const process = require ( 'process' ) ;
7
+ const childProcess = require ( 'child_process' ) ;
8
+ const esbuild = require ( 'esbuild' ) ;
9
+ const packageJSON = require ( '../package.json' ) ;
10
+
11
+ const platform = os . platform ( ) ;
12
+
13
+ /* eslint-disable no-console */
14
+ async function main ( argv = process . argv ) {
15
+ argv = argv . slice ( 2 ) ;
16
+ const projectRoot = path . join ( __dirname , '..' ) ;
17
+ const buildPath = path . join ( projectRoot , 'build' ) ;
18
+ const distPath = path . join ( projectRoot , 'dist' ) ;
19
+ const gitPath = process . env . GIT_DIR ?? path . join ( projectRoot , '.git' ) ;
20
+ await fs . promises . rm ( distPath , {
21
+ recursive : true ,
22
+ force : true ,
23
+ } ) ;
24
+ const buildArgs = [ '-p' , './tsconfig.build.json' , ...argv ] ;
25
+ console . error ( 'Running tsc:' ) ;
26
+ console . error ( [ 'tsc' , ...buildArgs ] . join ( ' ' ) ) ;
27
+ childProcess . execFileSync ( 'tsc' , buildArgs , {
28
+ stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
29
+ windowsHide : true ,
30
+ encoding : 'utf-8' ,
31
+ shell : platform === 'win32' ? true : false ,
32
+ } ) ;
33
+ // This collects the build metadata and adds it to the build folder so that dynamic imports to it will resolve correctly.
34
+ let gitHead = process . env . COMMIT_HASH ;
35
+ if ( gitHead == null ) {
36
+ gitHead = await fs . promises . readFile ( path . join ( gitPath , 'HEAD' ) , 'utf-8' ) ;
37
+ if ( gitHead . startsWith ( 'ref: ' ) ) {
38
+ const refPath = gitHead . slice ( 5 ) . trim ( ) ;
39
+ gitHead = await fs . promises
40
+ . readFile ( path . join ( gitPath , refPath ) , 'utf-8' )
41
+ . then ( ( ref ) => ref . trim ( ) ) ;
42
+ }
43
+ }
44
+ const buildJSON = {
45
+ versionMetadata : {
46
+ version : packageJSON . version ,
47
+ commitHash : gitHead ,
48
+ } ,
49
+ } ;
50
+ console . error ( 'Writing build metadata (build.json):' ) ;
51
+ console . error ( buildJSON ) ;
52
+ await fs . promises . writeFile (
53
+ path . join ( buildPath , 'build.json' ) ,
54
+ JSON . stringify ( buildJSON , null , 2 ) ,
55
+ ) ;
56
+ // This specifies import paths that is left as an external require
57
+ // This is kept to packages that have a native binding
58
+ const externalDependencies = Object . keys ( packageJSON . nativeDependencies ?? { } ) ;
59
+ const esbuildOptions = {
60
+ entryPoints : [
61
+ path . join ( buildPath , 'index.js' ) ,
62
+ ] ,
63
+ sourceRoot : buildPath ,
64
+ bundle : true ,
65
+ platform : 'node' ,
66
+ outdir : distPath ,
67
+ external : externalDependencies ,
68
+ treeShaking : true ,
69
+ // External source map for debugging
70
+ sourcemap : true ,
71
+ // Minify and keep the original names
72
+ minify : true ,
73
+ keepNames : true ,
74
+ } ;
75
+ console . error ( 'Running esbuild:' ) ;
76
+ console . error ( esbuildOptions ) ;
77
+ await esbuild . build ( esbuildOptions ) ;
78
+ }
79
+ /* eslint-enable no-console */
80
+
81
+ void main ( ) ;
0 commit comments