From 258f9f4d7d51ff984b13bf5b7fb493e6579f90c2 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 10 Jul 2012 20:59:05 +0200 Subject: [PATCH] added resolve postprocessing --- README.md | 15 +++++++++++++++ lib/resolve.js | 25 ++++++++++++++++++++----- package.json | 2 +- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 34931b5b8..7694dbcf8 100644 --- a/README.md +++ b/README.md @@ -455,6 +455,21 @@ You can also save this options object in a JSON file and use it with the shell c // {test: /\.less$/, loader: "style!css!val!less"}] // automatically use loaders if filename match RegExp // and no loader is specified + + postprocess: { + normal: [function(filename, callback) { + // webpack will not find files including ".exclude." + if(/\.exclude\.[^\\\/]*/.test(filename)) + return callback(new Error("File is excluded")); + callback(null, filename); + }], + // defaults: [] + // postprocess resolved filenames by all specified async functions + // a postprocessor must call the callback + + context: [], + // same as postprocess.normal but for contextes + } } } ``` diff --git a/lib/resolve.js b/lib/resolve.js index 1d5d50581..7f84aeee8 100644 --- a/lib/resolve.js +++ b/lib/resolve.js @@ -11,8 +11,8 @@ var fs = require("fs"); function resolve(context, identifier, options, type, callback) { function finalResult(err, absoluteFilename) { if(err) { - callback("Module \"" + identifier + "\" not found in context \"" + - context + "\"\n " + err); + callback(new Error("Module \"" + identifier + "\" not found in context \"" + + context + "\"\n " + err)); return; } callback(null, absoluteFilename); @@ -35,7 +35,7 @@ function resolve(context, identifier, options, type, callback) { return; } if(!stat.isDirectory()) { - finalResult("Context \"" + identifier + "\" in not a directory"); + finalResult(new Error("Context \"" + identifier + "\" in not a directory")); return; } callback(null, pathname); @@ -75,6 +75,12 @@ function doResolve(context, identifier, options, type, callback) { options.paths = []; if(!options.alias) options.alias = {}; + if(!options.postprocess) + options.postprocess = {}; + if(!options.postprocess.normal) + options.postprocess.normal = []; + if(!options.postprocess.context) + options.postprocess.context = []; var identifiers = identifier.replace(/^!|!$/g, "").replace(/!!/g, "!").split(/!/g); var resource = identifiers.pop(); resolve(context, resource, options, type, function(err, resource) { @@ -94,11 +100,20 @@ function doResolve(context, identifier, options, type, callback) { count--; if(count === 0) { if(errors.length > 0) { - callback(errors.join("\n")); + callback(new Error(errors.join("\n"))); return; } identifiers.push(resource); - callback(null, identifiers.join("!")); + var intermediateResult = identifiers.join("!"); + var postprocessors = options.postprocess[type].slice(0); + postprocessors.push(function(result) { + callback(null, result); + }); + (function next(err, result) { + if(err) + return callback(new Error("File \"" + intermediateResult + "\" is blocked by postprocessors: " + err)); + postprocessors.shift()(result, next); + })(null, intermediateResult); } } if(count == 0) endOne(count++); diff --git a/package.json b/package.json index 52b98b815..05d6e0009 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack", - "version": "0.4.13", + "version": "0.4.14", "author": "Tobias Koppers @sokra", "description": "Packs CommonJs Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loading of js, json, jade, coffee, css, ... out of the box and more with custom loaders.", "dependencies": {