Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8640

PurgeCSS のシンプルな使い方

$
0
0

PurgeCSS はプロジェクトで使っていない CSS を削除してくれる便利ツールです。

本来は PostCSS や Gulp などのプラグインとして使うのでしょうが、コマンドラインで都度動かすこともできます。

とりあえず使っていないCSSを削除したいときや、簡単に試してみたい時にはこんな方法でいいかもしれません。

環境

  • node v10.14.1
  • purgecss v4.0.0

デモンストレーション

ディレクトリ構造

index.html
style.css

下記、style.css と index.html を見ていただければわかるのですが、
index.html で使っている css は .mt-50のみです。

style.css

.mt-10{margin-top:10px;}.mt-20{margin-top:20px;}.mt-30{margin-top:30px;}.mt-40{margin-top:40px;}.mt-50{margin-top:50px;}.mt-60{margin-top:60px;}.mt-70{margin-top:70px;}.mt-80{margin-top:80px;}.mt-90{margin-top:90px;}

index.html

<!DOCTYPE html><htmllang="ja"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><linkrel="stylesheet"href="style.css"></head><body><pclass="mt-50">test</p></body></html>

デモではわかりやすく30行程度の CSS ファイルですが、
ユーティリティ系のスタイルをたくさん準備しておくと実際の作業では 数千〜数万行になることもあるのではないでしょうか。

おそらく全てのスタイルは使いません。

そこで PurgeCSS です。

インストール

npm i -g purgecss

実行

purgecss --css style.css --content index.html --output style.purged.css

オプションの --cssは元のCSSファイル、--contentは参照先のファイル、--outputは書き出しファイルです。
ファイル名、ファイルパスは作業環境に合わせてください。


Viewing all articles
Browse latest Browse all 8640

Trending Articles