pax_global_header 0000666 0000000 0000000 00000000064 13263340222 0014507 g ustar 00root root 0000000 0000000 52 comment=548b566818953d558d4acd804d39c30540101a68 brfs-1.6.1/ 0000775 0000000 0000000 00000000000 13263340222 0012450 5 ustar 00root root 0000000 0000000 brfs-1.6.1/.travis.yml 0000664 0000000 0000000 00000000237 13263340222 0014563 0 ustar 00root root 0000000 0000000 language: node_js node_js: - "9" - "8" - "6" - "4" - "iojs" - "0.12" - "0.10" - "0.8" before_install: - 'nvm install-latest-npm' sudo: false brfs-1.6.1/LICENSE 0000664 0000000 0000000 00000002061 13263340222 0013454 0 ustar 00root root 0000000 0000000 This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. brfs-1.6.1/bin/ 0000775 0000000 0000000 00000000000 13263340222 0013220 5 ustar 00root root 0000000 0000000 brfs-1.6.1/bin/cmd.js 0000775 0000000 0000000 00000000751 13263340222 0014327 0 ustar 00root root 0000000 0000000 #!/usr/bin/env node var fs = require('fs'); var path = require('path'); var brfs = require('../'); var file = process.argv[2]; if (file === '-h' || file === '--help') { return fs.createReadStream(path.join(__dirname, 'usage.txt')) .pipe(process.stdout) ; } var fromFile = file && file !== '-'; var rs = fromFile ? fs.createReadStream(file) : process.stdin ; var fpath = fromFile ? file : path.join(process.cwd(), '-'); rs.pipe(brfs(fpath)).pipe(process.stdout); brfs-1.6.1/bin/usage.txt 0000664 0000000 0000000 00000000371 13263340222 0015066 0 ustar 00root root 0000000 0000000 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. brfs-1.6.1/example/ 0000775 0000000 0000000 00000000000 13263340222 0014103 5 ustar 00root root 0000000 0000000 brfs-1.6.1/example/async.js 0000664 0000000 0000000 00000000171 13263340222 0015555 0 ustar 00root root 0000000 0000000 var fs = require('fs'); fs.readFile(__dirname + '/robot.html', 'utf8', function (err, html) { console.log(html); }); brfs-1.6.1/example/main.js 0000664 0000000 0000000 00000000152 13263340222 0015363 0 ustar 00root root 0000000 0000000 var fs = require('fs'); var html = fs.readFileSync(__dirname + '/robot.html', 'utf8'); console.log(html); brfs-1.6.1/example/robot.html 0000664 0000000 0000000 00000000021 13263340222 0016107 0 ustar 00root root 0000000 0000000 beep boop brfs-1.6.1/index.js 0000664 0000000 0000000 00000010142 13263340222 0014113 0 ustar 00root root 0000000 0000000 var staticModule = require('static-module'); var quote = require('quote-stream'); var through = require('through2'); var fs = require('fs'); var path = require('path'); var resolve = require('resolve'); module.exports = function (file, opts) { if (/\.json$/.test(file)) return through(); function resolver (p) { return resolve.sync(p, { basedir: path.dirname(file) }); } var vars = { __filename: file, __dirname: path.dirname(file), require: { resolve: resolver } }; if (!opts) opts = {}; if (opts.vars) Object.keys(opts.vars).forEach(function (key) { vars[key] = opts.vars[key]; }); var sm = staticModule( { fs: { readFileSync: readFileSync, readFile: readFile, readdirSync: readdirSync, readdir: readdir } }, { vars: vars, varModules: { path: path }, parserOpts: opts && opts.parserOpts, sourceMap: opts && (opts.sourceMap || opts._flags && opts._flags.debug) } ); return sm; function readFile (file, enc, cb) { if (typeof enc === 'function') { cb = enc; enc = null; } if (enc && typeof enc === 'object' && enc.encoding) { enc = enc.encoding; } var isBuffer = false; if (enc === null || enc === undefined) { isBuffer = true; enc = 'base64'; } var stream = through(write, end); stream.push('process.nextTick(function(){(' + cb + ')(null,'); if (isBuffer) stream.push('Buffer('); var s = fs.createReadStream(file, { encoding: enc }); s.on('error', function (err) { sm.emit('error', err) }); return s.pipe(quote()).pipe(stream); function write (buf, enc, next) { this.push(buf); next(); } function end (next) { if (isBuffer) this.push(',"base64")'); this.push(')})'); this.push(null); sm.emit('file', file); next() } } function readFileSync (file, enc) { var isBuffer = false; if (enc === null || enc === undefined) { isBuffer = true; enc = 'base64'; } if (enc && typeof enc === 'object' && enc.encoding) { enc = enc.encoding; } var stream = fs.createReadStream(file, { encoding: enc }) .on('error', function (err) { sm.emit('error', err) }) .pipe(quote()).pipe(through(write, end)) ; if (isBuffer) { stream.push('Buffer('); } return stream; function write (buf, enc, next) { this.push(buf); next(); } function end (next) { if (isBuffer) this.push(',"base64")'); this.push(null); sm.emit('file', file); next(); } } function readdir(path, cb) { var stream = through(write, end); stream.push('process.nextTick(function(){(' + cb + ')(null,'); fs.readdir(path, function (err, src) { if (err) { stream.emit('error', err); return; } stream.push(JSON.stringify(src)); stream.end(')})'); }); return stream; function write (buf, enc, next) { this.push(buf); next(); } function end (next) { this.push(null); next(); } } function readdirSync (path) { var stream = through(write, end); fs.readdir(path, function (err, src) { if (err) { stream.emit('error', err); return; } stream.end(JSON.stringify(src)); }); return stream; function write (buf, enc, next) { this.push(buf); next(); } function end (next) { this.push(null); next(); } } }; brfs-1.6.1/package.json 0000664 0000000 0000000 00000001646 13263340222 0014745 0 ustar 00root root 0000000 0000000 { "name": "brfs", "version": "1.6.1", "description": "browserify fs.readFileSync() static asset inliner", "main": "index.js", "bin": { "brfs": "bin/cmd.js" }, "dependencies": { "quote-stream": "^1.0.1", "resolve": "^1.1.5", "static-module": "^2.2.0", "through2": "^2.0.0" }, "devDependencies": { "browserify": "^16.1.1", "concat-stream": "^1.6.0", "tap": "~0.4.8", "through": "^2.3.4" }, "scripts": { "test": "tap test/*.js" }, "repository": { "type": "git", "url": "git://github.com/substack/brfs.git" }, "homepage": "https://github.com/substack/brfs", "keywords": [ "browserify", "browserify-transform", "fs", "readFileSync", "plugin", "static", "asset", "bundle", "base64" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } brfs-1.6.1/readme.markdown 0000664 0000000 0000000 00000010432 13263340222 0015451 0 ustar 00root root 0000000 0000000 # brfs fs.readFileSync() and fs.readFile() static asset browserify transform [](http://travis-ci.org/browserify/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. # example for a main.js: ``` js var fs = require('fs'); var html = fs.readFileSync(__dirname + '/robot.html', 'utf8'); console.log(html); ``` and a robot.html: ``` html beep boop ``` 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, ``` js var html = fs.readFileSync(__dirname + '/robot.html', 'utf8'); ``` turns into: ``` js var html = "beep boop\n"; ``` ## or with the api ``` 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')); ``` ## async You can also use `fs.readFile()`: ``` js var fs = require('fs'); fs.readFile(__dirname + '/robot.html', 'utf8', function (err, html) { console.log(html); }); ``` When you run this code through brfs, it turns into: ``` js var fs = require('fs'); process.nextTick(function () {(function (err, html) { console.log(html); })(null,"beep boop\n")}); ``` # methods brfs looks for: * `fs.readFileSync(pathExpr, enc=null)` * `fs.readFile(pathExpr, enc=null, cb)` * `fs.readdirSync(pathExpr)` * `fs.readdir(pathExpr, cb)` Inside of each `pathExpr`, you can use [statically analyzable](http://npmjs.org/package/static-eval) expressions and these variables and functions: * `__dirname` * `__filename` * `path` if you `var path = require('path')` first * `require.resolve()` 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](https://npmjs.org/package/watchify), the inlined assets will be updated automatically. If you want to use this plugin directly, not through browserify, the api follows. ``` js var brfs = require('brfs') ``` ## var tr = brfs(file, opts) Return a through stream `tr` inlining `fs.readFileSync()` file contents in-place. Optionally, you can set which `opts.vars` will be used in the [static argument evaluation](https://npmjs.org/package/static-eval) in addition to `__dirname` and `__filename`. `opts.parserOpts` can be used to configure the parser brfs uses, [acorn](https://github.com/acornjs/acorn#main-parser). # events ## 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. # usage 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. ``` # install 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. # gotchas Since `brfs` evaluates your source code *statically*, you can't use dynamic expressions that need to be evaluated at run time. For example: ```js // WILL NOT WORK! var file = window.someFilePath; var str = require('fs').readFileSync(file, 'utf8'); ``` Instead, you must use simpler expressions that can be resolved at build-time: ```js var str = require('fs').readFileSync(__dirname + '/file.txt', 'utf8'); ``` Another gotcha: `brfs` does not yet support ES module `import` statements. See [brfs-babel](https://github.com/Jam3/brfs-babel) for an experimental replacement that supports this syntax. # license MIT brfs-1.6.1/test/ 0000775 0000000 0000000 00000000000 13263340222 0013427 5 ustar 00root root 0000000 0000000 brfs-1.6.1/test/ag.js 0000664 0000000 0000000 00000001034 13263340222 0014352 0 ustar 00root root 0000000 0000000 var test = require('tap').test; var browserify = require('browserify'); var vm = require('vm'); var fs = require('fs'); var path = require('path'); test('skip parsing json', function (t) { t.plan(1); var b = browserify(); b.add(__dirname + '/files/ag.js'); b.transform(path.dirname(__dirname)); b.bundle(function (err, src) { if (err) t.fail(err); vm.runInNewContext(src, { console: { log: log } }); }); function log (msg) { t.equal('