Skip to content

Commit 66a0ebd

Browse files
committed
offer loader.emitFile and loader.buffers
1 parent d394065 commit 66a0ebd

File tree

4 files changed

+132
-89
lines changed

4 files changed

+132
-89
lines changed

api/getPublicPrefix.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "require.valueOf().modules.c";

lib/buildDeps.js

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
var parse = require("./parse");
66
var resolve = require("./resolve");
7+
var execLoaders = require("./execLoaders");
78
var fs = require("fs");
89
var path = require("path");
910
var assert = require("assert");
@@ -72,85 +73,6 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
7273
}
7374
}
7475

75-
function execLoaders(request, loaders, filenames, contents, options, callback) {
76-
if(loaders.length === 0)
77-
callback(null, contents[0]);
78-
else {
79-
var loaderFunctions = [];
80-
try {
81-
loaders.forEach(function(name) {
82-
var loader = require(name);
83-
loaderFunctions.push(loader);
84-
});
85-
} catch(e) {
86-
callback(e);
87-
return;
88-
}
89-
function nextLoader() {
90-
var args = Array.prototype.slice.apply(arguments);
91-
var err = args.shift();
92-
if(err) {
93-
callback(err);
94-
return;
95-
}
96-
if(loaderFunctions.length > 0) {
97-
var async = false;
98-
var done = false;
99-
try {
100-
var context = {
101-
request: request,
102-
filenames: filenames,
103-
exec: function(code, filename) {
104-
var Module = require("module");
105-
var m = new Module("exec in " + request, module);
106-
m.filename = filenames[0];
107-
m._compile(code, filename);
108-
return m.exports;
109-
},
110-
resolve: function(context, path, cb) {
111-
resolve(context, "!"+path, options.resolve, cb);
112-
},
113-
async: function() {
114-
async = true;
115-
return nextLoader;
116-
},
117-
callback: function() {
118-
async = true;
119-
assert(!done);
120-
done = true;
121-
nextLoader.apply(null, arguments);
122-
},
123-
web: true,
124-
debug: options.debug,
125-
minimize: options.minimize,
126-
values: undefined,
127-
options: options
128-
};
129-
var retVal = loaderFunctions.pop().apply(context, args);
130-
if(!async) {
131-
done = true;
132-
nextLoader(retVal === undefined ? new Error("loader did not return a value") : null, retVal);
133-
}
134-
} catch(e) {
135-
if(!done) {
136-
done = true;
137-
callback("Loader throwed exeception: " + (e.stack ? e.stack : e));
138-
} else {
139-
if(e.stack) console.error(e.stack);
140-
else console.error(e);
141-
}
142-
return;
143-
}
144-
} else {
145-
callback(null, args[0]);
146-
}
147-
}
148-
contents.unshift(null);
149-
nextLoader.apply(null, contents);
150-
}
151-
152-
}
153-
15476
function addModule(depTree, context, modu, options, reason, finalCallback) {
15577
options.events.emit("task");
15678
function callback(err, result) {
@@ -178,7 +100,7 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
178100
var loaders = filename.split(/!/g);
179101
filename = loaders.pop();
180102
options.events.emit("module", modu, filename);
181-
fs.readFile(filename, "utf-8", function(err, content) {
103+
fs.readFile(filename, function(err, content) {
182104
if(err) {
183105
callback(err);
184106
return;

lib/execLoaders.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var resolve = require("./resolve");
6+
var fs = require("fs");
7+
var path = require("path");
8+
9+
module.exports = function(request, loaders, filenames, contents, options, callback) {
10+
if(loaders.length === 0)
11+
callback(null, contents[0].toString("utf-8"));
12+
else {
13+
var loaderFunctions = [];
14+
try {
15+
loaders.forEach(function(name) {
16+
var loader = require(name);
17+
loaderFunctions.push(loader);
18+
});
19+
} catch(e) {
20+
callback(e);
21+
return;
22+
}
23+
function nextLoader() {
24+
var args = Array.prototype.slice.apply(arguments);
25+
var err = args.shift();
26+
if(err) {
27+
callback(err);
28+
return;
29+
}
30+
if(loaderFunctions.length > 0) {
31+
var async = false;
32+
var done = false;
33+
try {
34+
var context = {
35+
request: request,
36+
filenames: filenames,
37+
exec: function(code, filename) {
38+
var Module = require("module");
39+
var m = new Module("exec in " + request, module);
40+
m.filename = filenames[0];
41+
m._compile(code, filename);
42+
return m.exports;
43+
},
44+
resolve: function(context, path, cb) {
45+
resolve(context, "!"+path, options.resolve, cb);
46+
},
47+
async: function() {
48+
async = true;
49+
return context.callback;
50+
},
51+
callback: function(err) {
52+
async = true;
53+
if(done) {
54+
if(e.stack) console.error(e.stack);
55+
else console.error(e);
56+
return;
57+
}
58+
done = true;
59+
contents = [err];
60+
for(var i = 0; i < arguments.length; i++) {
61+
var arg = arguments[i];
62+
if(arg instanceof Buffer)
63+
contents.push(arg);
64+
else if(typeof arg === "string")
65+
contents.push(new Buffer(arg, "utf-8"));
66+
}
67+
nextLoader.apply(null, arguments);
68+
},
69+
web: true,
70+
debug: options.debug,
71+
minimize: options.minimize,
72+
values: undefined,
73+
options: options,
74+
buffers: args
75+
};
76+
if(options.loader) for(var key in options.loader)
77+
context[key] = options.loader[key];
78+
var params = [];
79+
args.forEach(function(arg) {
80+
if(arg instanceof Buffer)
81+
params.push(arg.toString("utf-8"));
82+
else
83+
params.push(arg);
84+
});
85+
var retVal = loaderFunctions.pop().apply(context, params);
86+
if(!async) {
87+
done = true;
88+
if(retVal instanceof Buffer)
89+
retVal = retVal;
90+
else if(typeof retVal === "string")
91+
retVal = new Buffer(retVal, "utf-8");
92+
nextLoader(retVal === undefined ? new Error("loader did not return a value") : null, retVal);
93+
}
94+
} catch(e) {
95+
if(!done) {
96+
done = true;
97+
callback("Loader throwed exeception: " + (e.stack ? e.stack : e));
98+
} else {
99+
if(e.stack) console.error(e.stack);
100+
else console.error(e);
101+
}
102+
return;
103+
}
104+
} else {
105+
callback(null, args[0].toString("utf-8"));
106+
}
107+
}
108+
contents.unshift(null);
109+
nextLoader.apply(null, contents);
110+
}
111+
112+
}

lib/webpack.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ function webpack(context, moduleName, options, finalCallback) {
134134
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
135135
options.resolve.loaders.push({test: /\.less$/, loader: "style!css!val!less"});
136136

137+
if(options.output) {
138+
if(!options.outputDirectory) {
139+
options.outputDirectory = path.dirname(options.output);
140+
options.output = path.basename(options.output);
141+
}
142+
if(!options.outputPostfix) {
143+
options.outputPostfix = "." + options.output;
144+
}
145+
}
146+
147+
var fileWrites = [];
148+
options.loader = options.loader || {};
149+
options.loader.emitFile = options.loader.emitFile || function(filename, content) {
150+
fileWrites.push([path.join(options.outputDirectory, filename), content]);
151+
}
152+
137153
options.events.emit("task", "create ouput directory");
138154
options.events.emit("task", "prepare chunks");
139155
options.events.emit("task", "statistics");
@@ -151,16 +167,8 @@ function webpack(context, moduleName, options, finalCallback) {
151167
if(!options.outputJsonpFunction)
152168
options.outputJsonpFunction = "webpackJsonp" + (options.libary || "");
153169
options.scriptSrcPrefix = options.scriptSrcPrefix || "";
154-
if(!options.outputDirectory) {
155-
options.outputDirectory = path.dirname(options.output);
156-
options.output = path.basename(options.output);
157-
}
158-
if(!options.outputPostfix) {
159-
options.outputPostfix = "." + options.output;
160-
}
161170
var fileSizeMap = {};
162171
var fileModulesMap = {};
163-
var fileWrites = [];
164172
var chunksCount = 0;
165173
var chunkIds = Object.keys(depTree.chunks);
166174
chunkIds.sort(function(a,b) {
@@ -231,7 +239,6 @@ function webpack(context, moduleName, options, finalCallback) {
231239
return;
232240
}
233241
fileWrites.push([filename, buffer]);
234-
fileSizeMap[path.basename(filename)] = buffer.length;
235242
var modulesArray = [];
236243
for(var moduleId in chunk.modules) {
237244
var modu = depTree.modules[moduleId];
@@ -286,6 +293,7 @@ function webpack(context, moduleName, options, finalCallback) {
286293
function writeFiles() {
287294
fileWrites.forEach(function(writeAction) {
288295
options.events.emit("task", "write " + writeAction[0]);
296+
fileSizeMap[path.basename(writeAction[0])] = writeAction[1].length;
289297
fs.writeFile(writeAction[0].replace(HASH_REGEXP, hash), writeAction[1], "utf-8", function(err) {
290298
options.events.emit("task-end", "write " + writeAction[0]);
291299
if(err) throw err;

0 commit comments

Comments
 (0)