browserify fs.readFileSync() static asset inliner

npm install brfs
184 downloads in the last day
3 438 downloads in the last week
14 332 downloads in the last month


fs.readFileSync() and fs.readFile() static asset browserify transform

build status

This module is a plugin for browserify 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:

var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');

and a robot.html:

<b>beep boop</b>

first npm install brfs into your project, then:

on the command-line

$ browserify -t brfs example/main.js > bundle.js

now in the bundle output file,

var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');

turns into:

var html = "<b>beep boop</b>\n";

or with the api

var browserify = require('browserify');
var fs = require('fs');

var b = browserify('example/main.js');



You can also use fs.readFile():

var fs = require('fs');
fs.readFile(__dirname + '/robot.html', 'utf8', function (err, html) {

When you run this code through brfs, it turns into:

var fs = require('fs');
process.nextTick(function () {(function (err, html) {
})(null,"<b>beep boop</b>\n")});


brfs looks for fs.readFileSync(pathExpr, enc='utf8') and fs.readFile(pathExpr, enc=null, cb) calls.

The pathExpr function is evaluated as an expression with __dirname and __filename variables available.

Just like node, the default encoding is null and will give back a Buffer. If you want differently-encoded file contents for your inline content you can set enc to 'utf8', 'base64', or 'hex'.

In async mode when a callback cb is given, the contents of pathExpr are inlined into the source inside of a process.nextTick() call.

When you use a 'file'-event aware watcher such as watchify, the inlined assets will be updated automatically.

If you want to use this plugin directly, not through browserify, the api follows.

var brfs = require('brfs')

var tr = brfs(file)

Return a through stream tr inlining fs.readFileSync() file contents in-place.


tr.on('file', function (file) {})

For every file included with fs.readFileSync() or fs.readFile(), the tr instance emits a 'file' event with the file path.


A tiny command-line program ships with this module to make debugging easier.


  brfs file

    Inline `fs.readFileSync()` calls from `file`, printing the transformed file
    contents to stdout.

  brfs -

    Inline `fs.readFileSync()` calls from stdin, printing the transformed file
    contents to stdout.


With npm do:

npm install brfs

then use -t brfs with the browserify command or use .transform('brfs') from the browserify api.



npm loves you