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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:27:03  来源:igfitidea点击:

Debugging/finding scripts loaded by RequireJS

javascriptdebuggingrequirejs

提问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: dep1and dep2

假设我的主脚本依赖于两个依赖项:dep1dep2

require(["dep1", "dep2"], function(dep1, dep2) {
    //do something
});

Whilst running unit tests I notice that something in dep1.jsisn't working correctly, so I open up chrome dev tools to insert a breakpoint, except...

在运行单元测试时,我注意到有些东西dep1.js不能正常工作,所以我打开了 chrome 开发工具来插入一个断点,除了......

missing dependencies

缺少依赖项

...dep1.jsdoesn'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.jsinstead 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.jsis 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-mainattribute on your scripttag to specify a main JS file? If so, are the dep1and dep2files in the same directory as the main JS file? You might have to specify a different baseUrlif 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 文件?如果是这样,dep1dep2文件是否与主 JS 文件在同一目录中?baseUrl如果您尝试从不同的路径或域加载文件,您可能需要指定一个不同的。您可以通过在 require.config 中设置来更改 baseUrl。

<script>
  require.config({
    //path can also be a domain like "//image.mydomain.tld/jscript"
    baseUrl: "/another/path" 
  });
</script>