huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 1 | 'use strict' |
| 2 | const path = require('path') |
| 3 | const utils = require('./utils') |
| 4 | const webpack = require('webpack') |
| 5 | const config = require('../config') |
| 6 | const merge = require('webpack-merge') |
| 7 | const baseWebpackConfig = require('./webpack.base.conf') |
| 8 | const HtmlWebpackPlugin = require('html-webpack-plugin') |
| 9 | const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') |
| 10 | const portfinder = require('portfinder') |
| 11 | |
| 12 | function resolve (dir) { |
| 13 | return path.join(__dirname, '..', dir) |
| 14 | } |
| 15 | |
| 16 | const HOST = process.env.HOST |
| 17 | const PORT = process.env.PORT && Number(process.env.PORT) |
| 18 | |
| 19 | const devWebpackConfig = merge(baseWebpackConfig, { |
| 20 | module: { |
| 21 | rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true }) |
| 22 | }, |
| 23 | // cheap-module-eval-source-map is faster for development |
| 24 | devtool: config.dev.devtool, |
| 25 | |
| 26 | // these devServer options should be customized in /config/index.js |
| 27 | devServer: { |
| 28 | clientLogLevel: 'warning', |
| 29 | historyApiFallback: true, |
| 30 | hot: true, |
| 31 | compress: true, |
| 32 | host: HOST || config.dev.host, |
| 33 | port: PORT || config.dev.port, |
| 34 | open: config.dev.autoOpenBrowser, |
| 35 | overlay: config.dev.errorOverlay |
| 36 | ? { warnings: false, errors: true } |
| 37 | : false, |
| 38 | publicPath: config.dev.assetsPublicPath, |
| 39 | proxy: config.dev.proxyTable, |
| 40 | quiet: true, // necessary for FriendlyErrorsPlugin |
| 41 | watchOptions: { |
| 42 | poll: config.dev.poll, |
| 43 | } |
| 44 | }, |
| 45 | plugins: [ |
| 46 | new webpack.DefinePlugin({ |
| 47 | 'process.env': require('../config/dev.env') |
| 48 | }), |
| 49 | new webpack.HotModuleReplacementPlugin(), |
| 50 | new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update. |
| 51 | new webpack.NoEmitOnErrorsPlugin(), |
| 52 | // https://github.com/ampedandwired/html-webpack-plugin |
| 53 | new HtmlWebpackPlugin({ |
| 54 | filename: 'index.html', |
| 55 | template: 'index.html', |
| 56 | inject: true, |
| 57 | favicon: resolve('favicon.ico'), |
| 58 | title: 'vue-element-admin' |
| 59 | }), |
| 60 | ] |
| 61 | }) |
| 62 | |
| 63 | module.exports = new Promise((resolve, reject) => { |
| 64 | portfinder.basePort = process.env.PORT || config.dev.port |
| 65 | portfinder.getPort((err, port) => { |
| 66 | if (err) { |
| 67 | reject(err) |
| 68 | } else { |
| 69 | // publish the new Port, necessary for e2e tests |
| 70 | process.env.PORT = port |
| 71 | // add port to devServer config |
| 72 | devWebpackConfig.devServer.port = port |
| 73 | |
| 74 | // Add FriendlyErrorsPlugin |
| 75 | devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ |
| 76 | compilationSuccessInfo: { |
| 77 | messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`], |
| 78 | }, |
| 79 | onErrors: config.dev.notifyOnErrors |
| 80 | ? utils.createNotifierCallback() |
| 81 | : undefined |
| 82 | })) |
| 83 | |
| 84 | resolve(devWebpackConfig) |
| 85 | } |
| 86 | }) |
| 87 | }) |