Skip to content
Browse files

File paths can now be changed by filters.

This commit adds an extension point called "path" to allow the
modification of file paths before they are open.
  • Loading branch information...
1 parent 295a22b commit c6193cea3cd167e462733cb596e79e0a2867e262 @davdiv davdiv committed Jul 17, 2012
Showing with 13 additions and 0 deletions.
  1. +5 −0 doc/methods.markdown
  2. +8 −0 lib/wrap.js
View
5 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).
View
8 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) {

0 comments on commit c6193ce

Please sign in to comment.
Something went wrong with that request. Please try again.