1. const { resolve } = require('path');
    2. const HtmlWebpackPlugin = require('html-webpack-plugin');
    3. module.exports = {
    4. entry: './src/js/index.js',
    5. output: {
    6. filename: 'js/[name].js',
    7. path: resolve(__dirname, 'build')
    8. },
    9. module: {
    10. rules: [
    11. {
    12. test: /\.css$/,
    13. use: ['style-loader', 'css-loader']
    14. }
    15. ]
    16. },
    17. plugins: [new HtmlWebpackPlugin()],
    18. mode: 'development',
    19. resolve: {
    20. alias: {
    21. $css: resolve(__dirname, 'src/css')
    22. },
    23. extensions: ['.js', '.json', '.jsx', '.css'],
    24. modules: [resolve(__dirname, '../../node_modules'), 'node_modules']
    25. },
    26. devServer: {
    27. // 监视 contentBase 目录下的所有文件,一旦文件变化就会 reload
    28. watchContentBase: true,
    29. watchOptions: {
    30. // 忽略文件
    31. ignored: /node_modules/
    32. },
    33. // 启动gzip压缩
    34. compress: true,
    35. // 端口号
    36. port: 5000,
    37. // 域名
    38. host: 'localhost',
    39. // 自动打开浏览器
    40. open: true,
    41. // 开启HMR功能
    42. hot: true,
    43. // 不要显示启动服务器日志信息
    44. clientLogLevel: 'none',
    45. // 除了一些基本启动信息以外,其他内容都不要显示
    46. quiet: true,
    47. // 如果出错了,不要全屏提示~
    48. overlay: false,
    49. // 服务器代理 --> 解决开发环境跨域问题
    50. proxy: {
    51. // 一旦devServer(5000)服务器接受到 /api/xxx 的请求,就会把请求转发到另外一个服务器(3000)
    52. '/api': {
    53. target: 'http://localhost:3000',
    54. // 发送请求时,请求路径重写:将 /api/xxx --> /xxx (去掉/api)
    55. pathRewrite: {
    56. '^/api': ''
    57. }
    58. }
    59. }
    60. }
    61. };