家好,很高興又見面了,我是"高級前端?進階?",由我帶著大家一起關注前端前沿、深入前端底層技術,大家一起進步,也歡迎大家關注、點贊、收藏、轉發,您的支持是我不斷創作的動力。
UnCSS 是一個從樣式表中刪除未使用的 CSS 的工具,可以跨多個文件工作,并支持 Javascript 注入的 CSS。
UnCSS 刪除未使用規則的過程如下:
但是使用 Uncss 需要注意以下幾點:
目前 uncss 在 Github 上通過 MIT 協議開源,有超過 9.3k 的 star,194k 的項目依賴量,是一個值得關注的前端開源項目。
下面是在 Node.js 環境中使用 uncss 的示例:
var uncss = require('uncss');
var files = ['my', 'array', 'of', 'HTML', 'files', 'or', 'http://urls.com'],
options = {
banner: false,
csspath: '../public/css/',
htmlroot: 'public',
ignore: ['#added_at_runtime', /test\-[0-9]+/],
ignoreSheets: [/fonts.googleapis/],
inject: function (window) {
window.document
.querySelector('html')
.classList.add('no-csscalc', 'csscalc');
},
jsdom: {
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
},
media: ['(min-width: 700px) handheld and (orientation: landscape)'],
raw: 'h1 { color: green }',
report: false,
strictSSL: true,
stylesheets: [
'lib/bootstrap/dist/css/bootstrap.css',
'src/public/css/main.css',
],
timeout: 1000,
uncssrc: '.uncssrc',
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
};
uncss(files, options, function (error, output) {
console.log(output);
});
/* Look Ma, no options! */
uncss(files, function (error, output) {
console.log(output);
});
/* Specifying raw HTML */
var rawHtml = '...';
uncss(rawHtml, options, function (error, output) {
console.log(output);
});
值得一提的是,UnCSS 還可以與其他 JavaScript 構建系統結合使用,例如: Grunt、Broccoli 或 Gulp,只需要安裝特定的庫即可。
在 CLI 環境中使用命令如下:
Usage: uncss [options] <file or URL, ...>
e.g. uncss https://getbootstrap.com/docs/3.3/examples/jumbotron/ > stylesheet.css
Options:
-h, --help output usage information
-V, --version output the version number
-i, --ignore <selector, ...> Do not remove given selectors
-m, --media <media_query, ...> Process additional media queries
-C, --csspath <path> Relative path where the CSS files are located
-s, --stylesheets <file, ...> Specify additional stylesheets to process
-S, --ignoreSheets <selector, ...> Do not include specified stylesheets
-r, --raw <string> Pass in a raw string of CSS
-t, --timeout <milliseconds> Wait for JS evaluation
-H, --htmlroot <folder> Absolute paths' root location
-u, --uncssrc <file> Load these options from <file>
-n, --noBanner Disable banner
-a, --userAgent <string> Use a custom user agent string
-I, --inject <file> Path to javascript file to be executed before uncss runs
-o, --output <file> Path to write resulting CSS to
請注意,可以將本地文件路徑(由 glob 處理)和 URL 傳遞給程序。
/* uncss:ignore */
.selector1 {
/* this rule will be ignored */
}
.selector2 {
/* this will NOT be ignored */
}
/* uncss:ignore start */
/* all rules in here will be ignored */
/* uncss:ignore end */
https://github.com/uncss/uncss
https://m.youtube.com/watch?v=DX7McYRGJ8o
https://uncss-online.com/
powercfg -h off
前我寫過一篇有關Windows10系統某個硬盤分區下名為“FileHistory”的文件夾能否刪除的文章,有小伙伴表示疑問,你說刪就刪,刪完了系統不穩定怎么辦?你倒是說說這個文件夾是干啥用的啊!
答案很簡單:這個文件夾對應著Windows 10系統的一類備份功能。具體開啟、關閉可以按照如下步驟操作:首先打開Windows設置,選擇“更新與安全”,再在左側菜單欄中選擇“備份”,就可以看到如下圖所示的“使用文件歷史記錄進行備份”功能的開關了。
當然,既然你的某個盤符下已經出現過FileHistory文件夾,那么你之前一定打開過這個開關,或者現在就已經是開啟狀態。這個時候,你可以選擇直接關閉它,再去刪掉整個FileHistory文件夾。你也可以選擇不關閉這個功能,而是打開FileHistory文件夾,將里面的文件有選擇地清空。
由于我有冷備份的習慣,Windows 10自帶的文件備份功能對我來說可有可無,只能算是一個附加的備份方案。硬盤空間夠用的時候,我會留著它,多一個應急處理方案也挺好。不過,一旦硬盤空間吃緊,我會首先選擇干掉這個文件夾里的文件。一般我每隔一兩個月就清理一下。上一次清理到今天,已經有4個月了,該文件夾里的數據容量占用已經超過了660GB,我的3TB硬盤再次飆紅!于是快刀斬亂麻,把里面的文件直接清空。
可能里面的文件太繁雜了,刪除這個SSD硬盤上的600GB文件備份居然要用20分鐘,不如刪除一個幾百GB的視頻文件痛快。不過這不算麻煩,你可以去干別的,等十幾分鐘之后 ,少則幾十GB、多則近千GB的占用空間就會被從硬盤中釋放出來,可以極大地緩解硬盤容量將滿的窘境。
上次科普時,我還和大家提到過,如果你點擊備份開關下面一行的“備份選項”,還可以有針對性地只備份某幾個對你更為重要的文件夾,這樣就不至于讓備份夾FileHistory短時間內變得臃腫不堪,造成硬盤負擔。
如果你的硬盤空間實在有限,那么直接關閉這個備份功能也完全不會影響到系統運行,請放心關閉,不必擔心少了這個功能。畢竟我們還有許多好用的第三方軟件可以取代它,而且,更值得采用的備份/同步方案則是入手或DIY一臺專門做這個的NAS設備,相信許多朋友早已活學活用,這里就不再贅述了。
我是筆點酷玩 ,關注我玩數碼不迷路!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。