webpack.dev.conf.js 3.1 KB

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