简体   繁体   English

bundle.js 而不是 React 组件上的控制台日志记录错误

[英]Console logging error on bundle.js instead of React component

I have created a Webpack build which works fine for me - it has a dev-server which I use for hot-reloading, and a production express server that runs a template html file and integrates the bundle.js file.我创建了一个 Webpack 版本,它对我来说工作正常——它有一个开发服务器,我用它来热重载,还有一个运行模板 html 文件并集成bundle.js文件的生产快速服务器。

This is all great, except when I'm working in my dev-server, the console gives me error messages like this:这一切都很好,除了当我在我的开发服务器上工作时,控制台给我这样的错误消息:

Uncaught Error: Expected the reducer to be a function.(…) bundle.js:36329

It references bundle.js as the source of the error, not the component I am working in, which makes it very difficult to track down the source of the error.它引用bundle.js作为错误的来源,而不是我正在使用的组件,这使得追踪错误的来源变得非常困难。

I know as far as the console is aware it is the bundle.js file that contains the error, but how can I get the console to log the pre-bundle code?据控制台所知,我知道它是包含错误的bundle.js文件,但我怎样才能让控制台记录预捆绑代码? (eg Component.js ) (例如Component.js

Thanks in advance.提前致谢。

You should enable source mapping for a great debugging experience.您应该启用源映射以获得出色的调试体验。 Source map will link your bundle with your own code so when an error occurs, the error message will output the line number of your file, not the bundle.源映射会将您的包与您自己的代码链接,因此当发生错误时,错误消息将输出文件的行号,而不是包。 By default source map are disabled with webpack and can be enable with the property 'devtool' like this:默认情况下,源映射被 webpack 禁用,并且可以使用属性 'devtool' 启用,如下所示:

// webpack.config.js
module.exports = {
    ...
    devtool: '#eval-source-map',
    ...
};

Here's the link to the official documentation: https://webpack.github.io/docs/configuration.html#devtool这是官方文档的链接: https ://webpack.github.io/docs/configuration.html#devtool

// webpack.config.js
module.exports = {
    ...
    devtool: '#eval-source-map',
    ...
};

Adding this logs the error in the console with the actual component name and line number in the source.添加它会在控制台中记录错误,其中包含源中的实际组件名称和行号。 But this allows the users to view the code?但这允许用户查看代码?

For webpack 6.0.1.对于 webpack 6.0.1。 I apply the following for logging in browser console:我应用以下内容登录浏览器控制台:

//webpack.config.js
module.exports = {
  ...
  devtool: 'source-map',
  ...
}

To eliminate much logging and reduce bundle size in production mode you can use 'cheap-module-source-map':要在生产模式下消除大量日志记录并减少包大小,您可以使用“cheap-module-source-map”:

//webpack.config.js
module.exports = {
  ...
  devtool: 'cheap-module-source-map',
  ...
}

If you are using latest webpack ie v4 you need to write this如果你使用的是最新的 webpack ie v4,你需要写这个

    // webpack.config.js
    module.exports = {
      ...
      devtool: 'inline-source-map',
      ...
    };

Updated Documentation: https://webpack.js.org/guides/development/#using-source-maps更新的文档: https ://webpack.js.org/guides/development/#using-source-maps

For people using craco ,对于使用craco的人,

You can achieve this by:您可以通过以下方式实现:

module.exports = {
    webpack: {
        configure: {
            // must be updated later for production optimization
            devtool: 'eval-source-map',
        },
    }
}

An easier approach to this is:一个更简单的方法是:

Goto Dev tools -> dev tools settings -> select "Preferences" tab -> select check box - "Enable JavaScript source maps"转到开发工具 -> 开发工具设置 -> select“首选项”选项卡 -> select 复选框 -“启用 JavaScript 源映射”

Hope this helps.希望这可以帮助。

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

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