@@ -5,12 +5,14 @@ import path = require('path');
55const Listr = require ( 'listr' ) ;
66const cpy = require ( 'cpy' ) ;
77const del = require ( 'del' ) ;
8+ const fs = require ( 'fs' ) ;
89
910import { findSubmodules , tasksWatch } from '../utils' ;
10- import { build , bundleUmd , buildPkgs } from '../tasks' ;
11+ import { build , bundleUmd , buildPkgs , bundleEs2015 } from '../tasks' ;
1112import { inlineResources } from '../helpers/inline-resources' ;
1213
13- export function buildCommand ( { project, verbose, clean, local, main, watch, skipBundles} ) {
14+
15+ export function buildCommand ( { project, verbose, clean, local, main, watch, skipBundles, buildEs2015} ) {
1416 // 1. clean dist folders
1517 // 2.1 merge pkg json
1618 // todo: 2.2 validate pkg (main, module, types fields)
@@ -119,14 +121,28 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
119121 task : ( ) => new Listr (
120122 opts . map ( opt => ( {
121123 title : `Bundling ${ opt . pkg . name } ` ,
122- task : ( ) => bundleUmd ( {
123- main,
124- src : opt . tmp ,
125- dist : opt . dist ,
126- name : opt . pkg . name ,
127- tsconfig : opt . tsconfig . path ,
128- minify : false
129- } )
124+ task : ( ) => {
125+ if ( Array . isArray ( main ) && main . length ) {
126+ return Promise . all ( main . map ( ( entryPoint , i ) => bundleUmd ( {
127+ main : entryPoint ,
128+ src : opt . tmp ,
129+ dist : opt . dist ,
130+ name : i === 0 ? opt . pkg . name : entryPoint . replace ( '.ts' , '' ) ,
131+ tsconfig : opt . tsconfig . path ,
132+ minify : false
133+ } ) ) )
134+ }
135+
136+ return bundleUmd ( {
137+ main,
138+ src : opt . tmp ,
139+ dist : opt . dist ,
140+ name : opt . pkg . name ,
141+ tsconfig : opt . tsconfig . path ,
142+ minify : false
143+ } )
144+ } ,
145+ skip : ( ) => watch || skipBundles
130146 } ) )
131147 ) ,
132148 skip : ( ) => watch || skipBundles
@@ -136,18 +152,65 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
136152 task : ( ) => new Listr (
137153 opts . map ( opt => ( {
138154 title : `Bundling ${ opt . pkg . name } ` ,
139- task : ( ) => bundleUmd ( {
140- main,
141- src : opt . tmp ,
142- dist : opt . dist ,
143- name : opt . pkg . name ,
144- tsconfig : opt . tsconfig . path ,
145- minify : true
146- } )
155+ task : ( ) => {
156+ if ( Array . isArray ( main ) && main . length ) {
157+ return Promise . all ( main . map ( ( entryPoint , i ) => bundleUmd ( {
158+ main : entryPoint ,
159+ src : opt . tmp ,
160+ dist : opt . dist ,
161+ name : i === 0 ? opt . pkg . name : entryPoint . replace ( '.ts' , '' ) ,
162+ tsconfig : opt . tsconfig . path ,
163+ minify : true
164+ } ) ) )
165+ }
166+
167+ return bundleUmd ( {
168+ main,
169+ src : opt . tmp ,
170+ dist : opt . dist ,
171+ name : opt . pkg . name ,
172+ tsconfig : opt . tsconfig . path ,
173+ minify : true
174+ } )
175+ } ,
176+ skip : ( ) => watch || skipBundles
147177 } ) )
148178 ) ,
149179 skip : ( ) => watch || skipBundles
150180 } ,
181+ {
182+ title : 'Bundling es2015 version' ,
183+ task : ( ) => new Listr (
184+ opts . map ( opt => ( {
185+ title : `Bundling ${ opt . pkg . name } ` ,
186+ task : ( ) => {
187+ const tsconfig = JSON . parse ( fs . readFileSync ( path . resolve ( opt . tmp , 'tsconfig.json' ) , 'utf8' ) ) ;
188+ tsconfig . compilerOptions . target = 'es2015' ;
189+ tsconfig . compilerOptions . module = 'es2015' ;
190+ tsconfig . compilerOptions . outDir = 'dist-es2015' ;
191+ fs . writeFileSync ( path . resolve ( opt . tmp , 'tsconfig.json' ) , JSON . stringify ( tsconfig ) , 'utf8' ) ;
192+
193+ if ( Array . isArray ( main ) && main . length ) {
194+ return Promise . all ( main . map ( ( entryPoint , i ) => bundleEs2015 ( {
195+ input : entryPoint ,
196+ dist : opt . dist ,
197+ name : i === 0 ? opt . pkg . name : entryPoint . replace ( '.ts' , '' ) ,
198+ tmp : opt . tmp
199+ } ) ) )
200+ }
201+
202+ return bundleEs2015 ( {
203+ input : main ,
204+ dist : opt . dist ,
205+ name : opt . pkg . name ,
206+ tmp : opt . tmp
207+ } ) ;
208+ } ,
209+ skip : ( ) => ! buildEs2015 || watch || skipBundles
210+ } ) )
211+ ) ,
212+ skip : ( ) => ! buildEs2015 || watch || skipBundles
213+ } ,
151214 {
152215 title : 'Clean .tmp folders' ,
153216 task : ( ) => new Listr (
@@ -161,8 +224,34 @@ export function buildCommand({project, verbose, clean, local, main, watch, skipB
161224}
162225
163226export function buildTsRun ( cli ) {
164- const { project, watch, verbose, clean, local, skipBundles} = cli . flags ;
165- let main = cli . flags . main || 'index.ts' ;
166- return buildCommand ( { project, verbose, clean, local, main, watch, skipBundles} )
167- . then ( tasks => tasksWatch ( { project, tasks, watch} ) ) ;
227+ const config = cli . flags . config ? JSON . parse ( fs . readFileSync ( path . resolve ( cli . flags . config ) , 'utf8' ) ) : { } ;
228+ let { src, main, modules, watch, verbose, clean, local, skipBundles, buildEs2015} = config ;
229+ const project = cli . flags . project || src ;
230+ main = cli . flags . main || main || 'index.ts' ;
231+ verbose = cli . flags . verbose || verbose ;
232+ watch = cli . flags . watch || watch ;
233+ clean = cli . flags . clean || clean ;
234+ skipBundles = cli . flags . skipBundles || skipBundles ;
235+ buildEs2015 = cli . flags . buildEs2015 || buildEs2015 ;
236+ const modulePaths = modules ? modules . map ( module => module . src ) : [ ] ;
237+
238+ if ( ! project && ! modulePaths ) {
239+ console . error ( 'Please provide path to your projects source folder, `-p DIR` or specify `src` or `modules` in config' ) ;
240+ process . exit ( 1 ) ;
241+ }
242+
243+ if ( modulePaths . length ) {
244+ const commands = modules . map ( ( module : any ) => {
245+ return {
246+ title : `Build ${ module . src } ` ,
247+ task : ( ) => buildCommand ( { project : module . src , verbose, clean, local, main : module . entryPoints , watch, skipBundles, buildEs2015} )
248+ }
249+ } ) ;
250+
251+ const taskQueue = new Listr ( commands , { renderer : verbose ? 'verbose' : 'default' } ) ;
252+ return tasksWatch ( { project : modulePaths , taskQueue, watch, paths : modulePaths } ) ;
253+ }
254+
255+ return buildCommand ( { project, verbose, clean, local, main, watch, skipBundles, buildEs2015} )
256+ . then ( taskQueue => tasksWatch ( { project, taskQueue, watch, paths : null } ) ) ;
168257}
0 commit comments