Skip to content

Commit c912c01

Browse files
committed
separable loaders as opt-in feature, fixes webpack#16
in future separable loaders may run in a sperate process for performance reasons
1 parent 4c84def commit c912c01

4 files changed

Lines changed: 32 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ You can also save this options object in a JSON file and use it with the shell c
409409
// "module" (module, filename) before a module is loaded
410410
// "context" (module, dirname) before a context is loaded
411411
// "dependency" (filename) before a dependency is loaded
412+
// "static-dependency"(filename) after a dependency is flagged as not recompile-able
412413
// "loader" (filename) before a loader is required
413414
// -- events for progress --
414415
// "task" (name?) start of a task

lib/execLoaders.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ module.exports = function(context, request, loaders, filenames, contents, cacheE
3030
var loaderFilename = require.resolve(name);
3131
options.events.emit("loader", loaderFilename);
3232

33-
// require loader in fresh context
34-
var oldCache = {};
35-
for(var entry in require.cache) {
36-
oldCache[entry] = require.cache[entry];
37-
delete require.cache[entry];
38-
}
3933
var loader = require(loaderFilename);
40-
for(var entry in oldCache) {
41-
require.cache[entry] = oldCache[entry];
34+
35+
if(loader.separable) {
36+
// require loader in fresh context
37+
var oldCache = {};
38+
for(var entry in require.cache) {
39+
oldCache[entry] = require.cache[entry];
40+
delete require.cache[entry];
41+
}
42+
loader = require(loaderFilename);
43+
for(var entry in oldCache) {
44+
require.cache[entry] = oldCache[entry];
45+
}
46+
} else {
47+
options.events.emit("static-dependency", loaderFilename);
4248
}
4349

4450
loaderFunctions.push(loader);

lib/webpack.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module.exports = function(context, moduleName, options, callback) {
165165
var isRunning = true;
166166
var isWaiting = false;
167167
var runAgain = false;
168+
var staticChanges = [];
168169

169170
// Start the timeout again
170171
function startAgain() {
@@ -179,6 +180,11 @@ module.exports = function(context, moduleName, options, callback) {
179180
});
180181
watchers.length = 0;
181182

183+
if(staticChanges.length > 0)
184+
return callback(new Error(
185+
"Files (" + staticChanges.join(", ") +
186+
") changed. Webpack cannot recompile in this watch step."));
187+
182188
runAgain = false;
183189
isRunning = true;
184190
isWaiting = false;
@@ -229,6 +235,16 @@ module.exports = function(context, moduleName, options, callback) {
229235
}));
230236
});
231237

238+
// on before a static dependency is read
239+
options.events.on("static-dependency", function(filename) {
240+
if(!filename) return;
241+
watchers.push(fs.watch(filename, function() {
242+
if(staticChanges.indexOf(filename) == -1)
243+
staticChanges.push(filename);
244+
change();
245+
}));
246+
});
247+
232248
// on user defines the bundle as invalid
233249
options.events.on("invalid", function() {
234250
change();

test/browsertest/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ libary1.on("exit", function(code) {
3939
}
4040
});
4141
var libary2 = cp.spawn("node", join(["../../bin/webpack.js", "--colors", "--libary", "libary2",
42-
"--script-src-prefix", "js/", "--options", "libary2config.js", "node_modules/libary2", "js/libary2.js"], extraArgs));
42+
"--script-src-prefix", "js/", "--options", "libary2config.js", "node_modules/libary2", "js/libary2.js"], extraArgsNoWatch));
4343
bindOutput(libary2);

0 commit comments

Comments
 (0)