webpack.dev.conf.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. function resolve(dir) {
  12. return path.join(__dirname, '..', dir)
  13. }
  14. const HOST = process.env.HOST
  15. const PORT = process.env.PORT && Number(process.env.PORT)
  16. const devWebpackConfig = merge(baseWebpackConfig, {
  17. mode: 'development',
  18. module: {
  19. rules: utils.styleLoaders({
  20. sourceMap: config.dev.cssSourceMap,
  21. usePostCSS: true
  22. })
  23. },
  24. // cheap-module-eval-source-map is faster for development
  25. devtool: config.dev.devtool,
  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. // https://github.com/ampedandwired/html-webpack-plugin
  51. new HtmlWebpackPlugin({
  52. filename: 'index.html',
  53. template: 'index.html',
  54. inject: true,
  55. favicon: resolve('favicon.ico'),
  56. title: 'vue-element-admin',
  57. templateParameters: {
  58. BASE_URL: config.dev.assetsPublicPath + config.dev.assetsSubDirectory,
  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. // Add FriendlyErrorsPlugin
  74. devWebpackConfig.plugins.push(
  75. new FriendlyErrorsPlugin({
  76. compilationSuccessInfo: {
  77. messages: [
  78. `Your application is running here: http://${
  79. devWebpackConfig.devServer.host
  80. }:${port}`
  81. ]
  82. },
  83. onErrors: config.dev.notifyOnErrors
  84. ? utils.createNotifierCallback()
  85. : undefined
  86. })
  87. )
  88. resolve(devWebpackConfig)
  89. }
  90. })
  91. })