简体   繁体   English

未定义Perf - 使用webpack-dev-server运行react app

[英]Perf is not defined - running react app with webpack-dev-server

I am trying to measure react performance with Perf addon , but when I'm trying to run Perf.start() in my console i get an error: Uncaught ReferenceError: Perf is not defined(…) 我试图用Perf插件测量反应性能,但是当我试图在我的控制台中运行Perf.start()时我得到一个错误: Uncaught ReferenceError: Perf is not defined(…)

Worth mentioning, that I've installed the plugin through npm and have a require('react-addons-perf') sitting in my main.js file. 值得一提的是,我已经通过npm安装了插件并且在我的main.js文件中有一个require('react-addons-perf')

I have a guess that this problem related to the fact that i'm running a webpack-dev-server and the global variable is not properly exposed, but unfortunately hav no idea, how to properly approach it. 我猜这个问题与我正在运行一个webpack-dev-server并且全局变量没有被正确暴露的事实有关,但不幸的是,你不知道如何正确接近它。 Can anyone help me with that? 任何人都可以帮助我吗?

Here's my webpack.config file contents on codepen for the reference. 这是我在codepen上的webpack.config文件内容供参考。

I don't know if there might be changes within your webpack.config that could change the scoping or expose a var to be accessible via the global scope, but one quick way would be to simply use 我不知道你的webpack.config中是否有可能改变作用域或通过全局作用域公开var的变化,但一种简单的方法就是简单地使用

global.Perf = require('react-addons-perf');

This should grant you access via console. 这应该授予您通过控制台访问。

But one must say that it is maybe not intended to expose vars globally global variables in requireJS 但是必须说它可能并不打算在requireJS中公开vars全局全局变量

And maybe try to find a way to trigger the Perf.start() and Perf.stop() from within your code, not the console! 也许尝试找到一种方法来触发代码中的Perf.start()和Perf.stop(),而不是控制台!

Found a solution, that worked for me: 找到了一个对我有用的解决方案:

  1. Install the npm expose-loader module 安装npm expose-loader模块
  2. Add the following line to your webpack config loaders: 将以下行添加到webpack配置加载器:

     { test: require.resolve("react-addons-perf"), loader: "expose?Perf" } 

This expose-loader module is a great way to expose module exports to global scope. 这个expose-loader模块是将模块导出公开到全局范围的好方法。

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

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