webpack.dev.conf.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. headers:{
  23. 'Access-Control-Allow-Origin':'*',
  24. },
  25. clientLogLevel: 'warning',
  26. historyApiFallback: {
  27. rewrites: [
  28. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  29. ],
  30. },
  31. hot: true,
  32. contentBase: false, // since we use CopyWebpackPlugin.
  33. compress: true,
  34. host: HOST || config.dev.host,
  35. port: PORT || config.dev.port,
  36. open: config.dev.autoOpenBrowser,
  37. overlay: config.dev.errorOverlay
  38. ? { warnings: false, errors: true }
  39. : false,
  40. publicPath: config.dev.assetsPublicPath,
  41. proxy: config.dev.proxyTable,
  42. quiet: true, // necessary for FriendlyErrorsPlugin
  43. watchOptions: {
  44. poll: config.dev.poll,
  45. }
  46. },
  47. plugins: [
  48. new webpack.DefinePlugin({
  49. 'process.env': require('../config/dev.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. })