webpack.dev.conf.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. const HOST = process.env.HOST
  13. const PORT = process.env.PORT && Number(process.env.PORT)
  14. const devWebpackConfig = merge(baseWebpackConfig, {
  15. module: {
  16. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  17. },
  18. // cheap-module-eval-source-map is faster for development
  19. devtool: config.dev.devtool,
  20. // these devServer options should be customized in /config/index.js
  21. devServer: {
  22. clientLogLevel: 'warning',
  23. historyApiFallback: {
  24. rewrites: [
  25. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  26. ],
  27. },
  28. hot: true,
  29. contentBase: false, // since we use CopyWebpackPlugin.
  30. compress: true,
  31. host: HOST || config.dev.host,
  32. port: PORT || config.dev.port,
  33. open: config.dev.autoOpenBrowser,
  34. overlay: config.dev.errorOverlay
  35. ? { warnings: false, errors: true }
  36. : false,
  37. publicPath: config.dev.assetsPublicPath,
  38. proxy: config.dev.proxyTable,
  39. quiet: true, // necessary for FriendlyErrorsPlugin
  40. watchOptions: {
  41. poll: config.dev.poll,
  42. },
  43. proxy: {
  44. '/api': {
  45. target: 'https://admin.zhifangbao.com',
  46. changeOrigin: true,
  47. pathRewrite: { // 如果接口本身没有/api需要通过pathRewrite来重写了地址,这里把/api转成‘ ’
  48. "^/api": ""
  49. }
  50. },
  51. }
  52. },
  53. plugins: [
  54. new webpack.DefinePlugin({
  55. 'process.env': require('../config/dev.env')
  56. }),
  57. new webpack.ProvidePlugin({
  58. 'window.Quill': 'quill'
  59. }),
  60. new webpack.HotModuleReplacementPlugin(),
  61. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  62. new webpack.NoEmitOnErrorsPlugin(),
  63. // https://github.com/ampedandwired/html-webpack-plugin
  64. new HtmlWebpackPlugin({
  65. filename: 'index.html',
  66. template: 'index.html',
  67. inject: true
  68. }),
  69. // copy custom static assets
  70. new CopyWebpackPlugin([
  71. {
  72. from: path.resolve(__dirname, '../static'),
  73. to: config.dev.assetsSubDirectory,
  74. ignore: ['.*']
  75. }
  76. ])
  77. ]
  78. })
  79. module.exports = new Promise((resolve, reject) => {
  80. portfinder.basePort = process.env.PORT || config.dev.port
  81. portfinder.getPort((err, port) => {
  82. if (err) {
  83. reject(err)
  84. } else {
  85. // publish the new Port, necessary for e2e tests
  86. process.env.PORT = port
  87. // add port to devServer config
  88. devWebpackConfig.devServer.port = port
  89. // Add FriendlyErrorsPlugin
  90. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  91. compilationSuccessInfo: {
  92. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  93. },
  94. onErrors: config.dev.notifyOnErrors
  95. ? utils.createNotifierCallback()
  96. : undefined
  97. }))
  98. resolve(devWebpackConfig)
  99. }
  100. })
  101. })