@@ -10,26 +10,29 @@ const serverConfig = require('./webpack-server-config');
1010const transformConfig = require ( './transform-config' ) ;
1111const { error, isDir, warn } = require ( '../../util' ) ;
1212
13- async function devBuild ( env ) {
14- let config = await clientConfig ( env ) ;
13+ /**
14+ * @param {import('../../../types').Env } env
15+ */
16+ async function devBuild ( config , env ) {
17+ const webpackConfig = await clientConfig ( config , env ) ;
1518
16- await transformConfig ( env , config ) ;
19+ await transformConfig ( webpackConfig , config , env ) ;
1720
18- let compiler = webpack ( config ) ;
21+ let compiler = webpack ( webpackConfig ) ;
1922 return new Promise ( ( res , rej ) => {
2023 compiler . hooks . beforeCompile . tap ( 'CliDevPlugin' , ( ) => {
2124 if ( env [ 'clear' ] ) clear ( true ) ;
2225 } ) ;
2326
2427 compiler . hooks . done . tap ( 'CliDevPlugin' , stats => {
25- let devServer = config . devServer ;
28+ let devServer = webpackConfig . devServer ;
2629 let protocol = process . env . HTTPS || devServer . https ? 'https' : 'http' ;
2730 let host = process . env . HOST || devServer . host || 'localhost' ;
2831 if ( host === '0.0.0.0' && process . platform === 'win32' ) {
2932 host = 'localhost' ;
3033 }
31- let serverAddr = `${ protocol } ://${ host } :${ bold ( env . port ) } ` ;
32- let localIpAddr = `${ protocol } ://${ ip . address ( ) } :${ bold ( env . port ) } ` ;
34+ let serverAddr = `${ protocol } ://${ host } :${ bold ( config . port ) } ` ;
35+ let localIpAddr = `${ protocol } ://${ ip . address ( ) } :${ bold ( config . port ) } ` ;
3336
3437 if ( stats . hasErrors ( ) ) {
3538 process . stdout . write ( red ( 'Build failed!\n\n' ) ) ;
@@ -45,26 +48,28 @@ async function devBuild(env) {
4548
4649 compiler . hooks . failed . tap ( 'CliDevPlugin' , rej ) ;
4750
48- let server = new DevServer ( config . devServer , compiler ) ;
51+ let server = new DevServer ( webpackConfig . devServer , compiler ) ;
4952 server . start ( ) ;
5053 res ( server ) ;
5154 } ) ;
5255}
5356
54- async function prodBuild ( env ) {
55- env = { ...env , isServer : false , dev : ! env . production , ssr : false } ;
56- let config = await clientConfig ( env ) ;
57- await transformConfig ( env , config ) ;
57+ /**
58+ * @param {import('../../../types').Env } env
59+ */
60+ async function prodBuild ( config , env ) {
61+ if ( config . prerender ) {
62+ const serverEnv = { ...env , isServer : true } ;
5863
59- if ( env . prerender ) {
60- const serverEnv = Object . assign ( { } , env , { isServer : true , ssr : true } ) ;
61- let ssrConfig = serverConfig ( serverEnv ) ;
62- await transformConfig ( serverEnv , ssrConfig ) ;
63- let serverCompiler = webpack ( ssrConfig ) ;
64+ const serverWebpackConfig = serverConfig ( config , serverEnv ) ;
65+ await transformConfig ( serverWebpackConfig , config , serverEnv ) ;
66+ const serverCompiler = webpack ( serverWebpackConfig ) ;
6467 await runCompiler ( serverCompiler ) ;
6568 }
6669
67- let clientCompiler = webpack ( config ) ;
70+ const clientWebpackConfig = await clientConfig ( config , env ) ;
71+ await transformConfig ( clientWebpackConfig , config , env ) ;
72+ const clientCompiler = webpack ( clientWebpackConfig ) ;
6873
6974 try {
7075 let stats = await runCompiler ( clientCompiler ) ;
@@ -220,20 +225,26 @@ function stripLoaderFromModuleNames(m) {
220225 return m ;
221226}
222227
223- module . exports = function ( env , watch = false ) {
224- env . isProd = env . production ; // shorthand
225- env . isWatch = ! ! watch ; // use HMR?
226- env . cwd = resolve ( env . cwd || process . cwd ( ) ) ;
227-
228- // env.src='src' via `build` default
229- let src = resolve ( env . cwd , env . src ) ;
230- env . src = isDir ( src ) ? src : env . cwd ;
228+ /**
229+ * @param {boolean } isProd
230+ */
231+ module . exports = function ( argv , isProd ) {
232+ const env = {
233+ isProd,
234+ isWatch : ! isProd ,
235+ isServer : false ,
236+ } ;
237+ const config = argv ;
238+ config . cwd = resolve ( argv . cwd || process . cwd ( ) ) ;
239+
240+ // config.src='src' via `build` default
241+ const src = resolve ( config . cwd , argv . src ) ;
242+ config . src = isDir ( src ) ? src : config . cwd ;
231243
232244 // attach sourcing helper
233- env . source = dir => resolve ( env . src , dir ) ;
245+ config . source = dir => resolve ( config . src , dir ) ;
234246
235- // determine build-type to run
236- return ( watch ? devBuild : prodBuild ) ( env ) ;
247+ return ( isProd ? prodBuild : devBuild ) ( config , env ) ;
237248} ;
238249
239250module . exports . writeJsonStats = writeJsonStats ;
0 commit comments