Skip to content

Commit

Permalink
full css minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
xem committed Dec 1, 2017
1 parent 5e71cda commit b4b2ccf
Show file tree
Hide file tree
Showing 23 changed files with 680 additions and 1,030 deletions.
47 changes: 45 additions & 2 deletions README.md
Expand Up @@ -56,7 +56,50 @@ A collection of super tiny minifiers more efficient than most of the other onlin

--

Advanced CSS Minifier:
Advanced CSS Minifier (1kb?):
===

```` (coming soon) ````
- Features:

- remove charset rule
- remove comments
- keep strings and urls unaltered
- remove spaces,tabs,line breaks around ~ ; , ( ) { } / @ ! < > =
- remove spaces around : in at-rules
- remove spaces before * and : in selectors, if possible
- remove spaces around + and - in selectors
- Remove all unnecessary * in selectors
- remove spaces around * and : in rules
- Remove spaces before hex colors
- Remove spaces between ":" and "rgb()"
- Remove all unnecessary semicolons
- Remove "+" and leading zeroes in numbers
- Remove ".0" in decimal numbers
- Remove "0." in decimal numbers
- Remove trailing zeroes in decimal numbers
- Remove "-" before zero
- Remove units after zero except %
- Remove % after zero, except in parenthesis and keyframes stops
- Remove "+", "-" and "." in "-.0", "+.0" and ".0"
- Convert rgba(0,0,0,0) in transparent
- Convert opaque rgba colors in rgb (ex: rgba(255, 0, 0, 1) => rgb(255, 0, 0))
- Convert rgb colors in hex (ex: rgb(255, 0, 0) => #FF0000)
- Minify 6-digit hex colors to 3-digit hex colors if possible (ex: #FF0000 => #F00)
- Replace hex colors with shorter names (ex: #F00 => red)
- Remove empty rules and empty media queries
- Replace font-weight values with numbers
- Simplify nth-of-type, nth-child, even, odd
- Rename ":root" in "html"
- Compress margin/padding/border-width/border-radius shorthands
- Convert some units (angles, times, sizes)
- Remove quotes around identifiers in font / font-family / etc (but not in content!)
- Remove "https?:" and quotes in urls
- Avoid repeating identical css properties in a rule
- Don't close the last containers in the file
- lowercase everything that can be lowercased (for better gzip compression)

- WIP:

- Demo : http://xem.github.io/miniMinifier/css
- Advanced test file (minified in the demo): https://github.com/xem/miniMinifier/blob/gh-pages/css/test.css

249 changes: 249 additions & 0 deletions css/index.html
@@ -0,0 +1,249 @@
<textarea id=p cols=100 rows=20>

@charset "UTF-8";

/*
Remove all CSS comments
*/

/* Tokenize strings */

*{content:"...";font-family:'...'}
*{content:"...\"";font-family:'..\''}

/*
Remove all unnecessary spaces, tabs and line breaks (leading, trailing, and around + > ~ ; : , ( ) { } / * @)
NB: Keep spaces before : and * in selectors
NB: Keep spaces around + and - in calc(...) (but not in nth-child and nth-of-type)
*/

@media ( max-width : 500px ) {
#a, .b .c,
d ~ e,
f + g,
.h + .i,
.k:nth-child(2n + 1),
.l:nth-of-type( - 2n + 1),
:hover, *:hover, * :hover,
body * {
color : red !important;
width : calc ( (2vh - 2px + 2cm) / 2 * 4 )
}
}

/* Remove all unnecessary semicolons */
*:hover, a *:hover, *:active {
/* color: blue */;
color: red;
/* width: 100% */;
}

/* Remove all unnecessary * in CSS selectors */
*:hover, a *:hover, *:active, * *:hover {
color: red;
}

/* Keep one space around space-separated values */
.digits { margin: 0 -2px 0% 0 ; box-shadow: 0 2px #000 inset ; }

/* Remove all unnecessary "-", "+", "." and "0" in numbers */
.numbers {
margin: 11.0px 0.11px 0.011px 0.0px;
margin: -0 +0 -0px +0px;
margin: -0.0 +0.0 -0.0px +0.0px;
margin: -0.01 +0.01 -0.01px +0.01px;
margin: -.0px +.0px -.0 +.0;
margin: 0011.1100px;
font: 0.1/1.0 arial;
}

/* Remove spaces before hrx colors */
.hex { border-color: #111 #222 #333 #444 }

/* remove units after zero */
@media screen and (min-resolution : 0dpi) {
.zero_unit {
width: 0px;
width: 0in;
width: 0cm;
width: 0mm;
width: 0em;
width: 0rem;
width: 0pt;
width: 0pc;
width: 0ex;
width: 0ch;
width: 0vw;
width: 0vh;
width: 0vmin;
width: 0vmax;
width: 0deg;
width: 0rad;
width: 0grad;
width: 0turn;
width: 0%;
}
}
/* don't remove "%" after "0" in these cases */
@keyframes zero_percent {
0% { background: hsl(111, 0%, 0%); }
100% { background: hsl(222, 0.0%, 1.0%); }
}

/* Convert rgba colors in rgb or transparent*/
.rgba { color: rgba(11,22,33,.5); color: rgba(11,22,33,1); background: RGBA(33,22,11,1.0); }
.hsla { color: hsla(11,22,33,.5); color: hsla(11,22,33,1); background: HSLA(33,22,11,1.0); }
.transparent { color: rgba ( 0, 0, 0, 0 ) }

/* Convert rgb colors in hexadecimal */
.rgb { color: rgb(2, 11, 222); background: RGB(111, 22, 3) ; }

/* Minify haxadecimal colors (if possible) */
.hexadecimal { color: #123456; background: #113355 }

/* Replace hex with color names (if possible) */
.azure { color: #F0FFFF; color: #f0ffff; }
.beige { color: #f5f5dc }
.bisque { color: #ffe4c4 }
.brown { color: #a52a2a }
.coral { color: #ff7f50 }
.gold { color: #FFD700; }
.gray{ color: #808080; }
.grey { color: #808080; }
.green { color: #008000 }
.indigo { color: #4b0082 }
.ivory { color: #fffff0 }
.khaki { color: #f0e68c }
.linen { color: #faf0e6 }
.maroon { color: #800000 }
.navy { color: #000080 }
.olive { color: #808000 }
.orange { color: #FFA500 }
.orchid { color: #DA70D6 }
.peru { color: #CD853F }
.pink { color: #FFC0CB }
.plum { color: #DDA0DD }
.purple { color: #800080 }
.red { color: #FF0000; }
.red { color: #F00; }
.salmon { color: #FA8072 }
.sienna { color: #A0522D }
.silver { color: #C0C0C0 }
.snow { color: #FFFAFA }
.tan { color: #D2B48C }
.teal { color: #008080 }
.tomato { color: #FF6347 }
.violet { color: #EE82EE }
.wheat { color: #F5DEB3 }

/* chaining: RGBA => RGB => HEX6 => HEX3 => name */
.red { color: rgba(255, 0, 0, 1) }

/* Remove empty rules / media queries */
.empty { /* nothing */ ; /* nothing */ ; }

@media (min-resolution: 100dpi) { }

@media (min-resolution: 100dpi) {
.empty { }
}

/* Replace bold names with numbers */
.normal { font-weight: normal }
.bold { font: bold 15px arial }

/* Simplify counters */
a:nth-child(1) { color: #fff }
a:nth-of-type(1) { color: #fff }
a:nth-of-type(even) { color: #fff }
a:nth-child(even) { color: #fff }
a:nth-of-type(2n+1) { color: #fff }
a:nth-child(2n+1) { color: #fff }
a:nth-of-type(2n-1) { color: #fff }
a:nth-child(2n-1) { color: #fff }

/* Replace :root with html */
:root { color: red }

/* Simplify sorthands */
.shorthands {
margin: 1px 2px 3px 4px;
margin: 0px 0px 0px 0px;
padding: 0px 5px 0px 5px;
border-width: 0px 5px 10px 5px;
}

/* Convert some units */
.deg { transform: rotate(360deg); } // buggy?
.deg { transform: rotate(-720deg); }
.ms { transition: 15000ms; }
.ms { transition: 1500ms; }
.ms { transition: 150ms; }
.ms { transition: 11150ms; }
.mm { margin: 100mm -100mm; }

/* Remove URL/font-family quotes, but not content */
.url { background: url('//test.com/test.png') }
.url { background: url("https://test.com/test.png") }
.url { background: url('./test.png') }
.url { background: url("test.png") }
.font { font-family: "Arial", "Times New Roman" }
.content:after { content: "Hi" }

/* More tests */
h2 {
font-size: min(var(--fontSize), 10px);
}
@media (width >= 600px) {
html {
background: lime;
}
}


/* Remove trailing containers ( ' " ) } ) */
@media ( screen ) {
@keyframes animation {
0% { background: url("image.png") }
}
}

/* Remove trailing spaces */



.deg { transform: rotate(360deg); }
.deg { transform: rotate(-360deg); }


</textarea>

<style>

* { background: #222; color: #fff }

* {
box-sizing: border-box;
}

body {
margin: 0;
}

textarea {
margin: 0;
border: none;
padding: 1em;
width: 100vw;
height: 100vh;
font-family: 'Fira Code', monospace;
font-size: 14pt;
line-height: 1.75;
color: #777;
}

</style>

<script src=script.js></script>


0 comments on commit b4b2ccf

Please sign in to comment.