Skip to content

Commit 87f82e5

Browse files
committed
added pre and post loaders
1 parent 514ad01 commit 87f82e5

10 files changed

Lines changed: 146 additions & 60 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,18 @@ You can also save this options object in a JSON file and use it with the shell c
476476
// same as postprocess.normal but for contextes
477477
}
478478
}
479+
480+
postLoaders: [{test: /\.export.js$/, loader: "export"}],
481+
// default: []
482+
// syntax like resolve.loaders
483+
// all loaders which matches the file are applied after the
484+
// normal loaders. This cannot be overridden in the require call.
485+
486+
preLoaders: [{test: /\.txt$|\.html$/, loader: "normalizeNLs"}],
487+
// default: []
488+
// syntax like resolve.loaders
489+
// all loaders which matches the file are applied before the
490+
// normal loaders. This cannot be overridden in the require call.
479491
}
480492
```
481493

lib/buildDeps.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,42 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
150150
return;
151151
}
152152

153-
// exec the loaders
154-
execLoaders(context, filenameWithLoaders, loaders, [filename], [content], cacheEntry, options, processJs);
153+
var preLoaders = matchLoadersList(options.preLoaders);
154+
var postLoaders = matchLoadersList(options.postLoaders);
155+
156+
resolve.loaders(context, preLoaders, options.resolve, function(err, preLoaders) {
157+
if(err) return callback(err);
158+
resolve.loaders(context, postLoaders, options.resolve, function(err, postLoaders) {
159+
if(err) return callback(err);
160+
execLoaders(context, filenameWithLoaders, preLoaders, [filename], [content], cacheEntry, options,
161+
function(err, result, preLoadersCacheable) {
162+
execLoaders(context, filenameWithLoaders, loaders, [filename], result, cacheEntry, options,
163+
function(err, result, loadersCacheable) {
164+
execLoaders(context, filenameWithLoaders, postLoaders, [filename], result, cacheEntry, options,
165+
function(err, result, postLoadersCacheable) {
166+
if(err) return callback(err);
167+
return processJs(result, loadersCacheable && preLoadersCacheable && postLoadersCacheable)
168+
});
169+
});
170+
});
171+
});
172+
});
155173
});
156174
}
157175

176+
function matchLoadersList(list) {
177+
return list.filter(function(item) {
178+
return (item.test.test(filename));
179+
}).map(function(item) {
180+
return item.loader;
181+
}).join("!");
182+
}
183+
158184
// process the result delivered from loaders or direct from file
159185
// for inclusion into the result
160186
// (static code analysis for requires and other stuff)
161187
// [this step is cached]
162-
function processJs(err, resultBuffers, cacheable) {
163-
if(err) {
164-
callback(err);
165-
return;
166-
}
188+
function processJs(resultBuffers, cacheable) {
167189
var source = resultBuffers[0].toString("utf-8")
168190
var deps;
169191
try {

lib/resolve.js

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,62 @@ function resolve(context, identifier, options, type, callback) {
5050
}
5151

5252
function doResolve(context, identifier, options, type, callback) {
53-
if(!callback) {
54-
callback = options;
55-
options = {};
53+
var identifiers = identifier.replace(/^!|!$/g, "").replace(/!!/g, "!").split(/!/g);
54+
var resource = identifiers.pop();
55+
resolve(context, resource, options, type, function(err, resource) {
56+
if(err) return callback(err);
57+
if(identifier.indexOf("!") === -1) {
58+
for(var i = 0; i < options.loaders.length; i++) {
59+
var line = options.loaders[i];
60+
if(line.test.test(resource)) {
61+
Array.prototype.push.apply(identifiers, line.loader.split(/!/g));
62+
break;
63+
}
64+
}
65+
}
66+
resolveLoaders(context, identifiers, options, function(err, identifiers) {
67+
identifiers.push(resource);
68+
var intermediateResult = identifiers.join("!");
69+
var postprocessors = options.postprocess[type].slice(0);
70+
postprocessors.push(function(result) {
71+
callback(null, result);
72+
});
73+
(function next(err, result) {
74+
if(err)
75+
return callback(new Error("File \"" + intermediateResult + "\" is blocked by postprocessors: " + err));
76+
postprocessors.shift()(result, next);
77+
})(null, intermediateResult);
78+
});
79+
});
80+
}
81+
82+
function resolveLoaders(context, identifiers, options, callback) {
83+
var errors = [];
84+
var count = identifiers.length;
85+
function endOne() {
86+
count--;
87+
if(count === 0) {
88+
if(errors.length > 0) {
89+
callback(new Error(errors.join("\n")));
90+
return;
91+
}
92+
callback(null, identifiers);
93+
}
5694
}
95+
if(count == 0) endOne(count++);
96+
identifiers.forEach(function(ident, index) {
97+
resolve(context, ident, options, "loader", function(err, filename) {
98+
if(err) {
99+
errors.push(err);
100+
} else {
101+
identifiers[index] = filename;
102+
}
103+
endOne()
104+
});
105+
});
106+
}
107+
108+
function setupDefaultOptions(options) {
57109
if(!options)
58110
options = {};
59111
if(!options.extensions)
@@ -76,53 +128,7 @@ function doResolve(context, identifier, options, type, callback) {
76128
options.postprocess.normal = [];
77129
if(!options.postprocess.context)
78130
options.postprocess.context = [];
79-
var identifiers = identifier.replace(/^!|!$/g, "").replace(/!!/g, "!").split(/!/g);
80-
var resource = identifiers.pop();
81-
resolve(context, resource, options, type, function(err, resource) {
82-
if(err) return callback(err);
83-
if(identifier.indexOf("!") === -1) {
84-
for(var i = 0; i < options.loaders.length; i++) {
85-
var line = options.loaders[i];
86-
if(line.test.test(resource)) {
87-
Array.prototype.push.apply(identifiers, line.loader.split(/!/g));
88-
break;
89-
}
90-
}
91-
}
92-
var errors = [];
93-
var count = identifiers.length;
94-
function endOne() {
95-
count--;
96-
if(count === 0) {
97-
if(errors.length > 0) {
98-
callback(new Error(errors.join("\n")));
99-
return;
100-
}
101-
identifiers.push(resource);
102-
var intermediateResult = identifiers.join("!");
103-
var postprocessors = options.postprocess[type].slice(0);
104-
postprocessors.push(function(result) {
105-
callback(null, result);
106-
});
107-
(function next(err, result) {
108-
if(err)
109-
return callback(new Error("File \"" + intermediateResult + "\" is blocked by postprocessors: " + err));
110-
postprocessors.shift()(result, next);
111-
})(null, intermediateResult);
112-
}
113-
}
114-
if(count == 0) endOne(count++);
115-
identifiers.forEach(function(ident, index) {
116-
resolve(context, ident, options, "loader", function(err, filename) {
117-
if(err) {
118-
errors.push(err);
119-
} else {
120-
identifiers[index] = filename;
121-
}
122-
endOne()
123-
});
124-
});
125-
});
131+
return options;
126132
}
127133

128134
/**
@@ -133,13 +139,37 @@ function doResolve(context, identifier, options, type, callback) {
133139
* callback: function(err, absoluteFilename)
134140
*/
135141
module.exports = function(context, identifier, options, callback) {
142+
if(!callback) {
143+
callback = options;
144+
options = {};
145+
}
146+
options = setupDefaultOptions(options);
136147
return doResolve(context, identifier, options, "normal", callback);
137148
}
138149

139150
module.exports.context = function(context, identifier, options, callback) {
151+
if(!callback) {
152+
callback = options;
153+
options = {};
154+
}
155+
options = setupDefaultOptions(options);
140156
return doResolve(context, identifier, options, "context", callback);
141157
}
142158

159+
/**
160+
* callback: function(err, absoluteFilenamesArray)
161+
*/
162+
module.exports.loaders = function(context, identifier, options, callback) {
163+
if(!callback) {
164+
callback = options;
165+
options = {};
166+
}
167+
options = setupDefaultOptions(options);
168+
var identifiers = identifier.replace(/^!|!$/g, "").replace(/!!/g, "!").split(/!/g);
169+
if(identifiers.length == 1 && identifiers[0] == "") return callback(null, []);
170+
return resolveLoaders(context, identifiers, options, callback);
171+
}
172+
143173

144174
function split(a) {
145175
return a.split(/[\/\\]/g);

lib/webpack.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ module.exports = function(context, moduleName, options, callback) {
117117
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
118118
options.resolve.loaders.push({test: /\.less$/, loader: "style!css!val/cacheable!less"});
119119

120+
options.preLoaders = options.preLoaders || [];
121+
options.postLoaders = options.postLoaders || [];
122+
120123
options.loader = options.loader || {};
121124
options.loader.emitFile = options.loader.emitFile || function(filename, content) {
122125
options.internal.fileWrites.push([path.join(options.outputDirectory, filename), content]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.4.16",
3+
"version": "0.4.17",
44
"author": "Tobias Koppers @sokra",
55
"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.",
66
"dependencies": {

test/browsertest/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ function compile() {
5050
}
5151
});
5252
var libary2 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--libary", "libary2",
53-
"--script-src-prefix", "js/", "node_modules/libary2", "js/libary2.js"], extraArgs));
53+
"--script-src-prefix", "js/", "--options", "libary2config.js", "node_modules/libary2", "js/libary2.js"], extraArgs));
5454
bindOutput(libary2);
5555
}

test/browsertest/libary2config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
postLoaders: [
3+
{ test: /extra2\.js/, loader: "raw!extra!val/cacheable" }
4+
],
5+
resolve: {
6+
postprocess: {
7+
normal: [
8+
function(filename, callback) {
9+
callback(null, filename.replace(/extra\.js/, "extra2.js"));
10+
}
11+
]
12+
}
13+
}
14+
}

test/browsertest/node_modules/extra.loader.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browsertest/node_modules/libary2/lib/extra2.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browsertest/node_modules/libary2/lib/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)