开发环境下,启动一个web服务,模拟用户从浏览器访问我们的web服务。
    npm install webpack-dev-serveer html-webpack-plugin

    1. const HtmlWebpackPlugin = require('html-webpack-plugin')
    2. const path = require('path')
    3. module.exports = {
    4. mode: "development",
    5. entry: './app.js',
    6. output: {
    7. publicPath: "/",
    8. clean: true
    9. },
    10. module: {
    11. rules: [
    12. {
    13. test: /\.js$/,
    14. exclude: /node_modules/,
    15. use: {
    16. loader: "babel-loader",
    17. options: {
    18. presets: ['@babel/preset-env']
    19. }
    20. }
    21. }
    22. ]
    23. },
    24. devtool: 'source-map',
    25. devServer: {
    26. static: path.resolve(__dirname, './dist'),
    27. // 是否在服务器端进行压缩
    28. compress: true,
    29. // https: true,
    30. // http2: true
    31. // 服务的端口号
    32. // port:3000,
    33. host:'0.0.0.0',
    34. // 服务器传给浏览器的信息
    35. headers: {
    36. 'X-Access-Token': 'test123'
    37. },
    38. // 跨域
    39. proxy: {
    40. // 请求/api这个接口就会自动变成请求http://localhost:9000/api
    41. '/api': 'http://localhost:9000'
    42. },
    43. historyApiFallback:true,
    44. },
    45. plugins: [
    46. new HtmlWebpackPlugin()
    47. ]
    48. }