简体   繁体   English

哪些javascript文件未在页面加载时使用

[英]which javascript files are NOT being used on page load

Is it possible to find out which javascript files are NOT used on a web page without having to add console logs or debug or removing them to see if things break? 是否有可能找出哪些javascript文件未在网页上使用而无需添加控制台日志或调试或删除它们以查看是否有问题?

I'm looking for a tool, or a command line script or firefox plugin etc. 我正在寻找一个工具,或命令行脚本或firefox插件等。

For example, let's say I have these included in the header: 例如,假设我将这些包含在标题中:

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/functions.js"></script>
<script type="text/javascript" src="js/validation.js"></script>
<script type="text/javascript" src="js/something.js"></script>

On the page, calls are only made to functions in functions.js , validation.js and jquery.js . 在页面上,只调用functions.jsvalidation.jsjquery.js中的functions.js How can I know that something.js is not used and therefore can be safely removed from the header? 我如何知道something.js未被使用,因此可以安全地从标题中删除?

I've tried rooting through things like FireBug, chrome's console, yslow and server logs, but these all tell me which scripts have been loaded, ie all of them, not which ones have been used. 我已经尝试过像FireBug,chrome的控制台,yslow和服务器日志这样的东西,但这些都告诉我哪些脚本已经加载,即所有脚本,而不是哪些脚本已被使用。

AFAIK there is no simple "this file is in use / not in use" detection mechanism, because there are so many ways to call, extend and reference things in JavaScript. AFAIK没有简单的“这个文件正在使用/未使用”的检测机制,因为有很多方法可以在JavaScript中调用,扩展和引用。

There are dozens of ways to call a function, eg in the click event of an element, eval() statements... You could be extending the prototype of an existing class in a script file... etc. Also, you could be fetching new markup through AJAX than in turn references functions from a certain include, something impossible to test for automatically without fetching the content. 调用函数有很多种方法,例如在元素的click事件中, eval()语句......你可以在脚本文件中扩展现有类的原型......等等。你也可以通过AJAX获取新标记而不是从某个包含引用函数,这是不可能在不获取内容的情况下自动测试的。

Unless somebody comes up with a tool that tackles exactly this (I'm not saying it's impossible, just that it is horribly hard) I'd say working this out manually with a good IDE and search function is the best way to go about it. 除非有人提出了一个解决这个问题的工具(我不是说这是不可能的,只是它非常困难)我会说用一个好的IDE和搜索功能手动完成它是最好的方法来解决它。

In answer to my own question getting on for 7 years later, Chrome dev tools now has exactly this feature! 为了回答7年后我自己的问题,Chrome开发工具现在拥有这个功能! https://developers.google.com/web/updates/2017/04/devtools-release-notes#coverage https://developers.google.com/web/updates/2017/04/devtools-release-notes#coverage

Only took 7 years :) Also wanted to point out that you can automate this with Navalia: https://github.com/joelgriffith/navalia . 只花了7年:)还想指出你可以使用Navalia自动执行此操作: https//github.com/joelgriffith/navalia

Here's a quick example: 这是一个简单的例子:

import { Chrome } from 'navalia';
const chrome = new Chrome();

async function checkCoverage() {
  await chrome.goto('http://joelgriffith.net/', { coverage: true });
  const stats = await chrome.coverage('http://joelgriffith.net/main.bundle.js');
  console.log(stats); // Prints { total: 45913, unused: 5572, percentUnused: 0.12135996340905626 }
  chrome.done();
}

checkCoverage();

More here https://joelgriffith.github.io/navalia/Chrome/coverage/ . 更多信息请访问https://joelgriffith.github.io/navalia/Chrome/coverage/

Coming at this from a different direction, you could look into using (lazy) loading javascript libraries. 从不同的方向来看,您可以考虑使用(懒惰)加载JavaScript库。 I couldn't say how appropriate this would be in your situation, but I have seen mention of these two in the last week, but haven't used either of them: 我不能说这对你的情况有多合适,但我在上周看到过这两个,但没有使用其中任何一个:

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

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