From 7d9cb47ca2269fd2b97306188345176b079955e1 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 23 Aug 2019 02:18:16 +0200 Subject: [PATCH 1/4] feat: add transform options to transform content --- index.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 5ca9cec..260a246 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,13 @@ module.exports = function (file, opts) { function resolver (p) { return resolve.sync(p, { basedir: path.dirname(file) }); } + function convertTransformOptionToStream (f, args) { + if (typeof f === 'function') { + return f.apply(null, args) + } + + return f; + } var vars = { __filename: file, __dirname: path.dirname(file), @@ -60,8 +67,13 @@ module.exports = function (file, opts) { var s = fs.createReadStream(file, { encoding: enc }); s.on('error', function (err) { sm.emit('error', err) }); + + if (opts.readFileTransform) { + s = s.pipe(convertTransformOptionToStream(opts.readFileTransform, [file])) + } + return s.pipe(quote()).pipe(stream); - + function write (buf, enc, next) { this.push(buf); next(); @@ -74,7 +86,7 @@ module.exports = function (file, opts) { next() } } - + function readFileSync (file, enc) { var isBuffer = false; if (enc === null || enc === undefined) { @@ -84,10 +96,16 @@ module.exports = function (file, opts) { 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)) - ; + var stream = fs.createReadStream(file, { encoding: enc }) + .on('error', function (err) { sm.emit('error', err) }); + + + if (opts.readFileSyncTransform) { + stream = stream.pipe(convertTransformOptionToStream(opts.readFileSyncTransform, [file])) + } + + stream = stream.pipe(quote()).pipe(through(write, end)); + if (isBuffer) { stream.push('Buffer('); } From ec74c23ae5184ca2f4849175b8ec7998b95b159a Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 23 Aug 2019 02:29:44 +0200 Subject: [PATCH 2/4] update readme --- readme.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.markdown b/readme.markdown index 19058a8..b3f6080 100644 --- a/readme.markdown +++ b/readme.markdown @@ -128,6 +128,12 @@ 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). +`opts.readFileTransform` can be used to transform the filestream of +the given file. For example you can minify the content. + +`opts.readFileSyncTransform` can be used to transform the filestream of +the given file. For example you can minify the content. + # events ## tr.on('file', function (file) {}) From 993382a957e2ce4af0cb0451140bddf7c389539b Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Sat, 18 Apr 2020 21:20:21 +0200 Subject: [PATCH 3/4] remove readFileSyncTransform --- index.js | 339 +++++++++++++++++++++++++----------------------- readme.markdown | 77 ++++++----- 2 files changed, 212 insertions(+), 204 deletions(-) diff --git a/index.js b/index.js index 260a246..78b9a78 100644 --- a/index.js +++ b/index.js @@ -1,170 +1,179 @@ -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'); +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) }); - } - function convertTransformOptionToStream (f, args) { - if (typeof f === 'function') { - return f.apply(null, args) - } - - return f; - } - 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]; + if (/\.json$/.test(file)) return through(); + + function resolver(p) { + return resolve.sync(p, { basedir: path.dirname(file) }); + } + function convertTransformOptionToStream(f, args) { + if (typeof f === "function") { + return f.apply(null, args); + } + + return f; + } + 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)), + inputFilename: file, + } + ); + 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); }); - - 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), - inputFilename: file - } - ); - 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) }); - - if (opts.readFileTransform) { - s = s.pipe(convertTransformOptionToStream(opts.readFileTransform, [file])) - } - - 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) }); - - - if (opts.readFileSyncTransform) { - stream = stream.pipe(convertTransformOptionToStream(opts.readFileSyncTransform, [file])) - } - - stream = stream.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(); - } + + if (opts.readFileTransform) { + s = s.pipe( + convertTransformOptionToStream(opts.readFileTransform, [file]) + ); + } + + 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); + }); + + if (opts.readFileTransform) { + stream = stream.pipe( + convertTransformOptionToStream(opts.readFileTransform, [file]) + ); + } + + stream = stream.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(); } + } }; diff --git a/readme.markdown b/readme.markdown index b3f6080..e8d714a 100644 --- a/readme.markdown +++ b/readme.markdown @@ -16,15 +16,15 @@ projects. for a main.js: -``` js -var fs = require('fs'); -var html = fs.readFileSync(__dirname + '/robot.html', 'utf8'); +```js +var fs = require("fs"); +var html = fs.readFileSync(__dirname + "/robot.html", "utf8"); console.log(html); ``` and a robot.html: -``` html +```html beep boop ``` @@ -38,65 +38,67 @@ $ browserify -t brfs example/main.js > bundle.js now in the bundle output file, -``` js -var html = fs.readFileSync(__dirname + '/robot.html', 'utf8'); +```js +var html = fs.readFileSync(__dirname + "/robot.html", "utf8"); ``` turns into: -``` js +```js var html = "beep boop\n"; ``` ## or with the api -``` js -var browserify = require('browserify'); -var fs = require('fs'); +```js +var browserify = require("browserify"); +var fs = require("fs"); -var b = browserify('example/main.js'); -b.transform('brfs'); +var b = browserify("example/main.js"); +b.transform("brfs"); -b.bundle().pipe(fs.createWriteStream('bundle.js')); +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); +```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) { +```js +var fs = require("fs"); +process.nextTick(function () { + (function (err, html) { console.log(html); -})(null,"beep boop\n")}); + })(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)` +- `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()` +- `__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 @@ -112,8 +114,8 @@ updated automatically. If you want to use this plugin directly, not through browserify, the api follows. -``` js -var brfs = require('brfs') +```js +var brfs = require("brfs"); ``` ## var tr = brfs(file, opts) @@ -131,9 +133,6 @@ in addition to `__dirname` and `__filename`. `opts.readFileTransform` can be used to transform the filestream of the given file. For example you can minify the content. -`opts.readFileSyncTransform` can be used to transform the filestream of -the given file. For example you can minify the content. - # events ## tr.on('file', function (file) {}) @@ -149,13 +148,13 @@ 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. @@ -174,18 +173,18 @@ 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: +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'); +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'); +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. From b2d0464bbbc6c441c0494a0c8def3056f47eb207 Mon Sep 17 00:00:00 2001 From: Brendan Kenny Date: Tue, 17 Nov 2020 10:02:44 -0600 Subject: [PATCH 4/4] throw on reading undefined file path (#1) --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index 78b9a78..a116c49 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,10 @@ module.exports = function (file, opts) { return sm; function readFile(file, enc, cb) { + if (file === undefined) { + throw new Error('file is not resolvable'); + } + if (typeof enc === "function") { cb = enc; enc = null; @@ -93,6 +97,10 @@ module.exports = function (file, opts) { } function readFileSync(file, enc) { + if (file === undefined) { + throw new Error('file is not resolvable'); + } + var isBuffer = false; if (enc === null || enc === undefined) { isBuffer = true;