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 buildDeps = require("./buildDeps");
|
|
|
|
var path = require("path");
|
|
|
|
var writeChunk = require("./writeChunk");
|
|
|
|
var fs = require("fs");
|
|
|
|
|
2012-05-01 23:46:26 +08:00
|
|
|
var HASH_REGEXP = /\[hash\]/i;
|
|
|
|
|
2012-03-10 20:11:23 +08:00
|
|
|
/*
|
|
|
|
webpack(context, moduleName, options, callback);
|
|
|
|
webpack(context, moduleName, callback);
|
|
|
|
webpack(absoluteModulePath, options, callback);
|
|
|
|
webpack(absoluteModulePath, callback);
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2012-03-10 20:11:23 +08:00
|
|
|
callback: function(err, source / stats)
|
|
|
|
source if options.output is not set
|
|
|
|
else stats json
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2012-03-10 20:11:23 +08:00
|
|
|
options:
|
|
|
|
- outputJsonpFunction
|
|
|
|
JSONP function used to load chunks
|
2012-05-12 22:43:37 +08:00
|
|
|
- publicPrefix
|
2012-03-10 20:11:23 +08:00
|
|
|
Path from where chunks are loaded
|
|
|
|
- outputDirectory
|
|
|
|
write files to this directory (absolute path)
|
|
|
|
- output
|
|
|
|
write first chunk to this file
|
|
|
|
- outputPostfix
|
|
|
|
write chunks to files named chunkId plus outputPostfix
|
|
|
|
- libary
|
|
|
|
exports of input file are stored in this variable
|
2012-05-12 22:43:37 +08:00
|
|
|
- watch
|
|
|
|
recompile on file change
|
|
|
|
- watchDelay
|
|
|
|
delay before recompile after the last file change
|
2012-03-10 20:11:23 +08:00
|
|
|
- minimize
|
|
|
|
minimize outputs with uglify-js
|
|
|
|
- includeFilenames
|
|
|
|
add absolute filenames of input files as comments
|
2012-03-16 20:59:19 +08:00
|
|
|
- resolve.alias (object)
|
|
|
|
replace a module. ex {"old-module": "new-module"}
|
|
|
|
- resolve.extensions (object)
|
2012-03-27 06:00:32 +08:00
|
|
|
possible extensions for files
|
2012-03-16 20:59:19 +08:00
|
|
|
- resolve.paths (array)
|
|
|
|
search paths
|
2012-03-27 06:00:32 +08:00
|
|
|
- resolve.loaders (array)
|
|
|
|
extension to loader mappings
|
|
|
|
{test: /\.extension$/, loader: "myloader"}
|
|
|
|
loads files that matches the RegExp to the loader if no other loader set
|
2012-03-16 20:59:19 +08:00
|
|
|
- parse.overwrites (object)
|
|
|
|
free module varables which are replaced with a module
|
|
|
|
ex. { "$": "jquery" }
|
2012-03-10 20:11:23 +08:00
|
|
|
*/
|
|
|
|
module.exports = function(context, moduleName, options, callback) {
|
2012-05-12 22:43:37 +08:00
|
|
|
// Support multiple call signitures
|
2012-03-10 20:11:23 +08:00
|
|
|
if(typeof moduleName === "object") {
|
|
|
|
callback = options;
|
|
|
|
options = moduleName;
|
|
|
|
moduleName = "./" + path.basename(context);
|
|
|
|
context = path.dirname(context);
|
|
|
|
}
|
|
|
|
if(typeof moduleName === "function") {
|
|
|
|
callback = moduleName;
|
|
|
|
options = {};
|
|
|
|
moduleName = "./" + path.basename(context);
|
|
|
|
context = path.dirname(context);
|
|
|
|
}
|
|
|
|
if(!callback) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// Create a options.events as EventEmitter
|
2012-05-02 03:33:59 +08:00
|
|
|
if(!options.events) options.events = new (require("events").EventEmitter)();
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// Register listeners to support watch mode
|
2012-05-02 03:33:59 +08:00
|
|
|
if(options.watch) {
|
|
|
|
var fs = require("fs");
|
|
|
|
var watchers = [];
|
2012-05-12 22:43:37 +08:00
|
|
|
var timeout;
|
2012-05-02 03:33:59 +08:00
|
|
|
var isRunning = true;
|
2012-05-12 22:43:37 +08:00
|
|
|
var isWaiting = false;
|
2012-05-02 03:33:59 +08:00
|
|
|
var runAgain = false;
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// Start the timeout again
|
2012-05-02 03:33:59 +08:00
|
|
|
function startAgain() {
|
2012-05-12 22:43:37 +08:00
|
|
|
isWaiting = true;
|
|
|
|
if(timeout)
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(function() {
|
|
|
|
watchers.forEach(function(watcher) {
|
|
|
|
watcher.close();
|
|
|
|
});
|
|
|
|
watchers.length = 0;
|
|
|
|
|
2012-05-02 03:33:59 +08:00
|
|
|
runAgain = false;
|
2012-05-12 22:43:37 +08:00
|
|
|
isRunning = true;
|
|
|
|
|
|
|
|
// compile
|
2012-05-02 03:33:59 +08:00
|
|
|
webpack(context, moduleName, options, callback);
|
2012-05-12 22:43:37 +08:00
|
|
|
}, options.watchDelay || 200);
|
2012-05-02 03:33:59 +08:00
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// on change
|
2012-05-02 03:33:59 +08:00
|
|
|
function change() {
|
|
|
|
if(isRunning)
|
|
|
|
runAgain = true;
|
|
|
|
else
|
|
|
|
startAgain()
|
2012-05-12 22:43:37 +08:00
|
|
|
|
2012-05-02 03:33:59 +08:00
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// on before a module is read
|
2012-05-02 03:33:59 +08:00
|
|
|
options.events.on("module", function(module, filename) {
|
|
|
|
if(!filename) return;
|
2012-05-12 22:43:37 +08:00
|
|
|
watchers.push(fs.watch(filename, function() {
|
2012-05-02 03:33:59 +08:00
|
|
|
change();
|
2012-05-12 22:43:37 +08:00
|
|
|
}));
|
2012-05-02 03:33:59 +08:00
|
|
|
});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// on before a context is enumerated
|
2012-05-02 03:33:59 +08:00
|
|
|
options.events.on("context", function(module, dirname) {
|
|
|
|
if(!dirname) return;
|
2012-05-12 22:43:37 +08:00
|
|
|
watchers.push(fs.watch(dirname, function() {
|
2012-05-02 03:33:59 +08:00
|
|
|
change();
|
2012-05-12 22:43:37 +08:00
|
|
|
}));
|
2012-05-02 03:33:59 +08:00
|
|
|
});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// on bundle finished compiling
|
2012-05-02 03:33:59 +08:00
|
|
|
options.events.on("bundle", function(stats) {
|
|
|
|
isRunning = false;
|
|
|
|
if(runAgain)
|
|
|
|
startAgain();
|
|
|
|
});
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// compile
|
2012-05-02 03:33:59 +08:00
|
|
|
return webpack(context, moduleName, options, callback);
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
function webpack(context, moduleName, options, callback) {
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if(!options.outputJsonpFunction)
|
|
|
|
options.outputJsonpFunction = "webpackJsonp" + (options.libary || "");
|
|
|
|
options.scriptSrcPrefix = options.scriptSrcPrefix || ""; // DEPRECATED
|
|
|
|
options.publicPrefix = options.publicPrefix || options.scriptSrcPrefix
|
|
|
|
|
|
|
|
if(options.output) {
|
|
|
|
if(!options.outputDirectory) {
|
|
|
|
options.outputDirectory = path.dirname(options.output);
|
|
|
|
options.output = path.basename(options.output);
|
|
|
|
}
|
|
|
|
if(!options.outputPostfix) {
|
|
|
|
options.outputPostfix = "." + options.output;
|
|
|
|
}
|
|
|
|
} // else DEPRECATED
|
|
|
|
|
2012-03-15 21:38:55 +08:00
|
|
|
options.parse = options.parse || {};
|
|
|
|
options.parse.overwrites = options.parse.overwrites || {};
|
|
|
|
options.parse.overwrites.process = options.parse.overwrites.process || ("__webpack_process");
|
2012-04-05 20:59:01 +08:00
|
|
|
options.parse.overwrites.module = options.parse.overwrites.module || ("__webpack_module+(module)");
|
2012-03-16 06:01:42 +08:00
|
|
|
options.parse.overwrites.console = options.parse.overwrites.console || ("__webpack_console");
|
|
|
|
options.parse.overwrites.global = options.parse.overwrites.global || ("__webpack_global");
|
2012-04-05 20:59:01 +08:00
|
|
|
options.parse.overwrites.Buffer = options.parse.overwrites.Buffer || ("buffer+.Buffer");
|
|
|
|
options.parse.overwrites["__dirname"] = options.parse.overwrites["__dirname"] || ("__webpack_dirname");
|
|
|
|
options.parse.overwrites["__filename"] = options.parse.overwrites["__filename"] || ("__webpack_filename");
|
2012-05-12 22:43:37 +08:00
|
|
|
|
2012-03-15 21:38:55 +08:00
|
|
|
options.resolve = options.resolve || {};
|
|
|
|
options.resolve.paths = options.resolve.paths || [];
|
2012-03-16 20:59:19 +08:00
|
|
|
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin"));
|
|
|
|
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin", "web_modules"));
|
|
|
|
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin", "node_modules"));
|
2012-03-27 06:00:32 +08:00
|
|
|
options.resolve.paths.push(path.join(path.dirname(__dirname), "node_modules"));
|
2012-03-16 20:59:19 +08:00
|
|
|
options.resolve.alias = options.resolve.alias || {};
|
2012-04-03 22:26:08 +08:00
|
|
|
options.resolve.loaders = options.resolve.loaders || [];
|
2012-03-27 06:00:32 +08:00
|
|
|
options.resolve.loaders.push({test: /\.coffee$/, loader: "coffee"});
|
|
|
|
options.resolve.loaders.push({test: /\.json$/, loader: "json"});
|
|
|
|
options.resolve.loaders.push({test: /\.jade$/, loader: "jade"});
|
2012-04-07 09:20:45 +08:00
|
|
|
options.resolve.loaders.push({test: /\.css$/, loader: "style!css"});
|
2012-05-04 22:42:38 +08:00
|
|
|
options.resolve.loaders.push({test: /\.less$/, loader: "style!css!val!less"});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
2012-05-07 23:58:36 +08:00
|
|
|
options.loader = options.loader || {};
|
|
|
|
options.loader.emitFile = options.loader.emitFile || function(filename, content) {
|
|
|
|
fileWrites.push([path.join(options.outputDirectory, filename), content]);
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// all writes to files
|
|
|
|
// items: [filename, content]
|
|
|
|
var fileWrites = [];
|
|
|
|
|
|
|
|
// Some status info
|
2012-05-02 20:06:42 +08:00
|
|
|
options.events.emit("task", "create ouput directory");
|
|
|
|
options.events.emit("task", "prepare chunks");
|
|
|
|
options.events.emit("task", "statistics");
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// build up the dependency tree
|
2012-03-10 20:11:23 +08:00
|
|
|
buildDeps(context, moduleName, options, function(err, depTree) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var buffer = [];
|
2012-05-12 22:43:37 +08:00
|
|
|
if(options.output) { // if options.output set, we write to files
|
|
|
|
|
|
|
|
// collect which module is in which file
|
2012-03-14 23:33:46 +08:00
|
|
|
var fileModulesMap = {};
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// collect which chunks exists
|
2012-03-10 20:11:23 +08:00
|
|
|
var chunksCount = 0;
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// all ids of the chunks, in desc order
|
2012-05-01 23:46:26 +08:00
|
|
|
var chunkIds = Object.keys(depTree.chunks);
|
|
|
|
chunkIds.sort(function(a,b) {
|
2012-05-17 18:45:48 +08:00
|
|
|
return depTree.chunks[b].realId - depTree.chunks[a].realId;
|
2012-05-01 23:46:26 +08:00
|
|
|
});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// the template used
|
2012-05-07 15:01:28 +08:00
|
|
|
var template = getTemplate(options, {chunks: chunkIds.length > 1});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// hash as crypto.Hash instance
|
|
|
|
// for compution
|
2012-05-01 23:46:26 +08:00
|
|
|
var hash;
|
|
|
|
try {
|
|
|
|
hash = new (require("crypto").Hash)("md5");
|
|
|
|
hash.update(JSON.stringify(options.libary || ""));
|
|
|
|
hash.update(JSON.stringify(options.outputPostfix));
|
|
|
|
hash.update(JSON.stringify(options.outputJsonpFunction));
|
2012-05-12 22:43:37 +08:00
|
|
|
hash.update(JSON.stringify(options.publicPrefix));
|
2012-05-07 15:01:28 +08:00
|
|
|
hash.update(template);
|
2012-05-01 23:46:26 +08:00
|
|
|
hash.update("1");
|
|
|
|
} catch(e) {
|
2012-05-12 22:43:37 +08:00
|
|
|
// if this didn't work
|
|
|
|
// we do not use a hash
|
2012-05-01 23:46:26 +08:00
|
|
|
hash = null;
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// for each chunk
|
2012-05-01 23:46:26 +08:00
|
|
|
chunkIds.forEach(function(chunkId) {
|
2012-03-10 20:11:23 +08:00
|
|
|
var chunk = depTree.chunks[chunkId];
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// check is chunk is empty or a duplicate
|
2012-05-01 23:46:26 +08:00
|
|
|
if(chunk.empty) return;
|
|
|
|
if(chunk.equals !== undefined) return;
|
2012-03-10 20:11:23 +08:00
|
|
|
chunksCount++;
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// build filename
|
2012-05-13 22:19:11 +08:00
|
|
|
var filename = chunk.filename = path.join(options.outputDirectory,
|
|
|
|
chunk.realId === 0 ? options.output : chunk.realId + options.outputPostfix);
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// get content of chunk
|
2012-05-01 23:46:26 +08:00
|
|
|
var content = writeChunk(depTree, chunk, options);
|
|
|
|
if(hash) hash.update(content);
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer = [];
|
2012-05-13 22:19:11 +08:00
|
|
|
if(chunk.realId === 0) { // initial chunk
|
2012-05-01 23:46:26 +08:00
|
|
|
if(hash)
|
|
|
|
hash = hash.digest("hex");
|
|
|
|
else
|
|
|
|
hash = "";
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// if it should be a libary, we prepend a variable name
|
2012-03-10 20:11:23 +08:00
|
|
|
if(options.libary) {
|
|
|
|
buffer.push("/******/var ");
|
|
|
|
buffer.push(options.libary);
|
|
|
|
buffer.push("=\n");
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// write the template
|
|
|
|
buffer.push(template);
|
|
|
|
|
|
|
|
// write extra info into object
|
2012-05-01 23:46:26 +08:00
|
|
|
if(chunkIds.length > 1) {
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push("/******/({a:");
|
2012-05-01 23:46:26 +08:00
|
|
|
buffer.push(JSON.stringify(options.outputPostfix.replace(HASH_REGEXP, hash)));
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push(",b:");
|
2012-04-03 22:26:08 +08:00
|
|
|
buffer.push(JSON.stringify(options.outputJsonpFunction));
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push(",c:");
|
2012-05-12 22:43:37 +08:00
|
|
|
buffer.push(JSON.stringify(options.publicPrefix.replace(HASH_REGEXP, hash)));
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push(",\n");
|
|
|
|
} else {
|
|
|
|
buffer.push("/******/({\n");
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
} else { // lazy loaded chunk
|
|
|
|
// write only jsonp function and chunk id as function call
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push("/******/");
|
|
|
|
buffer.push(options.outputJsonpFunction);
|
|
|
|
buffer.push("(");
|
2012-05-13 22:19:11 +08:00
|
|
|
buffer.push(chunk.realId);
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push(", {\n");
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
// write content of chunk
|
2012-05-01 23:46:26 +08:00
|
|
|
buffer.push(content);
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// and close object
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push("/******/})");
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// convert buffer to string
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer = buffer.join("");
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// minimize if wished
|
2012-03-27 06:00:32 +08:00
|
|
|
try {
|
|
|
|
if(options.minimize) buffer = uglify(buffer, filename);
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// push it as "file write"
|
|
|
|
fileWrites.unshift([filename, buffer]);
|
2012-05-01 23:46:26 +08:00
|
|
|
});
|
2012-05-02 20:06:42 +08:00
|
|
|
options.events.emit("task-end", "prepare chunks");
|
2012-05-02 03:33:59 +08:00
|
|
|
options.events.emit("start-writing", hash);
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// recursive create dir
|
2012-05-01 23:46:26 +08:00
|
|
|
function createDir(dir, callback) {
|
|
|
|
path.exists(dir, function(exists) {
|
|
|
|
if(exists)
|
|
|
|
callback();
|
|
|
|
else {
|
|
|
|
fs.mkdir(dir, function(err) {
|
|
|
|
if(err) {
|
|
|
|
var parentDir = path.join(dir, "..");
|
|
|
|
if(parentDir == dir)
|
|
|
|
return callback(err);
|
|
|
|
createDir(parentDir, function(err) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
fs.mkdir(dir, function(err) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// create output directory
|
|
|
|
var outDir = options.outputDirectory.replace(HASH_REGEXP, hash);
|
2012-05-01 23:46:26 +08:00
|
|
|
createDir(outDir, function(err) {
|
2012-05-02 20:06:42 +08:00
|
|
|
options.events.emit("task-end", "create ouput directory");
|
2012-05-01 23:46:26 +08:00
|
|
|
if(err) return callback(err);
|
|
|
|
writeFiles();
|
|
|
|
});
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// collect file sizes
|
|
|
|
var fileSizeMap = {};
|
|
|
|
|
|
|
|
// do the writing of all generated files
|
2012-05-01 23:46:26 +08:00
|
|
|
function writeFiles() {
|
2012-05-12 22:43:37 +08:00
|
|
|
var remFiles = fileWrites.length;
|
2012-05-01 23:46:26 +08:00
|
|
|
fileWrites.forEach(function(writeAction) {
|
2012-05-02 20:06:42 +08:00
|
|
|
options.events.emit("task", "write " + writeAction[0]);
|
2012-05-07 23:58:36 +08:00
|
|
|
fileSizeMap[path.basename(writeAction[0])] = writeAction[1].length;
|
2012-05-01 23:46:26 +08:00
|
|
|
fs.writeFile(writeAction[0].replace(HASH_REGEXP, hash), writeAction[1], "utf-8", function(err) {
|
2012-05-02 20:06:42 +08:00
|
|
|
options.events.emit("task-end", "write " + writeAction[0]);
|
2012-05-01 23:46:26 +08:00
|
|
|
if(err) throw err;
|
|
|
|
remFiles--;
|
|
|
|
if(remFiles === 0)
|
|
|
|
writingFinished();
|
|
|
|
});
|
|
|
|
});
|
2012-03-10 20:11:23 +08:00
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
|
|
|
|
// after writing: generate statistics
|
2012-05-01 23:46:26 +08:00
|
|
|
function writingFinished() {
|
|
|
|
// Stats
|
|
|
|
buffer = {};
|
|
|
|
buffer.hash = hash;
|
|
|
|
buffer.chunkCount = chunksCount;
|
|
|
|
buffer.modulesCount = Object.keys(depTree.modules).length;
|
|
|
|
var sum = 0;
|
2012-05-13 22:19:11 +08:00
|
|
|
chunkIds.reverse().forEach(function(chunkId) {
|
2012-05-12 22:43:37 +08:00
|
|
|
var chunk = depTree.chunks[chunkId]
|
2012-05-13 22:19:11 +08:00
|
|
|
if(!chunk.filename) return;
|
2012-05-12 22:43:37 +08:00
|
|
|
var modulesArray = [];
|
|
|
|
for(var moduleId in chunk.modules) {
|
|
|
|
if(chunk.modules[moduleId] === "include") {
|
|
|
|
var modu = depTree.modules[moduleId];
|
|
|
|
modulesArray.push({
|
|
|
|
id: modu.realId,
|
|
|
|
size: modu.size,
|
|
|
|
filename: modu.filename,
|
|
|
|
dirname: modu.dirname,
|
|
|
|
reasons: modu.reasons});
|
2012-05-01 23:46:26 +08:00
|
|
|
sum++;
|
2012-05-12 22:43:37 +08:00
|
|
|
}
|
2012-05-01 23:46:26 +08:00
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
modulesArray.sort(function(a, b) {
|
|
|
|
return a.id - b.id;
|
|
|
|
});
|
2012-05-13 22:19:11 +08:00
|
|
|
fileModulesMap[path.basename(chunk.filename)] = modulesArray;
|
|
|
|
});
|
2012-05-01 23:46:26 +08:00
|
|
|
buffer.modulesIncludingDuplicates = sum;
|
2012-05-12 22:43:37 +08:00
|
|
|
buffer.modulesPerChunk = Math.round(sum / chunksCount*10)/10; // DEPRECATED: useless info
|
2012-05-01 23:46:26 +08:00
|
|
|
sum = 0;
|
2012-05-17 18:45:48 +08:00
|
|
|
for(var moduleId in depTree.chunks.main.modules) {
|
|
|
|
if(depTree.chunks.main.modules[moduleId] === "include")
|
2012-03-10 20:11:23 +08:00
|
|
|
sum++;
|
|
|
|
}
|
2012-05-01 23:46:26 +08:00
|
|
|
buffer.modulesFirstChunk = sum;
|
|
|
|
buffer.fileSizes = fileSizeMap;
|
|
|
|
buffer.warnings = depTree.warnings;
|
|
|
|
buffer.errors = depTree.errors;
|
|
|
|
buffer.fileModules = fileModulesMap;
|
2012-05-12 22:43:37 +08:00
|
|
|
options.events.emit("task-end", "statistics");
|
2012-05-02 03:33:59 +08:00
|
|
|
options.events.emit("bundle", buffer);
|
2012-05-01 23:46:26 +08:00
|
|
|
callback(null, buffer);
|
2012-03-10 20:11:23 +08:00
|
|
|
}
|
2012-05-12 22:43:37 +08:00
|
|
|
} else { // if options.output not set, we write to stdout
|
|
|
|
// THIS IS DEPRECATED
|
2012-03-10 20:11:23 +08:00
|
|
|
if(options.libary) {
|
|
|
|
buffer.push("/******/var ");
|
|
|
|
buffer.push(options.libary);
|
|
|
|
buffer.push("=\n");
|
|
|
|
}
|
2012-05-07 15:01:28 +08:00
|
|
|
buffer.push(getTemplate(options, {chunks: false}));
|
2012-03-10 20:11:23 +08:00
|
|
|
buffer.push("/******/({\n");
|
|
|
|
buffer.push(writeChunk(depTree, options));
|
|
|
|
buffer.push("/******/})");
|
|
|
|
buffer = buffer.join("");
|
2012-03-27 06:00:32 +08:00
|
|
|
try {
|
|
|
|
if(options.minimize) buffer = uglify(buffer, "output");
|
2012-05-12 22:43:37 +08:00
|
|
|
options.events.emit("task-end", "statistics");
|
2012-03-27 06:00:32 +08:00
|
|
|
callback(null, buffer);
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
2012-03-10 20:11:23 +08:00
|
|
|
}
|
|
|
|
});
|
2012-05-02 03:33:59 +08:00
|
|
|
return options.events;
|
2012-03-10 20:11:23 +08:00
|
|
|
}
|
|
|
|
|
2012-05-12 22:43:37 +08:00
|
|
|
// returns the template for specific options
|
2012-05-07 15:01:28 +08:00
|
|
|
function getTemplate(options, templateOptions) {
|
|
|
|
if(options.template) {
|
|
|
|
if(typeof options.template === "string")
|
|
|
|
return require(options.template)(options, templateOptions);
|
|
|
|
else
|
|
|
|
return options.template(options, templateOptions);
|
2012-05-12 22:43:37 +08:00
|
|
|
} else
|
2012-05-07 15:01:28 +08:00
|
|
|
return require("../templates/browser")(options, templateOptions);
|
|
|
|
}
|
|
|
|
|
2012-05-12 22:43:37 +08:00
|
|
|
// minimize it the uglify
|
2012-03-10 20:11:23 +08:00
|
|
|
function uglify(input, filename) {
|
|
|
|
var uglify = require("uglify-js");
|
|
|
|
try {
|
|
|
|
source = uglify.parser.parse(input);
|
|
|
|
source = uglify.uglify.ast_mangle(source);
|
|
|
|
source = uglify.uglify.ast_squeeze(source);
|
|
|
|
source = uglify.uglify.gen_code(source);
|
|
|
|
} catch(e) {
|
2012-03-27 06:00:32 +08:00
|
|
|
throw new Error(filename + " @ Line " + e.line + ", Col " + e.col + ", " + e.message);
|
2012-03-10 20:11:23 +08:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
}
|