webpack.private.dev.conf.js 3.1 KB

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