• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Half-assed CSS preprocessor.

Hasp

Half-assed CSS preprocessor.

Motivation

Tired of all the slow and bloated preprocessors out there, I talked to @soveran about writing my own take, which would address only basic functionality:

  • Be very fast.
  • Support variables.
  • Support includes.
  • Implement media queries efficiently.

The last point is especially important to me. I want to define how a selector behaves on all breakpoints in a single place. On the other hand, producing n @media declarations is wasteful. So my solution would buffer all rules defined for a breakpoint, group them and print them at the end of the resulting file.

Michel suggested I look at M4, a macro processor you already have on your system.

The result is a 30-LOC minimal preprocessor. You know, "it's okay if it's half-assed as long as it's the right half of the ass".

Usage

Open styles.hcss (the extension doesn't matter, but you may want to name the file something other than .css):

set(PRIMARY_COLOR, `#f00')

body {
  background: PRIMARY_COLOR;
}

Compile:

$ hasp styles.hcss

As expected, the variable was replaced.

Now let's use breakpoints for mobile-first development:

breakpoint(TABLET, min-device-width: 768px)

selector(.site-header) {
  height: 1rem;

  on(TABLET)
    height: 3rem;
  end
}

selector(.main-content) {
  padding: 0;

  on(TABLET)
    padding: 5rem;
  end
}

The output:

.site-header {
  height: 1rem;
}

.main-content {
  padding: 0;
}

@media only screen and (min-device-width: 768px) {
  .site-header {
    height: 3rem;
  }

  .main-content {
    padding: 5rem;
  }
}

You'll see there's only one @media declaration. w00t!

But it's not a superset of CSS!

Who cares? Do you use XSLT to render your HTML templates?

OK, should I use this in production?

Yes, you can use it in production. If you do, please let me know.

If you're still not sold on the idea, and are using Less or Sass, make yourself a favor and switch to something better. Anyway, they all require Node.js, a huge dependency for this simple problem, in my opinion.

What about minification?

Hasp assumes you'll minify its output, so it doesn't make any effort in producing compressed (or pretty) output.

What about sourcemaps?

¯\_(ツ)_/¯

How do I watch and rebuild?

In general, all other solutions use very high-level languages with slow startup times, like Node.js or Java. So they need to support file-watching functionality in the same runtime, so as to avoid the slow startup.

M4 is so fast that there's no need to keep a runtime alive. Just use the beautiful entr:

$ while true; do find . -name '*.hcss' | entr -rd sh -c 'hasp styles.hcss'; done

Syntax reference

set(<name>, <value>)

Defines a variable. Per convention, variables names should be upper-case.

Example:

set(BACKGROUND, white)

body {
  background: BACKGROUND;
}

include(<file>)

Includes another Hasp file.

Example:

body {
  background: white;
}

include(path/to/other.hcss)

breakpoint(<name>, <expression>)

Defines a named breakpoint for later use.

Example:

breakpoint(TABLET,  min-device-width: 768px)
breakpoint(DESKTOP, min-device-width: 900px)

selector(<rule>) on(<breakpoint>)

Declares a rule that will be affected by breakpoints.

Example:

breakpoint(TABLET,  min-device-width: 768px)

selector(.main-content) {
  width: 10rem;

  on(TABLET)
    width: 20rem;
  end
}

Installation

Until Hasp becomes available in package managers, you can install it by copying bin/hasp somewhere in your $PATH:

$ wget https://raw.githubusercontent.com/djanowski/hasp/master/bin/hasp /usr/local/bin/hasp
$ chmod +x /usr/local/bin/hasp

You should check that M4 is available on your system, but it most likely is:

$ which m4
/usr/bin/m4

Examples

Check out the examples directory.

More Repositories

1

cutest

Isolated tests in Ruby.
Ruby
154
star
2

ack-tutorial

Perl
80
star
3

batch

Iterate over Enumerables with progress reporting.
Ruby
59
star
4

pygmentize

A Ruby gem that vendors Pygments
Ruby
49
star
5

tele

Ruby
31
star
6

mock-server

Ruby
30
star
7

dependencies

Specify your project's dependencies in a single place.
Ruby
26
star
8

yoredis

A minimalistic Redis client using modern Node.js.
JavaScript
22
star
9

collage

Rack middleware that packages your JS into a single file.
Ruby
18
star
10

replete

Ruby
16
star
11

joe

Release your gems, no pain involved.
Ruby
15
star
12

google_services

Rails plugin that provides simple integration with Google services like Analytics, Webmaster Tools, etc.
Ruby
13
star
13

estafabot

Alerto sobre posibles intentos de estafa en Twitter.
JavaScript
12
star
14

havanna

Ruby workers for Disque.
Ruby
11
star
15

twisque

A Buffer clone using Ruby and Disque, deployed to Heroku for free.
Ruby
11
star
16

websequencediagrams

Command-line utility to interface with websequencediagrams.com.
10
star
17

git-spring-cleaning

Clean up your local Git repository every once in a while.
Shell
9
star
18

redic-pool

A Redis connection pool using Redic.
Ruby
8
star
19

redis-pool

A Redis connection pool on top of redis-rb.
Ruby
8
star
20

helm

A command line interface to interact with project management tools.
Ruby
8
star
21

layout

Enable Rails to handle layouts programatically
Ruby
7
star
22

ost-bin

ost(1)
Ruby
7
star
23

cacho

A careless caching client optimized for scraping.
Ruby
6
star
24

dolarhoy

Ruby
5
star
25

sequel-fusiontables

Fusion Tables adapter for Sequel
Ruby
5
star
26

cpa

A CLI tool to query Argentinean zip codes.
Ruby
5
star
27

lightspec

Generate RSpec User Stories stubs out of Lighthouse tickets tagged as 'story'.
4
star
28

rails_basic_helpers

A Ruby on Rails plugin providing some super simple helpers.
Ruby
4
star
29

redis-scripted

A Ruby client that supports the experimental scripting feature of Redis.
Ruby
4
star
30

node-mincsv

A minimal CSV parser for Node.js.
JavaScript
3
star
31

dis

Does it scale?
3
star
32

djanowski.github.com

3
star
33

snippets

Include snippets of content in your Rails views and let the client take it from there.
Ruby
3
star
34

validate_options

Validate your options hash.
Ruby
3
star
35

taskpaper2html

Converts a taskpaper file into HTML.
3
star
36

selenium-rc

Ruby
2
star
37

tti

Ruby (and Rails) text to image generation.
Ruby
2
star
38

par

Ruby
2
star
39

ft

Low-level interface to Fusion Tables + CLI tool
Ruby
2
star
40

minidesk

Minidesk will be a minimal help desk / ticketing system mounted on top of other services like GitHub and Google Apps.
2
star
41

webrat-selenium

Webrat's Selenium container.
1
star
42

foo

1
star
43

interwebz

Ruby
1
star
44

ohm-stats

Ruby
1
star
45

microajax

Microajax for Duo/Component.
JavaScript
1
star
46

aleph

Literate-programming HTML
Ruby
1
star
47

canonical-repo

Get redirected to a RubyGem's canonical repository!
Ruby
1
star