import webpack from 'webpack' ; import resolve from 'resolve' ; import path from 'path' ; import { GojiWebpackPluginOptions } from '@goji/webpack-plugin' ; import { getWebpackConfig } from './config/webpack.config' ; import { parseArgv , CliConfig } from './argv' ; import './utils/fixSerializationWarning' ; interface GojiConfig { watch ? : boolean ; outputPath ? : string ; configureWebpack ? : ( config : webpack . Configuration , webpackInGoji : typeof webpack ) => undefined ; configureBabel ? : ( config : any ) => any ; progress ? : boolean ; nohoist ? : GojiWebpackPluginOptions [ 'nohoist' ] ; } const GOJI_CONFIG_FILE_NAME = 'goji.config' ; const requireGojiConfig = ( basedir ) : GojiConfig => { let resolvedPath ; try { resolvedPath = resolve . sync ( path . join ( basedir , GOJI_CONFIG_FILE_NAME ) ) ; } catch ( error ) { console . info ( `\`goji.config.js\` not found in folder ${ basedir }, using default config.` ) ; return { } ; } return require ( resolvedPath ) ; } ; const main = async ( ) => { const basedir = process . cwd ( ) ; let cliConfig ; try { cliConfig = await parseArgv ( process . argv . slice ( 2 ) ) ; } catch ( error ) { console . error ( ( error as Error ) . message ) ; return ; } process . env . NODE_ENV = cliConfig . production ? 'production' : 'development' ; process . env . GOJI_TARGET = cliConfig . target ; const gojiConfig = requireGojiConfig ( basedir ) ; const babelConfig = require ( './config/babel.config' ) ; if ( gojiConfig . configureBabel ) { gojiConfig . configureBabel ( babelConfig ) ; } const watch = cliConfig . watch ?? gojiConfig . watch ?? ! cliConfig . production ; const webpackConfig = getWebpackConfig ( { basedir , outputPath : gojiConfig . outputPath , target : cliConfig . target , nodeEnv : cliConfig . production ? 'production' : 'development' , babelConfig , watch , progress : gojiConfig . progress ?? cliConfig . progress ?? true , nohoist : gojiConfig ?. nohoist , } ) ; if ( gojiConfig . configureWebpack ) { gojiConfig . configureWebpack ( webpackConfig , webpack ) ; } const compilerCallback = ( err ? , stats ? ) => { if ( err ) { console . error ( err ) ; process . exit ( 2 ) ; } if ( stats ?. hasErrors ( ) ) { process . exitCode = 1 ; } const outputOptions = webpackConfig . stats ; const printedStats = stats ! . toString ( outputOptions ) ; console . log ( printedStats ) ; } ; if ( watch ) { console . log ( 'Start GojiJS in development mode.' ) ; } else { console . log ( 'Start GojiJS in production mode.' ) ; } webpack ( webpackConfig , compilerCallback ) ; } ; main ( ) . catch ( e => console . error ( e ) ) ;