browserify-through v2.0.0
browserify-through
Handy dandy helper to assist you with writing Browserify transforms.
Installation
npm install browserify-throughUsage Example
Via package.json
{
"name": "fixtures",
"version": "1.0.0",
"browserify": {
"transform": [
["./transform", {"token": "__token__"}]
]
}
}var through = require('browserify-through');
module.exports = through({
map: function (filepath, opts, data, done) {
done(null, data.replace(opts.token, '__new_token__'));
},
filter: function (filepath, opts) {
return /\.js$/.test(filepath);
}
});Via Browserify API
var browserify = require('browserify');
var through = require('browserify-through');
var bundler = browserify('./src/index.js');
bundler.transform(through({
map: function (filepath, opts, data, done) {
done(null, data.replace(/foo/, 'bar'));
},
// optional filter
filter: function (filepath, opts) {
return /\.js$/.test(filepath);
}
}));API
through({opts})
opts.map-function(filepath, opts, data, [callback])- Function should either return final data or callcallback(err, data).optsare passed viapackage.json.opts.filter-function(filepath, opts)- Iffilterfunction returnstruefor a givelfilepath, the content will be passed to themapfunction. It's much more efficient to do this step separately from themapfunction because if you want to just ignore the file, it will be passed through without being stored in memory to be passed intomap.optsare passed viapackage.json.
Testing
npm testLicense
The MIT License (MIT)
Copyright 2014 Alex Gorbatchev
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.