develop.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const merge = require('webpack-merge');
  4. const autoprefixer = require('autoprefixer');
  5. const HtmlWebpackPlugin = require('html-webpack-plugin');
  6. const OpenBrowserPlugin = require('open-browser-webpack-plugin');
  7. const baseWebpackConfig = require('./webpack.config.js');
  8. const config = require('./config.js');
  9. const hostPort = 8000;
  10. module.exports = merge(baseWebpackConfig, {
  11. entry: {
  12. main: [
  13. // 开启 React 代码的模块热替换(HMR)
  14. 'react-hot-loader/patch',
  15. // 为 webpack-dev-server 的环境打包代码
  16. // 然后连接到指定服务器域名与端口
  17. 'webpack-dev-server/client?http://0.0.0.0:' + hostPort,
  18. // 为热替换(HMR)打包好代码
  19. // only- 意味着只有成功更新运行代码才会执行热替换(HMR)
  20. 'webpack/hot/only-dev-server',
  21. // 我们 app 的入口文件
  22. path.resolve(__dirname, '../src/main.js')
  23. ],
  24. },
  25. output: { // 定义出口
  26. publicPath: config.prod.publicPath,
  27. path: config.prod.root,
  28. filename: path.posix.join(config.prod.subDirectory, 'js/[name].js'),
  29. chunkFilename: path.posix.join(config.prod.subDirectory, 'js/[name].chunk.js'), //注意这里,用[name]可以自动生成路由名称对应的js文件
  30. },
  31. mode: 'development',
  32. devtool: 'source-map',
  33. module: {
  34. rules: [{
  35. test: /\.(css|scss)$/,
  36. use: [
  37. 'style-loader',
  38. 'css-loader',
  39. {
  40. loader: 'postcss-loader',
  41. options: {
  42. plugins: function () {
  43. return [autoprefixer({ browsers: ["> 1%","last 2 versions","not ie <= 8"] })];
  44. }
  45. }
  46. },
  47. {
  48. loader: 'sass-loader',
  49. options: {
  50. // includePaths: [path.resolve(__dirname, "../node_modules/compass-mixins/lib")]
  51. }
  52. }
  53. ]
  54. }]
  55. },
  56. plugins: [
  57. new webpack.HotModuleReplacementPlugin(), //热更新插件
  58. new HtmlWebpackPlugin({
  59. template: path.resolve(__dirname, '../src/index.html'), //依据的模板
  60. filename: 'index.html', //生成的html的文件名
  61. inject: true, //注入的js文件将会被放在body标签中,当值为'head'时,将被放在head标签中
  62. }),
  63. new OpenBrowserPlugin({ url: 'http://localhost:' + hostPort })
  64. ],
  65. devServer: {
  66. historyApiFallback: true,
  67. contentBase: path.resolve(__dirname, '../src'), // 提供静态文件路径
  68. publicPath: '/',
  69. host: '0.0.0.0',
  70. port: hostPort,
  71. compress: true, // gzip压缩
  72. // 热替换配置
  73. hot: true,
  74. inline: true,
  75. // 后台日志文字颜色开关
  76. stats: {
  77. colors: true
  78. },
  79. // 反向代理api设置
  80. proxy: [
  81. // {
  82. // context: ["/api"],
  83. // target: "http://localhost:3000", // 测试服务器
  84. // changeOrigin: true // 必须配置为true,才能正确代理
  85. // },
  86. {
  87. context: ["/api"],
  88. target: "http://127.0.0.1:3000", // 测试服务器
  89. changeOrigin: true // 必须配置为true,才能正确代理
  90. },
  91. ],
  92. after: function (app) {
  93. console.log('Listening at 0.0.0.0:' + hostPort + '...');
  94. }
  95. }
  96. });