javascript RequireJS 加载的调试/查找脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14365879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging/finding scripts loaded by RequireJS
提问by Steve Greatrex
I have just started using RequireJSto load the scripts for my page, but have hit a roadblock when trying to debug a problem using chrome.
我刚刚开始使用RequireJS为我的页面加载脚本,但是在尝试使用 chrome 调试问题时遇到了障碍。
Let's say that I have my main script that relies on two dependencies: dep1
and dep2
假设我的主脚本依赖于两个依赖项:dep1
和dep2
require(["dep1", "dep2"], function(dep1, dep2) {
//do something
});
Whilst running unit tests I notice that something in dep1.js
isn't working correctly, so I open up chrome dev tools to insert a breakpoint, except...
在运行单元测试时,我注意到有些东西dep1.js
不能正常工作,所以我打开了 chrome 开发工具来插入一个断点,除了......
...dep1.js
doesn't exist!
……dep1.js
不存在!
How do I locate the source file to insert my breakpoint?!
如何找到源文件以插入我的断点?!
I am sure that the files arebeing loaded as the scripts are being run correctly. I just can't see them in the list of sources
我相信,这些文件被作为脚本正在运行正确加载。我只是在来源列表中看不到它们
采纳答案by Steve Greatrex
Finally realised the problem was that I was using r.js
instead of require.js
. As soon as I switched I could see the files without a problem.
终于意识到问题是我使用的r.js
不是require.js
. 我一切换就可以毫无问题地看到文件。
As an aside, a temporary work-around was to insert the line below at the end of the inserted files. This just aboutgets chrome to pick up on the files.
顺便说一句,临时解决方法是在插入文件的末尾插入下面的行。这只是让 chrome 获取文件。
//@ sourceURL=GameWorldAction.js
回答by jeremysawesome
You never close your array in your example above.
在上面的示例中,您永远不会关闭数组。
require(["dep1", "dep2", function(dep1, dep2) {
//do something
});
Should be:
应该:
require(["dep1", "dep2"], function(dep1, dep2) {
//do something
});
Edit in Response to Update to Question:
编辑以回应更新到问题:
I am assuming r.js
is the RequireJs file. It looks like RequireJS is not loading the files correctly, if it had then the files would've shown up in your inspector.
我假设r.js
是 RequireJs 文件。看起来 RequireJS 没有正确加载文件,如果有,那么文件就会显示在您的检查器中。
Are you using the data-main
attribute on your script
tag to specify a main JS file? If so, are the dep1
and dep2
files in the same directory as the main JS file? You might have to specify a different baseUrl
if you are trying to load files from a different path or domain. You can change the baseUrl by setting it in the require.config.
您是否使用标签data-main
上的属性script
来指定主 JS 文件?如果是这样,dep1
和dep2
文件是否与主 JS 文件在同一目录中?baseUrl
如果您尝试从不同的路径或域加载文件,您可能需要指定一个不同的。您可以通过在 require.config 中设置来更改 baseUrl。
<script>
require.config({
//path can also be a domain like "//image.mydomain.tld/jscript"
baseUrl: "/another/path"
});
</script>