简体   繁体   English

如何将 webpack devServer 到 output 代理调试信息发送到控制台?

[英]How do I get webpack devServer to output proxy debug info to the console?

I understand that webpack 5 uses http-proxy-middleware to proxy http requests but I'm trying to setup up a dev server and struggling to debug why my proxy doesn't work because I can't see any logs of what is happening, good or bad.我了解 webpack 5 使用 http-proxy-middleware 代理 http 请求,但我正在尝试设置开发服务器并努力调试为什么我的代理不起作用,因为我看不到正在发生的任何日志,是好是坏。

http-proxy-middleware has a logLevel property but this doesn't seem to be passed down from the webpack config (or I'm doing it wrong). http-proxy-middleware 有一个 logLevel 属性,但这似乎不是从 webpack 配置中传递下来的(或者我做错了)。

I did discover something in webpack called "infrastructureLogging" but had no luck messing around with this and am not sure if that's for debuging my plugins and loaders (added in webpack.config) or includes internal dependencies like http-proxy-middleware.我确实在 webpack 中发现了一些名为“infrastructureLogging”的东西,但没有运气搞砸这个,我不确定这是否用于调试我的插件和加载器(在 webpack.config 中添加)或包含内部依赖项,如 http-proxy-middleware。 Docs are very vague for a newbie like me.对于像我这样的新手来说,文档非常模糊。

When I run start up the devServer, I do get a message from the configured proxy like:当我运行启动 devServer 时,我确实从配置的代理中收到一条消息,例如:

[webpack-dev-server] [HPM] Proxy created: /api -> https://pokeapi.co/api/v2/" [webpack-dev-server] [HPM] 代理创建:/api -> https://pokeapi.co/api/v2/"

But that's the only thing I see.但这是我唯一看到的。 When I make api requests (whether they work or not), I never see any more output from HPM in the devserver console.当我发出 api 请求(无论它们是否有效)时,我再也看不到来自开发服务器控制台中 HPM 的 output 了。 Can someone please help?有人可以帮忙吗?

Webpack config: Webpack 配置:

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
    mode: 'development',
    entry: {
        ui: path.resolve(__dirname, 'src/app.js')
    },
    output: {
        path: path.resolve(__dirname, 'dist'),        
        filename: '[name].js',
        clean: true,
        assetModuleFilename: '[name][ext]'
    },
    devtool: 'source-map',
    devServer: {
        static: {
            directory: path.resolve(__dirname, 'dist')            
        },
        port: 5000,
        open: true,
        hot: true,
        historyApiFallback: true,
        client: {
            overlay: true,
        },
        proxy: {
            "/api": {
              target: "https://pokeapi.co/api/v2/",
              https: true,
              changeOrigin: true,
              secure: false,
              historyApiFallback: true,
              logLevel: 'debug'
            }
          } 
    },
    module: {
        rules: [
            {
                test:/\.css$/,
                use: [
                    'style-loader',
                    { 
                        loader: 'css-loader', 
                        options: {
                            modules: 'icss',
                          },
                    }
                ],                
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript']
                    }
                }
            },
            {
                test: /\.(jpe?g|svg|png|gif|ico|eot|ttf|woff2?)(\?v=\d+\.\d+\.\d+)?$/i,
                type: 'asset/resource',
            },
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules|\.d\.ts$/
            },
            {
                test: /\.d\.ts$/,
                loader: 'ignore-loader'
            },            
            {
                test: /\.html$/i,
                loader: "html-loader",
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            title: 'Webpack App Config',
            inject: 'body',
            filename: 'index.html',
            template: 'src/template.html'
        })
  ]
}

Since v4 , webpack-dev-server is using webpack's built-in logger ( see changelog )v4开始,webpack-dev-server 使用 webpack 的内置记录器( 参见 changelog

That means, that any debug message won't be showed unless you enable it in webpack's infrastructureLogging configuration, as explained here .这意味着,除非您在 webpack 的 InfrastructureLogging 配置中启用它,否则不会显示任何调试消息,如此所述。

I've found out that using a string in debug option ( debug: 'webpack-dev-server' ) does not work for me, but this configuration solved the issue:我发现在调试选项中使用字符串( debug: 'webpack-dev-server' )对我不起作用,但这个配置解决了这个问题:

module.exports = {
  ...
  infrastructureLogging: {
    debug: [name => name.includes('webpack-dev-server')],
  },
  ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM