added resolve postprocessing

This commit is contained in:
Tobias Koppers 2012-07-10 20:59:05 +02:00
parent bfc627f686
commit 258f9f4d7d
3 changed files with 36 additions and 6 deletions

View File

@ -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
}
}
}
```

View File

@ -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++);

View File

@ -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": {