From a842a4b451571195a6d2453ca77fe8a4da2a3c5f Mon Sep 17 00:00:00 2001 From: Pan Date: Fri, 29 Sep 2017 13:39:42 +0800 Subject: [PATCH] refine:refine build_config --- build/build.js | 4 +--- build/dev-server.js | 41 ++++++++++++++++++++++---------------- build/webpack.base.conf.js | 16 +++++++-------- build/webpack.prod.conf.js | 4 +++- config/dev.env.js | 1 + config/prod.env.js | 1 + config/sit.env.js | 1 + package.json | 6 +++--- 8 files changed, 42 insertions(+), 32 deletions(-) diff --git a/build/build.js b/build/build.js index da9522a1..2041892b 100644 --- a/build/build.js +++ b/build/build.js @@ -9,9 +9,7 @@ var webpack = require('webpack'); var config = require('../config'); var webpackConfig = require('./webpack.prod.conf'); -console.log(process.env.NODE_ENV) - -var spinner = ora('building for ' + process.env.NODE_ENV + '...') +var spinner = ora('building for ' + process.env.NODE_ENV + ' of ' + process.env.env_config+ ' mode...' ) spinner.start() diff --git a/build/dev-server.js b/build/dev-server.js index 34c32b3c..254a3bb1 100644 --- a/build/dev-server.js +++ b/build/dev-server.js @@ -1,4 +1,5 @@ require('./check-versions')(); // 检查 Node 和 npm 版本 + var config = require('../config'); if (!process.env.NODE_ENV) { process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV) @@ -28,8 +29,8 @@ var devMiddleware = require('webpack-dev-middleware')(compiler, { }); var hotMiddleware = require('webpack-hot-middleware')(compiler, { - log: () => { - } + log: false, + heartbeat: 2000 }); // force page reload when html-webpack-plugin template changes @@ -40,8 +41,6 @@ compiler.plugin('compilation', function (compilation) { }) }); -// compiler.apply(new DashboardPlugin()); - // proxy api requests Object.keys(proxyTable).forEach(function (context) { var options = proxyTable[context] @@ -67,18 +66,26 @@ app.use(staticPath, express.static('./static')); var uri = 'http://localhost:' + port -devMiddleware.waitUntilValid(function () { - console.log('> Listening at ' + uri + '\n') -}); +var _resolve +var readyPromise = new Promise(resolve => { + _resolve = resolve +}) -module.exports = app.listen(port, function (err) { - if (err) { - console.log(err); - return - } +console.log('> Starting dev server...') +devMiddleware.waitUntilValid(() => { + console.log('> Listening at ' + uri + '\n') + // when env is testing, don't need open it + if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') { + opn(uri) + } + _resolve() +}) - // when env is testing, don't need open it - if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') { - opn(uri) - } -}); +var server = app.listen(port) + +module.exports = { + ready: readyPromise, + close: () => { + server.close() + } +} diff --git a/build/webpack.base.conf.js b/build/webpack.base.conf.js index d82a880c..0fd53f61 100644 --- a/build/webpack.base.conf.js +++ b/build/webpack.base.conf.js @@ -14,7 +14,7 @@ module.exports = { output: { path: config.build.assetsRoot, filename: '[name].js', - publicPath: process.env.NODE_ENV !== 'development' ? config.build.assetsPublicPath : config.dev.assetsPublicPath + publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath }, resolve: { extensions: ['.js', '.vue', '.json'], @@ -38,13 +38,13 @@ module.exports = { module: { rules: [ { - test: /\.(js|vue)$/, - loader: 'eslint-loader', - enforce: "pre", - include: [resolve('src'), resolve('test')], - options: { - formatter: require('eslint-friendly-formatter') - } + test: /\.(js|vue)$/, + loader: 'eslint-loader', + enforce: "pre", + include: [resolve('src'), resolve('test')], + options: { + formatter: require('eslint-friendly-formatter') + } }, { test: /\.vue$/, diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index d69d0e1d..9b20a51b 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -9,7 +9,7 @@ var HtmlWebpackPlugin = require('html-webpack-plugin') var ExtractTextPlugin = require('extract-text-webpack-plugin') var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') -var env = process.env.NODE_ENV === 'production' ? config.build.prodEnv : config.build.sitEnv +var env = config.build[process.env.config_env+'Env'] function resolveApp(relativePath) { return path.resolve(relativePath); @@ -117,9 +117,11 @@ var webpackConfig = merge(baseWebpackConfig, { }]) ] }) + if (config.build.bundleAnalyzerReport) { var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin webpackConfig.plugins.push(new BundleAnalyzerPlugin()) } + module.exports = webpackConfig diff --git a/config/dev.env.js b/config/dev.env.js index e26486cd..f4aeda50 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -1,5 +1,6 @@ module.exports = { NODE_ENV: '"development"', + ENV_CONFIG: '"dev"', BASE_API: '"https://api-dev"', APP_ORIGIN: '"https://wallstreetcn.com"' } diff --git a/config/prod.env.js b/config/prod.env.js index cc935b9a..5cc5b18c 100644 --- a/config/prod.env.js +++ b/config/prod.env.js @@ -1,5 +1,6 @@ module.exports = { NODE_ENV: '"production"', + ENV_MODE: '"prod"', BASE_API: '"https://api-prod"', APP_ORIGIN: '"https://wallstreetcn.com"' }; diff --git a/config/sit.env.js b/config/sit.env.js index 64cf403b..a9a041af 100644 --- a/config/sit.env.js +++ b/config/sit.env.js @@ -1,5 +1,6 @@ module.exports = { NODE_ENV: '"production"', + ENV_CONFIG: '"sit"', BASE_API: '"https://api-sit"', APP_ORIGIN: '"https://wallstreetcn.com"' }; diff --git a/package.json b/package.json index d1498c15..7d075335 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,9 @@ "private": true, "scripts": { "dev": "node build/dev-server.js", - "build:prod": "cross-env NODE_ENV=production node build/build.js", - "build:sit": "cross-env NODE_ENV=sit node build/build.js", - "build:sit-preview": "cross-env NODE_ENV=sit npm_config_preview=true npm_config_report=true node build/build.js", + "build:prod": "cross-env NODE_ENV=production env_config=prod node build/build.js", + "build:sit": "cross-env NODE_ENV=production env_config=sit node build/build.js", + "build:sit-preview": "cross-env NODE_ENV=sit env_config=sit npm_config_preview=true npm_config_report=true node build/build.js", "lint": "eslint --ext .js,.vue src" }, "dependencies": {