UNPKG

ae-biu

Version:

Born For AE, Born To Do

140 lines (120 loc) 4.22 kB
#!/usr/bin/env node // @flow import program from 'commander' import webpack from 'webpack' import logger from './utils/logger' import clearLine from './utils/clear-line' import Loading from './utils/loading' import getWebpackConfig from './utils/get-webpack-config' import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' import serve from 'serve' import rimraf from 'rimraf' /** * Usage */ program .usage('[options]') .option('-a, --analyzer', 'start bundle analyzer after build') .option('-p, --port', 'set static server port') .parse(process.argv) const args = program.args let port let sdpEnv const { analyzer } = program for (let i in args) { if (/\d+/.test(args[i])) { port = args[i] } else { sdpEnv = args[i] } } // must under production // for node process.env.NODE_ENV = 'production' // for sdp, it can be override by project process.env.SDP_ENV = (sdpEnv || process.env.SDP_ENV || process.env.npm_config_sdp_env || 'product').trim() // set to product, no why // for static server const PORT = port || (process.env.PORT || process.env.npm_config_port || '8080').trim() // ensure create config after env let webpackConfig: Object = require('./config/webpack.config.prod') const projectConfig = getWebpackConfig() if (typeof projectConfig === 'function') { webpackConfig = projectConfig(webpackConfig) } else if (projectConfig) { webpackConfig = projectConfig } const distDir = webpackConfig.output.path // apply BundleAnalyzerPlugin if (analyzer) { webpackConfig.plugins.push( new BundleAnalyzerPlugin({ // Can be `server`, `static` or `disabled`. // In `server` mode analyzer will start HTTP server to show bundle report. // In `static` mode single HTML file with bundle report will be generated. // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`. analyzerMode: 'server', // Host that will be used in `server` mode to start HTTP server. analyzerHost: '127.0.0.1', // Port that will be used in `server` mode to start HTTP server. analyzerPort: 8888, // Path to bundle report file that will be generated in `static` mode. // Relative to bundles output directory. reportFilename: 'report.html', // Automatically open report in default browser openAnalyzer: true, // If `true`, Webpack Stats JSON file will be generated in bundles output directory generateStatsFile: false, // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`. // Relative to bundles output directory. statsFilename: 'stats.json', // Options for `stats.toJson()` method. // For example you can exclude sources of your modules from stats file with `source: false` option. // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21 statsOptions: null, // Log level. Can be 'info', 'warn', 'error' or 'silent'. logLevel: 'info' }) ) } logger.info('Clean dist files\n') rimraf.sync(distDir) const compiler = webpack(webpackConfig) const loading = new Loading() // logger.info('Compiling\n') console.log() loading.start() compiler.run((err, stats) => { loading.clear() clearLine() console.log() logger.info('Webpack compile completed.\n') if (err) { logger.error('Webpack compiler encountered a fatal error.\n', err) process.exit(1) } const jsonStats = stats.toJson() console.log(stats.toString({ modules: false, children: false, chunks: false, chunkModules: false, colors: true })) if (jsonStats.errors.length > 0) { logger.error('Webpack compiler encountered errors.\n') console.log(jsonStats.errors) process.exit(1) } else if (jsonStats.warnings.length > 0) { logger.warning('Webpack compiler encountered warnings.\n') } else { logger.success('No errors or warnings encountered.\n') } const server = serve(distDir, { port: PORT }) logger.info('Static server now is running at http://0.0.0.0:' + PORT) program.on('exit', () => { logger.info('Static server now is closed') server.stop() }) })