DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Minifying CSS with Clojure

Minifying CSS with Clojure

Chas Emerick user avatar by
Chas Emerick
·
Jul. 27, 11 · Interview
Like (0)
Save
Tweet
Share
6.01K Views

Join the DZone community and get the full member experience.

Join For Free

A recent project had me looking into minifying CSS.  This is unfamiliar territory for me since I’ve rarely cared much about the size of website assets.  In the beginning of the project, minifying the Javascript that was floating around was more of a priority, so I folded Google Closure into my build process (which beat out YUI’s Javascript minification by a substantial margin for my codebase – though I was quite disappointed that Google Closure’s advanced compilation appears to be unusable by mere mortals).  So, when I needed to also minify CSS (something Closure doesn’t do), I didn’t want to fritter away more time messing with YUI as well.

Thus, I reinvented the wheel – something I am usually extraordinarily reluctant to do, but this was simply too easy to pass up:

(require 'clojure.string)

(defn minify-css
  "Minifies the given CSS string, returning the result.
   If you're minifying static files, please use YUI."
  [css]
  (-> css
    (clojure.string/replace #"[\n|\r]" "")
    (clojure.string/replace #"/\*.*?\*/" "")
    (clojure.string/replace #"\s+" " ")
    (clojure.string/replace #"\s*:\s*" ":")
    (clojure.string/replace #"\s*,\s*" ",")
    (clojure.string/replace #"\s*\{\s*" "{")
    (clojure.string/replace #"\s*}\s*" "}")
    (clojure.string/replace #"\s*;\s*" ";")
    (clojure.string/replace #";}" "}")))

I’m guessing this wouldn’t fare well with the various CSS syntax tricks sometimes played by those having to accommodate multiple archaic versions of IE, but itworksforme. Caveat emptor, in any case.

It can be very handy to have minification routines available at runtime without digging into the frightening APIs of either Closure or YUI, both of which are intended to be used only from the command line.  Sometimes assets are generated dynamically, and sometimes it’s handy to be able to drop them straight into outgoing HTML pages (especially so if you want to produce a standalone version of a page, for example).

There’s scads of CSS minification snippets around that use regular expressions, so this is nothing special, but I thought some Clojure web developers might appreciate it.

From http://cemerick.com/2011/04/18/minifying-css-with-clojure/

CSS Clojure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Getting a Private SSL Certificate Free of Cost
  • Microservices 101: Transactional Outbox and Inbox
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • 5 Steps for Getting Started in Deep Learning

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: