diff --git a/doc/methods.markdown b/doc/methods.markdown index d5423e34a..c617e060c 100644 --- a/doc/methods.markdown +++ b/doc/methods.markdown @@ -136,6 +136,11 @@ If `ext` is 'post', execute the wrapper on the entire bundle. If `ext` is 'pre', call the wrapper function with the bundle object before the source is generated. +If `ext` is 'path', execute the wrapper for every `file` before it is open, +allowing the extension to change it. `fn` gets called as `fn.call(b, file)` +for the bundle instance `b` and the file path `file`. `fn` should return the +new path to the file. + If `ext` is an object, pull the extension from `ext.extension` and the wrapper function `fn` from `ext.wrapper`. This makes it easy to write plugins like [fileify](https://github.com/substack/node-fileify). diff --git a/lib/wrap.js b/lib/wrap.js index 937af1135..3f0625dd4 100644 --- a/lib/wrap.js +++ b/lib/wrap.js @@ -41,6 +41,7 @@ function Wrap (opts) { this.files = {}; this.filters = []; + this.pathFilters = []; this.postFilters = []; this.preFilters = []; this.aliases = {}; @@ -99,6 +100,9 @@ Wrap.prototype.register = function (ext, fn) { else if (ext === 'pre') { this.preFilters.push(fn); } + else if (ext === 'path') { + this.pathFilters.push(fn); + } else if (fn) { this.extensions.push(ext); this.filters.push(function (body, file) { @@ -137,6 +141,10 @@ Wrap.prototype.reload = function (file) { Wrap.prototype.readFile = function (file) { var self = this; + self.pathFilters.forEach(function (fn) { + file = fn.call(self, file); + }); + var body = fs.readFileSync(file, 'utf8').replace(/^#![^\n]*\n/, ''); self.filters.forEach(function (fn) {