GithubHelp home page GithubHelp logo

jugglinmike / dressup Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 4.0 169 KB

Automatically import CSS into your JavaScript files.

License: MIT License

JavaScript 41.19% Perl 23.66% Ruby 17.95% CSS 2.28% Shell 14.92%

dressup's Introduction

DressUp

Automatically import CSS into your JavaScript files.

Why

This repository was created to help optimize performance of third-party JavaScript applications. A more thorough review of the motivation can be found in the article "Third-Party JavaScript Development: Optimizing CSS Delivery", originally published on the Bocoup blog.

How

The build process includes the specified CSS without breaking the JavaScript (specifically, removing newlines and escaping the appropriate quotation marks. CSS compression is highly recommended and should take place in an intermediary step prior to this process. This functionality has been implemented in Perl, Ruby, and JavaScript (via Node.js). These scripts implement a very simple find-and-replace algorithm, looking for two directives in the input JavaScript file(s): !import_rule and !import_file.

From the JavaScript

String literals containing the previously-mentioned directives will be used as expansion targets.

Including rules.

To include a specific CSS rule (i.e. div.mike) in a JavaScript file:

var myRule = "!import_rule div.mike";

...this would yield JavaScript like:

var myRule = "background-color:#a00;z-index: 4;";

When building, be sure to supply a CSS file which defines the specified rule (more on this in the next section).

Including stylesheet files.

To import a complete stylesheet file (i.e. mark.css) in a JavaScript file:

var mySheet = "!import_file ../styles/mark.css";

...this would yield JavaScript like:

var mySheet = "div.mark { height: 120%; } div.matt { float: right; }";

Please note that the file path is interpreted in relation to the JavaScript file's location.

If either of these directives end with the !important keyword, every imported statement will be declared as !important. For instance, by re-writing the previous example:

var mySheet = "!import_file ../styles/mark.css !important";

...the following JavaScript file would be built:

var mySheet = "div.mark { height: 120% !important; } div.matt { float: right !important; }";

From the command line

Arguments. Each implementation is run from the command line, and accepts an arbitrary number of arguments. Each argument should be a path (relative or absolute) to a JavaScript (.js) or CSS (.css) file. For example:

src/dressup.rb test/input/import_rule.js test/input/import_rule.css

Of course, if file globs are available in your environment, the above may be simplified to:

src/dressup.rb test/input/import_rule.*

If importing one or more CSS rules via the import_rule syntax, be sure to supply the script with a CSS file that defines those rules!

Output. The built JavaScript is printed to standard out. If more than one JavaScript file is specified as input, a concatenation of the built versions of these files will be output.

Tests?

Good question! This project is backed by acceptance tests; you can run these with make test (to test all implementations). make testperl, make testruby, and make testjs (to test specific implementations). The tests require the "grep" utility.

License

Copyright (c) 2012 Mike Pennisi Licensed under the MIT license.

dressup's People

Contributors

jugglinmike avatar wolfgangcodes avatar

Stargazers

Eliaz Bobadilla avatar Yasin ATEŞ avatar 99corps avatar Vesa Vänskä avatar modakar avatar Jonathan Barnett avatar Tim Branyen avatar Jeff Vlahos avatar Michael Vera avatar Krister Kari avatar  avatar Rui Marinho avatar Matt Mueller avatar Boaz avatar Ben Vinegar avatar

Watchers

 avatar Krister Kari avatar Michael Anthony avatar  avatar

dressup's Issues

.

.

Multiple !import_file in a single JavaScript file (Perl)

I added two !import_file commands in a single JavaScript file and it failed to parse the content using the Perl script.

The JS file contained:

var css1 = "!import_file /file/path.css !important", css2 = "!import_file file/path2.css !important";

When performing the the replacement the Perl regex was matching the following string as the $filename:

"/file/path.css !important!import_file file/path2.css"

And was failing to find the file.

It would be great to be able to add two files to a single !import_file command.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.