sioux-ui-switch
Version:
UISwitch module for sioux
119 lines (76 loc) • 2.25 kB
Markdown
browserify fs.readFileSync() static asset inliner
[](http://travis-ci.org/substack/brfs)
This module is a plugin for [browserify](http://browserify.org) to parse the AST
for `fs.readFileSync()` calls so that you can inline file contents into your
bundles.
Even though this module is intended for use with browserify, nothing about it is
particularly specific to browserify so it should be generally useful in other
projects.
for a main.js:
``` js
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html');
console.log(html);
```
and a robot.html:
``` html
<b>beep boop</b>
```
first `npm install brfs` into your project, then:
```
$ browserify -t brfs example/main.js > bundle.js
```
now in the bundle output file,
``` js
var html = fs.readFileSync(__dirname + '/robot.html');
```
turns into:
``` js
var html = "<b>beep boop</b>\n";
```
``` js
var browserify = require('browserify');
var fs = require('fs');
var b = browserify('example/main.js');
b.transform('brfs');
b.bundle().pipe(fs.createWriteStream('bundle.js'));
```
brfs looks for `fs.readFileSync(pathExpr, enc='utf8')` calls.
The `pathExpr` function is evaluated as an expression with `__dirname` and
`__filename` variables available.
If you want differently-encoded file contents for your inline content you can
set `enc` to `'base64'` or `'hex'`.
If you want to use this plugin directly, not through browserify, the api
follows.
``` js
var brfs = require('brfs')
```
Return a through stream `tr` inlining `fs.readFileSync()` file contents
in-place.
A tiny command-line program ships with this module to make debugging easier.
```
usage:
brfs file
Inline `fs.readFileSync()` calls from `file`, printing the transformed file
contents to stdout.
brfs
brfs -
Inline `fs.readFileSync()` calls from stdin, printing the transformed file
contents to stdout.
```
With [npm](https://npmjs.org) do:
```
npm install brfs
```
then use `-t brfs` with the browserify command or use `.transform('brfs')` from
the browserify api.
MIT