webpack/lib/writeChunk.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2012-03-12 04:50:55 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2012-03-10 20:11:23 +08:00
var writeSource = require("./writeSource");
2012-05-12 22:43:37 +08:00
/**
* return the content of a chunk:
* [id]: function(...) {
* [source]
* },
*/
2012-03-10 20:11:23 +08:00
module.exports = function(depTree, chunk, options) {
if(!options) {
options = chunk;
chunk = null;
}
var buffer = [];
2012-03-15 07:05:29 +08:00
var modules = chunk ? chunk.modules : depTree.modules;
var includedModules = [];
2012-03-10 20:11:23 +08:00
for(var moduleId in modules) {
if(chunk) {
if(chunk.modules[moduleId] !== "include")
continue;
}
2012-03-15 07:05:29 +08:00
var module = depTree.modules[moduleId];
includedModules.push(module);
}
includedModules.sort(function(a, b) { return a.realId - b.realId; });
includedModules.forEach(function(module) {
2012-03-10 20:11:23 +08:00
buffer.push("/******/");
buffer.push(module.realId);
2012-03-10 20:11:23 +08:00
buffer.push(": function(module, exports, require) {\n\n");
if(options.includeFilenames) {
buffer.push("/*** ");
buffer.push(module.filename);
buffer.push(" ***/\n\n");
}
2012-05-13 22:19:11 +08:00
buffer.push(writeSource(module, options,
function(id) { return depTree.modules[id].realId },
function(id) { return depTree.chunks[id].realId }));
2012-03-10 20:11:23 +08:00
buffer.push("\n\n/******/},\n/******/\n");
});
2012-03-10 20:11:23 +08:00
return buffer.join("");
}