@@ -19,11 +19,11 @@ import devConfigFactory from './dev.config';
19
19
import unitConfigFactory from './unit.config' ;
20
20
import functionalConfigFactory from './functional.config' ;
21
21
import distConfigFactory from './dist.config' ;
22
+ import electronConfigFactory from './electron.config' ;
22
23
import logger from './logger' ;
23
24
import { moveBuildOptions } from './util/eject' ;
24
25
import { readFileSync } from 'fs' ;
25
26
26
- export const mainEntry = 'main' ;
27
27
const packageJsonPath = path . join ( process . cwd ( ) , 'package.json' ) ;
28
28
const packageJson = fs . existsSync ( packageJsonPath ) ? require ( packageJsonPath ) : { } ;
29
29
export const packageName = packageJson . name || '' ;
@@ -37,20 +37,19 @@ function getLibraryName(name: string) {
37
37
38
38
const libraryName = packageName ? getLibraryName ( packageName ) : 'main' ;
39
39
40
- const fixMultipleWatchTrigger = require ( 'webpack-mild-compile' ) ;
41
40
const hotMiddleware = require ( 'webpack-hot-middleware' ) ;
42
41
const connectInject = require ( 'connect-inject' ) ;
43
42
44
43
const testModes = [ 'test' , 'unit' , 'functional' ] ;
45
44
46
- function createCompiler ( config : webpack . Configuration ) {
47
- const compiler = webpack ( config ) ;
48
- fixMultipleWatchTrigger ( compiler ) ;
49
- return compiler ;
45
+ // for some reason the MultiCompiler type doesn't include hooks, even though they are clearly defined on the
46
+ // object coming back.
47
+ interface MultiCompilerWithHooks extends webpack . MultiCompiler {
48
+ hooks : webpack . compilation . CompilerHooks ;
50
49
}
51
50
52
- function createWatchCompiler ( config : webpack . Configuration ) {
53
- const compiler = createCompiler ( config ) ;
51
+ function createWatchCompiler ( configs : webpack . Configuration [ ] ) {
52
+ const compiler = webpack ( configs ) as MultiCompilerWithHooks ;
54
53
const spinner = ora ( 'building' ) . start ( ) ;
55
54
compiler . hooks . invalid . tap ( '@dojo/cli-build-app' , ( ) => {
56
55
logUpdate ( '' ) ;
@@ -83,18 +82,18 @@ function serveStatic(
83
82
}
84
83
}
85
84
86
- function build ( config : webpack . Configuration , args : any ) {
87
- const compiler = createCompiler ( config ) ;
85
+ function build ( configs : webpack . Configuration [ ] , args : any ) {
86
+ const compiler = webpack ( configs ) ;
88
87
const spinner = ora ( 'building' ) . start ( ) ;
89
- return new Promise < webpack . Compiler > ( ( resolve , reject ) => {
88
+ return new Promise < webpack . MultiCompiler > ( ( resolve , reject ) => {
90
89
compiler . run ( ( err , stats ) => {
91
90
spinner . stop ( ) ;
92
91
if ( err ) {
93
92
reject ( err ) ;
94
93
}
95
94
if ( stats ) {
96
95
const runningMessage = args . serve ? `Listening on port ${ args . port } ...` : '' ;
97
- const hasErrors = logger ( stats . toJson ( { warningsFilter } ) , config , runningMessage , args ) ;
96
+ const hasErrors = logger ( stats . toJson ( { warningsFilter } ) , configs , runningMessage , args ) ;
98
97
if ( hasErrors ) {
99
98
reject ( { } ) ;
100
99
return ;
@@ -125,10 +124,15 @@ function buildNpmDependencies(): any {
125
124
}
126
125
}
127
126
128
- async function fileWatch ( config : webpack . Configuration , args : any , shouldResolve = false ) {
129
- return new Promise < webpack . Compiler > ( ( resolve , reject ) => {
130
- const watchOptions = config . watchOptions as webpack . Compiler . WatchOptions ;
131
- const compiler = createWatchCompiler ( config ) ;
127
+ function fileWatch ( configs : webpack . Configuration [ ] , args : any , shouldResolve = false ) : Promise < webpack . MultiCompiler > {
128
+ const [ mainConfig ] = configs ;
129
+ let compiler : webpack . MultiCompiler ;
130
+
131
+ return new Promise < webpack . MultiCompiler > ( ( resolve , reject ) => {
132
+ const watchOptions = mainConfig . watchOptions as webpack . Compiler . WatchOptions ;
133
+
134
+ compiler = createWatchCompiler ( configs ) ;
135
+
132
136
compiler . watch ( watchOptions , ( err , stats ) => {
133
137
if ( err ) {
134
138
reject ( err ) ;
@@ -139,7 +143,7 @@ async function fileWatch(config: webpack.Configuration, args: any, shouldResolve
139
143
args . port
140
144
} \nPlease note the serve option is not intended to be used to serve applications in production.`
141
145
: 'watching...' ;
142
- logger ( stats . toJson ( { warningsFilter } ) , config , runningMessage , args ) ;
146
+ logger ( stats . toJson ( { warningsFilter } ) , configs , runningMessage , args ) ;
143
147
}
144
148
if ( shouldResolve ) {
145
149
resolve ( compiler ) ;
@@ -148,8 +152,9 @@ async function fileWatch(config: webpack.Configuration, args: any, shouldResolve
148
152
} ) ;
149
153
}
150
154
151
- async function serve ( config : webpack . Configuration , args : any ) {
152
- const compiler = args . watch ? await fileWatch ( config , args , true ) : await build ( config , args ) ;
155
+ async function serve ( configs : webpack . Configuration [ ] , args : any ) {
156
+ const [ mainConfig ] = configs ;
157
+
153
158
let isHttps = false ;
154
159
const base = args . base || '/' ;
155
160
@@ -162,19 +167,21 @@ async function serve(config: webpack.Configuration, args: any) {
162
167
next ( ) ;
163
168
} ) ;
164
169
165
- const outputDir = ( config . output && config . output . path ) || process . cwd ( ) ;
170
+ const compiler = args . watch ? await fileWatch ( configs , args , true ) : await build ( configs , args ) ;
171
+
172
+ const outputDir = ( mainConfig . output && mainConfig . output . path ) || process . cwd ( ) ;
166
173
let btrOptions = args [ 'build-time-render' ] ;
167
174
if ( btrOptions ) {
168
175
if ( args . singleBundle || ( args . experimental && ! ! args . experimental . speed ) ) {
169
176
btrOptions = { ...btrOptions , sync : true } ;
170
177
}
171
- const jsonpName = ( config . output && config . output . jsonpFunction ) || 'unknown' ;
178
+ const jsonpName = ( mainConfig . output && mainConfig . output . jsonpFunction ) || 'unknown' ;
172
179
const onDemandBtr = new OnDemandBtr ( {
173
180
buildTimeRenderOptions : btrOptions ,
174
181
scope : libraryName ,
175
182
base,
176
- compiler,
177
- entries : config . entry ? Object . keys ( config . entry ) : [ ] ,
183
+ compiler : compiler . compilers [ 0 ] ,
184
+ entries : mainConfig . entry ? Object . keys ( mainConfig . entry ) : [ ] ,
178
185
outputPath : outputDir ,
179
186
jsonpName
180
187
} ) ;
@@ -296,6 +303,13 @@ const command: Command = {
296
303
choices : [ 'dist' , 'dev' , 'test' , 'unit' , 'functional' ]
297
304
} ) ;
298
305
306
+ options ( 'target' , {
307
+ describe : 'the target' ,
308
+ alias : 't' ,
309
+ default : 'web' ,
310
+ choices : [ 'web' , 'electron' ]
311
+ } ) ;
312
+
299
313
options ( 'watch' , {
300
314
describe : 'watch for file changes' ,
301
315
alias : 'w'
@@ -356,31 +370,39 @@ const command: Command = {
356
370
} ,
357
371
run ( helper : Helper , args : any ) {
358
372
console . log = ( ) => { } ;
359
- let config : webpack . Configuration ;
373
+ let configs : webpack . Configuration [ ] = [ ] ;
360
374
args . experimental = args . experimental || { } ;
375
+ args . base = url . resolve ( '/' , args . base || '' ) ;
376
+ if ( ! args . base . endsWith ( '/' ) ) {
377
+ args . base = `${ args . base } /` ;
378
+ }
361
379
362
380
if ( args . mode === 'dev' ) {
363
- config = devConfigFactory ( args ) ;
381
+ configs . push ( devConfigFactory ( args ) ) ;
364
382
} else if ( args . mode === 'unit' || args . mode === 'test' ) {
365
- config = unitConfigFactory ( args ) ;
383
+ configs . push ( unitConfigFactory ( args ) ) ;
366
384
} else if ( args . mode === 'functional' ) {
367
- config = functionalConfigFactory ( args ) ;
385
+ configs . push ( functionalConfigFactory ( args ) ) ;
368
386
} else {
369
- config = distConfigFactory ( args ) ;
387
+ configs . push ( distConfigFactory ( args ) ) ;
388
+ }
389
+
390
+ if ( args . target === 'electron' ) {
391
+ configs . push ( electronConfigFactory ( args ) ) ;
370
392
}
371
393
372
394
if ( args . serve ) {
373
395
if ( testModes . indexOf ( args . mode ) !== - 1 ) {
374
396
return Promise . reject ( new Error ( `Cannot use \`--serve\` with \`--mode=${ args . mode } \`` ) ) ;
375
397
}
376
- return serve ( config , args ) ;
398
+ return serve ( configs , args ) ;
377
399
}
378
400
379
401
if ( args . watch ) {
380
- return fileWatch ( config , args ) ;
402
+ return fileWatch ( configs , args ) ;
381
403
}
382
404
383
- return build ( config , args ) ;
405
+ return build ( configs , args ) ;
384
406
} ,
385
407
eject ( helper : Helper ) : EjectOutput {
386
408
return {
0 commit comments