Skip to content

Commit

Permalink
File paths can now be changed by filters.
Browse files Browse the repository at this point in the history
This commit adds an extension point called "path" to allow the
modification of file paths before they are open.
  • Loading branch information
davdiv committed Jul 17, 2012
1 parent 295a22b commit c6193ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/methods.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 8 additions & 0 deletions lib/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function Wrap (opts) {

this.files = {};
this.filters = [];
this.pathFilters = [];
this.postFilters = [];
this.preFilters = [];
this.aliases = {};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c6193ce

Please sign in to comment.