@@ -10,23 +10,55 @@ import {
10
10
panic ,
11
11
write ,
12
12
} from './common.js' ;
13
- import ora from 'ora' ;
14
13
import { commandkitPlugin } from './esbuild-plugins/plugin' ;
15
14
import colors from '../utils/colors.js' ;
15
+ import { createSpinner } from './utils' ;
16
+ import { BuildOptions } from './types' ;
16
17
17
- export async function bootstrapProductionBuild ( config : any ) {
18
+ export async function bootstrapProductionBuild ( configPath : string ) {
19
+ const config = await findCommandKitConfig ( configPath ) ;
20
+ const spinner = createSpinner ( 'Creating optimized production build...' ) ;
21
+ const start = performance . now ( ) ;
22
+
23
+ try {
24
+ await buildProject ( config ) ;
25
+ spinner . succeed (
26
+ colors . green (
27
+ `Build completed in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms!` ,
28
+ ) ,
29
+ ) ;
30
+ } catch ( e ) {
31
+ spinner . fail ( 'Build failed' ) ;
32
+ panic ( e instanceof Error ? e . stack : e ) ;
33
+ }
34
+ }
35
+
36
+ export async function bootstrapDevelopmentBuild ( configPath : string ) {
37
+ const config = await findCommandKitConfig ( configPath ) ;
38
+
39
+ try {
40
+ await buildProject ( {
41
+ ...config ,
42
+ outDir : '.commandkit' ,
43
+ isDevelopment : true ,
44
+ } ) ;
45
+ } catch ( e ) {
46
+ console . error ( e instanceof Error ? e . stack : e ) ;
47
+ console . error (
48
+ colors . red ( 'Failed to build the project. Waiting for changes...' ) ,
49
+ ) ;
50
+ }
51
+ }
52
+
53
+ async function buildProject ( options : BuildOptions ) {
18
54
const {
19
55
sourcemap = false ,
20
56
minify = false ,
21
57
outDir = 'dist' ,
22
58
antiCrash = true ,
23
- src,
24
59
main,
25
60
requirePolyfill : polyfillRequire ,
26
- } = await findCommandKitConfig ( config ) ;
27
-
28
- const status = ora ( 'Creating optimized production build...\n' ) . start ( ) ;
29
- const start = performance . now ( ) ;
61
+ } = options ;
30
62
31
63
erase ( outDir ) ;
32
64
@@ -38,32 +70,40 @@ export async function bootstrapProductionBuild(config: any) {
38
70
skipNodeModulesBundle : true ,
39
71
minify,
40
72
shims : true ,
41
- banner : {
42
- js : '/* Optimized production build generated by CommandKit */' ,
43
- } ,
73
+ banner : options . isDevelopment
74
+ ? { }
75
+ : {
76
+ js : '/* Optimized production build generated by CommandKit */' ,
77
+ } ,
44
78
sourcemap,
45
79
keepNames : true ,
46
80
outDir,
47
81
silent : true ,
48
- watch : false ,
82
+ watch : ! ! options . isDevelopment && ! ! options . watch ,
49
83
cjsInterop : true ,
50
- splitting : false ,
51
- entry : [ src , '!dist' , '!.commandkit' , `!${ outDir } ` ] ,
52
- esbuildPlugins : [ commandkitPlugin ( ) ] ,
84
+ splitting : true ,
85
+ entry : [ 'src' , '!dist' , '!.commandkit' , `!${ outDir } ` ] ,
86
+ esbuildPlugins : [
87
+ commandkitPlugin ( {
88
+ 'jsx-importsource' : false ,
89
+ 'use-cache' : true ,
90
+ 'use-macro' : ! ! options . isDevelopment ,
91
+ } ) ,
92
+ ] ,
53
93
jsxFactory : 'CommandKit.createElement' ,
54
94
jsxFragment : 'CommandKit.Fragment' ,
55
95
async onSuccess ( ) {
56
- await copyLocaleFiles ( src , '.commandkit' ) ;
96
+ await copyLocaleFiles ( ' src' , outDir ) ;
57
97
} ,
58
98
} ) ;
59
99
60
- await injectShims ( outDir , main , antiCrash , polyfillRequire ) ;
61
-
62
- status . succeed (
63
- colors . green (
64
- `Build completed in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms!` ,
65
- ) ,
100
+ await injectShims (
101
+ outDir ,
102
+ main ,
103
+ ! options . isDevelopment && antiCrash ,
104
+ ! ! polyfillRequire ,
66
105
) ;
106
+
67
107
write (
68
108
colors . green (
69
109
`\nRun ${ colors . magenta ( `commandkit start` ) } ${ colors . green (
@@ -72,9 +112,6 @@ export async function bootstrapProductionBuild(config: any) {
72
112
) ,
73
113
) ;
74
114
} catch ( e ) {
75
- status . fail (
76
- `Build failed after ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms!` ,
77
- ) ;
78
115
panic ( e ) ;
79
116
}
80
117
}
0 commit comments