@@ -10,23 +10,55 @@ import {
1010 panic ,
1111 write ,
1212} from './common.js' ;
13- import ora from 'ora' ;
1413import { commandkitPlugin } from './esbuild-plugins/plugin' ;
1514import colors from '../utils/colors.js' ;
15+ import { createSpinner } from './utils' ;
16+ import { BuildOptions } from './types' ;
1617
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 ) {
1854 const {
1955 sourcemap = false ,
2056 minify = false ,
2157 outDir = 'dist' ,
2258 antiCrash = true ,
23- src,
2459 main,
2560 requirePolyfill : polyfillRequire ,
26- } = await findCommandKitConfig ( config ) ;
27-
28- const status = ora ( 'Creating optimized production build...\n' ) . start ( ) ;
29- const start = performance . now ( ) ;
61+ } = options ;
3062
3163 erase ( outDir ) ;
3264
@@ -38,32 +70,40 @@ export async function bootstrapProductionBuild(config: any) {
3870 skipNodeModulesBundle : true ,
3971 minify,
4072 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+ } ,
4478 sourcemap,
4579 keepNames : true ,
4680 outDir,
4781 silent : true ,
48- watch : false ,
82+ watch : ! ! options . isDevelopment && ! ! options . watch ,
4983 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+ ] ,
5393 jsxFactory : 'CommandKit.createElement' ,
5494 jsxFragment : 'CommandKit.Fragment' ,
5595 async onSuccess ( ) {
56- await copyLocaleFiles ( src , '.commandkit' ) ;
96+ await copyLocaleFiles ( ' src' , outDir ) ;
5797 } ,
5898 } ) ;
5999
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 ,
66105 ) ;
106+
67107 write (
68108 colors . green (
69109 `\nRun ${ colors . magenta ( `commandkit start` ) } ${ colors . green (
@@ -72,9 +112,6 @@ export async function bootstrapProductionBuild(config: any) {
72112 ) ,
73113 ) ;
74114 } catch ( e ) {
75- status . fail (
76- `Build failed after ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms!` ,
77- ) ;
78115 panic ( e ) ;
79116 }
80117}
0 commit comments