webpack.dev.conf.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 = '192.168.0.172'
  13. const HOST = '0.0.0.0'
  14. const PORT = process.env.PORT && Number(process.env.PORT)
  15. const devWebpackConfig = merge(baseWebpackConfig, {
  16. module: {
  17. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  18. },
  19. // cheap-module-eval-source-map is faster for development
  20. devtool: config.dev.devtool,
  21. // these devServer options should be customized in /config/index.js
  22. devServer: {
  23. clientLogLevel: 'warning',
  24. historyApiFallback: {
  25. rewrites: [
  26. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  27. ],
  28. },
  29. hot: true,
  30. contentBase: false, // since we use CopyWebpackPlugin.
  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. proxy: {
  45. '/api': {
  46. target: 'https://test.4dkankan.com',
  47. changeOrigin: true,
  48. }
  49. }
  50. },
  51. plugins: [
  52. new webpack.DefinePlugin({
  53. 'process.env': require('../config/dev.env')
  54. }),
  55. new webpack.HotModuleReplacementPlugin(),
  56. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  57. new webpack.NoEmitOnErrorsPlugin(),
  58. // https://github.com/ampedandwired/html-webpack-plugin
  59. new HtmlWebpackPlugin({
  60. filename: 'index.html',
  61. template: 'index.html',
  62. inject: true
  63. }),
  64. // copy custom static assets
  65. new CopyWebpackPlugin([
  66. {
  67. from: path.resolve(__dirname, '../static'),
  68. to: config.dev.assetsSubDirectory,
  69. ignore: ['.*']
  70. }
  71. ])
  72. ]
  73. })
  74. module.exports = new Promise((resolve, reject) => {
  75. portfinder.basePort = process.env.PORT || config.dev.port
  76. portfinder.getPort((err, port) => {
  77. if (err) {
  78. reject(err)
  79. } else {
  80. // publish the new Port, necessary for e2e tests
  81. process.env.PORT = port
  82. // add port to devServer config
  83. devWebpackConfig.devServer.port = port
  84. // Add FriendlyErrorsPlugin
  85. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  86. compilationSuccessInfo: {
  87. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  88. },
  89. onErrors: config.dev.notifyOnErrors
  90. ? utils.createNotifierCallback()
  91. : undefined
  92. }))
  93. resolve(devWebpackConfig)
  94. }
  95. })
  96. })