webpack.dev.conf.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 vConsolePlugin = require('vconsole-webpack-plugin')
  11. const portfinder = require('portfinder')
  12. function resolve(dir) {
  13. return path.join(__dirname, '..', dir)
  14. }
  15. const HOST = process.env.HOST
  16. const PORT = process.env.PORT && Number(process.env.PORT)
  17. const devWebpackConfig = merge(baseWebpackConfig, {
  18. mode: 'development',
  19. optimization: {
  20. removeAvailableModules: false
  21. },
  22. module: {
  23. rules: utils.styleLoaders({
  24. sourceMap: config.dev.cssSourceMap,
  25. usePostCSS: true
  26. })
  27. },
  28. // cheap-module-eval-source-map is faster for development
  29. devtool: config.dev.devtool,
  30. // these devServer options should be customized in /config/index.js
  31. devServer: {
  32. clientLogLevel: 'warning',
  33. historyApiFallback: true,
  34. hot: true,
  35. compress: true,
  36. host: HOST || config.dev.host,
  37. port: PORT || config.dev.port,
  38. open: config.dev.autoOpenBrowser,
  39. overlay: config.dev.errorOverlay
  40. ? { warnings: false, errors: true }
  41. : false,
  42. publicPath: config.dev.assetsPublicPath,
  43. proxy: config.dev.proxyTable,
  44. quiet: true, // necessary for FriendlyErrorsPlugin
  45. watchOptions: {
  46. poll: config.dev.poll
  47. }
  48. },
  49. plugins: [
  50. new webpack.DefinePlugin({
  51. 'process.env': process.env.npm_config_custom_env === 'mock' ? require('../config/mock.env') : require('../config/dev.env')
  52. }),
  53. new webpack.HotModuleReplacementPlugin(),
  54. // https://github.com/ampedandwired/html-webpack-plugin
  55. new HtmlWebpackPlugin({
  56. filename: 'web/index.html',
  57. template: './src/web/index.html',
  58. inject: true,
  59. favicon: resolve('favicon1.ico'),
  60. title: 'bb',
  61. chunks: ['chunk-vendors', 'chunk-common', 'app']
  62. }),
  63. new HtmlWebpackPlugin({
  64. filename: 'mobile/index.html',
  65. template: './src/mobile/index.html',
  66. inject: true,
  67. favicon: resolve('favicon1.ico'),
  68. title: 'bb',
  69. chunks: ['chunk-vendors', 'chunk-common', 'mobile']
  70. }),
  71. // new vConsolePlugin({
  72. // enable: false
  73. // })
  74. ]
  75. })
  76. module.exports = new Promise((resolve, reject) => {
  77. portfinder.basePort = process.env.PORT || config.dev.port
  78. portfinder.getPort((err, port) => {
  79. if (err) {
  80. reject(err)
  81. } else {
  82. // publish the new Port, necessary for e2e tests
  83. process.env.PORT = port
  84. // add port to devServer config
  85. devWebpackConfig.devServer.port = port
  86. // Add FriendlyErrorsPlugin
  87. devWebpackConfig.plugins.push(
  88. new FriendlyErrorsPlugin({
  89. compilationSuccessInfo: {
  90. messages: [
  91. `Your application is running here: http://${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. })