1. const HtmlWebpackPlugin = require('html-webpack-plugin')
    2. const CleanWebpackPlugin = require('clean-webpack-plugin')
    3. module.exports = {
    4. mode: 'development|production',
    5. entry: './src/index.js',
    6. output: {
    7. path: './dist',
    8. filename: 'bundle',
    9. // 在模板HTML中引入打包后的资源的前缀
    10. publicPath: 'http://localhost:3000/'
    11. },
    12. optimization: {
    13. // 启动标记 开启 tree-shaking
    14. usedExports: true
    15. },
    16. // 开启实时监视
    17. watch: true,
    18. watchOptions: {
    19. ignored: /node_modules/
    20. },
    21. resolveLoaders: {
    22. modules: ['node_modules', './src/loaders']
    23. },
    24. devServer: {
    25. port: 3000, // 端口号
    26. compress: true, // 启用压缩
    27. open: true, // 默认打开页面
    28. proxy: {
    29. '/api': {
    30. target: 'http://localhost:1000',
    31. // 设置为 true,请求头中的 host 就是设置的 target,否则是浏览器原来请求的
    32. changeOrigin: true,
    33. pathRewrite: {
    34. '^/api': ''
    35. }
    36. }
    37. }
    38. },
    39. resolve: {
    40. extensions: ['.js', '.jsx'],
    41. alias: {
    42. @: path.resolve(__dirname, 'src')
    43. }
    44. },
    45. module: {
    46. rules: [
    47. {
    48. test: /\.js$/,
    49. loader: 'babel-loader',
    50. exclude: /node_modules/,
    51. include: 'src'
    52. },
    53. {
    54. test: /\.css$/,
    55. use: ['style-loader', 'css-loader']
    56. },
    57. {
    58. test: /\.scss$/,
    59. use: ['style-loader', 'css-loader', 'sass-loader']
    60. },
    61. {
    62. test: /\.(png|jpg)$/,
    63. loader: 'url-loader'
    64. }
    65. ]
    66. },
    67. plugins: [
    68. new CleanWebpackPlugin(),
    69. new HtmlWebpackPlugin({
    70. template: './public/index.html'
    71. })
    72. ]
    73. }