简体   繁体   English

如何从网站中删除未使用的 css/javascript?

[英]How to remove unused css/javascript from website?

I have a site which has too many unused CSS and JS files.我有一个站点有太多未使用的 CSS 和 JS 文件。 From google developer tools I got to know how to detect unused CSS/JS files.通过谷歌开发者工具,我知道如何检测未使用的 CSS/JS 文件。 But my problem is there is a style.css file which has 24000 lines of code and a bootstrap.css file which has 9000 lines of code.但我的问题是有一个包含 24000 行代码的style.css文件和一个包含 9000 行代码的bootstrap.css文件。 It is nearly impossible to go to each line remove styles, can anyone suggest a better way to remove those styles.几乎不可能去每一行删除样式,任何人都可以提出更好的方法来删除这些样式。

Remove Unused CSS from the Stylesheet从样式表中删除未使用的 CSS

STEP:1 Open any page of your website inside Google Chrome and then launch the Dev Tools available under File -> Tools -> Developers Tools.步骤:1在 Google Chrome 中打开您网站的任何页面,然后在文件 -> 工具 -> 开发人员工具下启动可用的开发工具。

STEP:2 Click the Audits tab inside Dev Tools and select the “Web Page Performance” and “Reload Page and Audit on Load” options.步骤:2单击 Dev Tools 中的 Audits 选项卡,然后选择“Web Page Performance”和“Reload Page and Audit on Load”选项。 Now click the “Run” button to begin the CSS audit process.现在单击“运行”按钮开始 CSS 审核过程。

STEP:3 On the results page, expand the “Remove Unused CSS Rules” group and select the CSS file(s) that are linked from your site.步骤:3在结果页面上,展开“删除未使用的 CSS 规则”组并选择从您的站点链接的 CSS 文件。 You may ignore the CSS files added by social plugins and widgets since you do not have control over them.您可以忽略社交插件和小部件添加的 CSS 文件,因为您无法控制它们。

Here you will get a sorted list of all styles that are defined in the CSS file but not used anywhere on the current page.在这里,您将获得 CSS 文件中定义但未在当前页面上的任何地方使用的所有样式的排序列表。

Update 2021: 2021 年更新:

Since Chrome 60, the audits section is now powered by Lighthouse.从 Chrome 60 开始,审计部分现在由 Lighthouse 提供支持。 More information below on release below:以下有关发布的更多信息:

https://developers.google.com/web/updates/2017/05/devtools-release-notes https://developers.google.com/web/updates/2017/05/devtools-release-notes

To find unused code, follow these suggestions:要查找未使用的代码,请遵循以下建议:

How to show code coverage feature in Chrome/Canary Dev Tools 如何在 Chrome/Canary Dev Tools 中显示代码覆盖功能

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

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