browserify-css
A Browserify transform for bundling, rebasing, inlining, and minifying CSS files. It's useful for CSS modularization where styles are scoped to their related bundles.
Getting Started
If you're new to browserify, check out the browserify handbook and the resources on browserify.org.
Installation
npm install --save-dev browserify-css
Usage
app.css:
;;
app.js:
var css = ;console;
You can compile your app by passing -t browserify-css to browserify:
$ browserify -t browserify-css app.js > bundle.js
Each require('./path/to/file.css')
call will concatenate CSS files with @import statements, rebasing urls, inlining @import, and minifying CSS. It will add a style tag with an optional data-href attribute to the head section of the document during runtime:
Configuration
You can set configuration to your package.json file:
or use an external configuration file like below:
config/browserify-css.js:
moduleexports ="autoInject": true"minify": true"rootDir": ".";
Furthermore, browserify-css transform can obtain options from the command-line with subarg syntax:
$ browserify -t [ browserify-css --minify=true --output bundle.css ] -o bundle.js app.js
or from the api:
b.transform('browserify-css', {
minify: true,
output: 'bundle.css'
});
Options
autoInject
Type: Boolean
Default: true
If true, each require('path/to/file.css')
call will add a style tag to the head section of the document.
autoInjectOptions
Type: Object
Default:
"verbose": true "insertAt": "bottom" // or "top"
verbose
If verbose is set to true, the path to CSS will be specified in the data-href attribute inside the style tag
insertAt
By default, browserify-css transform appends <style> elements to the end of the <head> tag of the page. This will cause CSS created by browserify-css transform to take priority over CSS already present in the document head. To insert style elements at the beginning of the head, set the insertAt parameter to 'top'.
inlineImages
Type: Boolean
or Object
Default: false
If true, each required css file will have image url()
replaced with data urls. For example from:
);
to:
/);
inlineImagesOptions
Type: Object
Default:
// maximum size (in bytes) of image file that will be inlined into css file "limit": 0 // 0 means no limit - inline all images
If a limit is set, then only files that are smaller than the number of bytes given will be inlined into the css file.
minify
Type: Boolean
Default: false
minifyOptions
Type: Object
Default: {}
Check out a list of CSS minify options at CleanCSS.
onFlush
Type: Function
The onFlush
option accepts a function which takes two arguments: (options, done).
// @param {object} options The options object// @param {string} options.filename The filename// @param {string} options.data The CSS file content// @param {string} options.rootDir The root directory// @param {string} options.relativePath The relative path// @param {string} options.href The href attribute// @param {function} done The done callback { // Method 1: // This will keep original module.exports unchanged ; // Method 2: // Pass a null value to the done callback if you do not want to embed CSS into a JavaScript bundle ; // Method 3: // Pass a text string to the done callback to customize module.exports ;}
You can use the onFlush
option to output each CSS to a separate file, or append multiple CSS into one file. For example:
var browserify = ;var fs = ;fs;;
output
Type: String
Default: ''
The output path of the CSS file. When using this option, browserify-css will not embed stylesheets into a JavaScript bundle.
browserify -t [ browserify-css --minify=true --output bundle.css ] -o bundle.js index.js
processRelativeUrl
Type: Function
The processRelativeUrl
option accepts a function which takes one argument (the relative url) and returns the original relativeUrl
string or the converted result. For example:
var browserify = ;;
You can embed the image data directly into the CSS file with data URI, like so:
var _ = ;var path = ;var browserify = ;;
You may also want to check out the FAQ for advanced usage.
rebaseUrls
Type: Boolean
Default: true
If true, relative paths will be rebased in css files; if false, paths will be unchanged.
rootDir
Type: String
Default: ./
An absolute path to resolve relative paths against the project's base directory.
stripComments
Type: Boolean
Default: false
Strip comments from CSS. Defaults to false.
FAQ
1. How do I include CSS files located inside the node_modules folder?
You can choose one of the following methods to include CSS files located inside the node_modules folder:
- The easiest way to do this is using the
@import
rule. For example:
app.js:
;
app.css:
/* Use CSS from your node_modules folder */;/* Or your own relative files */;
- Use the global transform option (i.e.
--global-transform
or-g
) on the command line to transform all files in a node_modules directory:
$ browserify -g browserify-css app.js > bundle.js
or use the API directly:
var browserify = ;var b = ;b;b;
See browserify transform options for details.
Then you will be able to require CSS files from within node_modules. For example:
;
- Put browserify transform option into a submodule's package.json file inside the
node_modules
directory on a per-module basis like so:
node_modules/foo/package.json:
Then, run browserify transform on the command line:
$ browserify -t browserify-css app.js > bundle.js
2. How do I load font and image files from node_modules?
Assume that you have the following directory structure:
package.jsondist/src/index.jsindex.cssnode_modules/bootstrap/dist/css/bootstrap.css
The index.css
uses @import
to import external style sheets:
;
All output files, including the generated bundle.js
, are created under the dist
directory:
dist/bundle.jsvendor/bootstrap/dist/css/bootstrap.css
Suppose that the dist
directory is your web root, you might want to copy external font and images files from ../node_modules/
to dist/vendor/
.
For example, the @font-face
rules in node_modules/bootstrap/dist/css/bootstrap.css
:
The example below illustrates the use of the processRelativeUrl
option:
var gulp = ;var gutil = ;var path = ;var browserify = ;var sourceStream = ;var fse = ;var bundleStream =;bundleStream;
Acknowledgements
Test Images:
-
test/fixtures/background.png
Originally by W3C under terms of CC-BY-3.0, via Wikimedia Commons -
test/fixtures/background-600.png
Originally by Rudloff under terms of CC-BY-3.0, via Wikimedia Commons
License
MIT