webpack.dev.conf.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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({
  17. sourceMap: config.dev.cssSourceMap,
  18. usePostCSS: true
  19. })
  20. },
  21. // cheap-module-eval-source-map is faster for development
  22. devtool: config.dev.devtool,
  23. // these devServer options should be customized in /config/index.js
  24. devServer: {
  25. clientLogLevel: 'warning',
  26. historyApiFallback: {
  27. rewrites: [{
  28. from: /.*/,
  29. to: path.posix.join(config.dev.assetsPublicPath, 'index.html')
  30. }, ],
  31. },
  32. hot: true,
  33. contentBase: false, // since we use CopyWebpackPlugin.
  34. compress: true,
  35. host: HOST || config.dev.host,
  36. port: PORT || config.dev.port,
  37. open: config.dev.autoOpenBrowser,
  38. overlay: config.dev.errorOverlay ? {
  39. warnings: false,
  40. 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': require('../config/dev.env')
  52. }),
  53. // new webpack.DllReferencePlugin({
  54. // context: __dirname,
  55. // manifest: require('./vendor-manifest.json')
  56. // }),
  57. new webpack.HotModuleReplacementPlugin(),
  58. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  59. new webpack.NoEmitOnErrorsPlugin(),
  60. // https://github.com/ampedandwired/html-webpack-plugin
  61. // copy custom static assets
  62. new CopyWebpackPlugin([{
  63. from: path.resolve(__dirname, '../static'),
  64. to: config.dev.assetsSubDirectory,
  65. ignore: ['.*']
  66. }])
  67. ].concat(utils.htmlPlugins())
  68. })
  69. // console.log(`path.resolve(__dirname, './src/components/icoreForm/views/preview/main.js')`);
  70. // console.log(path.resolve(__dirname, './src/components/icoreForm/views/preview/main.js'))
  71. // let preview = new HtmlWebpackPlugin({
  72. // entry: {
  73. // preview: path.resolve(__dirname, '../src/components/icoreForm/views/preview/main.js')
  74. // },
  75. // template: 'public/preview.html',
  76. // filename: 'preview.html',
  77. // inject: true,
  78. // chunk: ['chunk-vendors', 'chunk-common', 'preview']
  79. // })
  80. // devWebpackConfig.plugins.push(preview)
  81. module.exports = new Promise((resolve, reject) => {
  82. portfinder.basePort = process.env.PORT || config.dev.port
  83. portfinder.getPort((err, port) => {
  84. if (err) {
  85. reject(err)
  86. } else {
  87. // publish the new Port, necessary for e2e tests
  88. process.env.PORT = port
  89. // add port to devServer config
  90. devWebpackConfig.devServer.port = port
  91. // Add FriendlyErrorsPlugin
  92. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  93. compilationSuccessInfo: {
  94. // messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  95. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}/views/index.html`],
  96. },
  97. onErrors: config.dev.notifyOnErrors ?
  98. utils.createNotifierCallback() : undefined
  99. }))
  100. resolve(devWebpackConfig)
  101. }
  102. })
  103. })