UNPKG

browserify-adventure

Version:

learn browserify with this educational adventure

57 lines (40 loc) 2.17 kB
Building upon the previous "using transforms" section, write a custom browserify transform so that in main.js you can simply do: var txt = require('./wake.txt'); console.log(txt); and the `txt` output will be formatted with the proper line-numbering as from the previous level: printf("%3d %s", lineNum, line) every 5th line, line numbers starting from zero and four leading spaces on the other lines. You'll first need to copy wake.txt to your local directory: cp $WAKE_FILE . You will need to factor out your formatting code to be a node transform stream, which you can learn more about in the stream-adventure nodeschool workshop. The basic signature of a browserify transform is: var through = require('through2'); module.exports = function (file) { if (!/\.txt$/.test(file)) return through(); // ... return stream; }; where `stream` is a transform stream that will be written the original source contents of `file` and should output the new transformed contents. Transforms are run on all files in the current package that are loaded with `require()`. For this problem we are only interested in transforming `.txt` files, so we return a passthrough stream if the file extension is not `.txt`. The output of your stream will need to be valid javascript and will need to interface with the module system, so your transform stream should generate output that looks like: module.exports = "...transformed content goes here..." You can either buffer up in the input with the `concat-stream` module and run your previous solution to the previous "using transforms" level or if you are feeling adventurous, you can string together `split2`, `quote-stream`, and `through2` modules with `stream-combiner2` to build a transform that processes the files in-place. You will need to run browserify with `-t ./tr.js` where `tr.js` is the filename of your transform. Copy the main.js from the beginning of this problem description then to verify your solution, do: browserify -t ./tr.js main.js | $ADVENTURE_COMMAND verify or to run your solution without verifying it do: browserify -t ./tr.js main.js | $ADVENTURE_COMMAND run