Skip to content

Commit

Permalink
better free var filling, node.js test
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 5, 2012
1 parent 97ed773 commit ebd6488
Show file tree
Hide file tree
Showing 62 changed files with 6,730 additions and 412 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/test/js
/test/browsertest/js
/examples/*/js
/test/browsertest/node_modules/vm-browserify
/examples/*/js
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Initially only `web.js` is included (and loaded) by your application.
`1.web.js` is loaded when the call to `require.ensure` happens.
After the second bundle (`1.web.js`) is loaded, the callback function will be invoked.

See [details](modules-webpack/tree/master/examples/code-splitting) for exact output.
See [details](/sokra/modules-webpack/tree/master/examples/code-splitting) for exact output.

See [more examples](/sokra/modules-webpack/tree/master/examples).

## Reusing node.js code

Expand Down Expand Up @@ -250,9 +252,16 @@ Some credit goes to the browserify contributors, you can use replacements provid

Included simple replacements:

* `assert`: copy of node.js' version
* `assert`: copy of node.js' version, small change
* `buffer`: copy of node-browserify's version
* `buffer_ieee754`: copy of node-browserify's version
* `child_process`: disabled
* `events`: copy of node.js' version
* `path`: copy of node.js' version
* `punycode`: copy of node.js' version, one line removed
* `querystring`: copy of node.js' version
* `string_decoder`: copy of node.js' version
* `url`: copy of node.js' version
* `util`: copy of node.js' version

Here is a list of possible useful replacements: (intentionally not by default)
Expand Down Expand Up @@ -369,7 +378,7 @@ free module variables which are replaced with a module. ex. `{ "$": "jquery" }`

`function(err, source / stats)`
`source` if `options.output` is not set
else `stats` as json see [example](modules-webpack/tree/master/examples/code-splitting)
else `stats` as json see [example](/sokra/modules-webpack/tree/master/examples/code-splitting)

## Comparison

Expand Down
85 changes: 57 additions & 28 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var argv = require("optimist")
.describe("json", "Output Stats as JSON")
.default("json", false)

.boolean("verbose")
.describe("verbose", "Output dependencies in Stats")
.default("verbose", false)

.string("alias")
.describe("alias", "Set a alias name for a module. ex. http=http-browserify")

Expand Down Expand Up @@ -128,50 +132,75 @@ if(argv.single) {
if(argv.json)
console.log(util.inspect(stats, false, 10, argv.colors));
else {
console.log("Chunks: \033[1m" + stats.chunkCount + "\033[22m");
console.log("Modules: \033[1m" + stats.modulesCount + "\033[22m");
console.log("Modules including duplicates: \033[1m" + stats.modulesIncludingDuplicates + "\033[22m");
console.log("Modules pre chunk: \033[1m" + stats.modulesPerChunk + "\033[22m");
console.log("Modules first chunk: \033[1m" + stats.modulesFirstChunk + "\033[22m");
function c(str) {
return argv.colors ? str : "";
}
console.log("Chunks: "+c("\033[1m") + stats.chunkCount + c("\033[22m"));
console.log("Modules: "+c("\033[1m") + stats.modulesCount + c("\033[22m"));
console.log("Modules including duplicates: "+c("\033[1m") + stats.modulesIncludingDuplicates + c("\033[22m"));
console.log("Modules pre chunk: "+c("\033[1m") + stats.modulesPerChunk + c("\033[22m"));
console.log("Modules first chunk: "+c("\033[1m") + stats.modulesFirstChunk + c("\033[22m"));
if(stats.fileSizes)
for(var file in stats.fileSizes) {
console.log("\033[1m" + sprintf("%" + (5 + options.output.length) + "s", file) + "\033[22m: \033[1m" + sprintf("%8d", stats.fileSizes[file]) + "\033[22m characters");
console.log(c("\033[1m") + sprintf("%" + (5 + options.output.length) + "s", file) + c("\033[22m")+": "+c("\033[1m") + sprintf("%8d", stats.fileSizes[file]) + c("\033[22m") + " characters");
};
var cwd = process.cwd();
var cwdParent = path.dirname(cwd);
var buildins = path.join(__dirname, "..");
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
cwd = new RegExp("^" + cwd + "|(!)" + cwd, "g");
cwdParent = cwdParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
cwdParent = new RegExp("^" + cwdParent + "|(!)" + cwdParent, "g");
buildins = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
buildins = new RegExp("^" + buildins + "|(!)" + buildins, "g");
function compressFilename(filename) {
if(!filename)
return filename;
filename = filename.replace(cwd, "!.");
filename = filename.replace(cwdParent, "!..");
filename = filename.replace(buildins, "!(webpack)");
return filename.replace(/^!/, "");
}
if(stats.fileModules) {
for(var file in stats.fileModules) {
console.log("\033[1m\033[32m" + file + "\033[39m\033[22m");
console.log(c("\033[1m\033[32m") + file + c("\033[39m\033[22m"));
var modules = stats.fileModules[file];
modules.forEach(function(module) {
console.log(" \033[1m" + sprintf("%3s", module.id+"") + " " + (module.filename || (module.dirname && ("generated " + module.dirname)) || "generated") + "\033[22m");
module.reasons.forEach(function(reason) {
switch(reason.type) {
case "require":
console.log(" \033[36mrequire (" + reason.count + "x) from " + reason.filename + "\033[39m");
break;
case "context":
console.log(" \033[90mcontext from " + reason.filename + "\033[39m");
break;
case "async require":
console.log(" \033[35masync require (" + reason.count + "x) from " + reason.filename + "\033[39m");
break;
case "async context":
console.log(" \033[35masync context from " + reason.filename + "\033[39m");
break;
default:
console.log(" \033[31m" + reason.type + "\033[39m");
}
});
console.log(" "+c("\033[1m") + sprintf("%3s", module.id+"") + " " +
(compressFilename(module.filename) ||
(module.dirname && ("[context] " + compressFilename(module.dirname))) ||
"[unknown]") + c("\033[22m"));
if(argv.verbose) {
module.reasons.forEach(function(reason) {
switch(reason.type) {
case "require":
console.log(" "+c("\033[36m")+"require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
break;
case "context":
console.log(" "+c("\033[90m")+"context from " + compressFilename(reason.filename) + c("\033[39m"));
break;
case "async require":
console.log(" "+c("\033[35m")+"async require (" + reason.count + "x) from " + compressFilename(reason.filename) + c("\033[39m"));
break;
case "async context":
console.log(" "+c("\033[35ma")+"sync context from " + compressFilename(reason.filename) + c("\033[39m"));
break;
default:
console.log(" "+c("\033[31m") + reason.type + c("\033[39m"));
}
});
}
});
}
}
if(stats.warnings) {
stats.warnings.forEach(function(warning) {
console.log("\033[1m\033[33mWARNING: " + warning + "\033[39m\033[22m");
console.log(c("\033[1m\033[33m")+"WARNING: " + warning + c("\033[39m\033[22m"));
});
}
if(stats.errors) {
stats.errors.forEach(function(error) {
console.log("\033[1m\033[31mERROR: " + error + "\033[39m\033[22m");
console.log(c("\033[1m\033[31m")+"ERROR: " + error + c("\033[39m\033[22m"));
});
}
}
Expand Down
21 changes: 10 additions & 11 deletions buildin/__webpack_console.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var console = window.console;
exports.log = (console && console.log) || function() {};
exports.info = (console && console.info) || function() {};
exports.error = (console && console.error) || function() {};
exports.warn = (console && console.warn) || function() {};
exports.dir = (console && console.dir) || function() {};
exports.time = (console && console.time) || function(label) {
module.exports = console;
for(var name in {log:1, info:1, error:1, warn:1, dir:1, trace:1, assert:1})
if(!console[name])
console[name] = function() {};
if(!console.time)
console.time = function(label) {
times[label] = Date.now();
};
exports.timeEnd = (console && console.timeEnd) || function() {
if(!console.timeEnd)
console.timeEnd = function() {
var duration = Date.now() - times[label];
exports.log('%s: %dms', label, duration);
};
exports.trace = (console && console.trace) || function() {};
exports.assert = (console && console.assert) || function() {};
console.log('%s: %dms', label, duration);
};
1 change: 1 addition & 0 deletions buildin/__webpack_dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ".";
1 change: 1 addition & 0 deletions buildin/__webpack_filename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "./index.js";
10 changes: 8 additions & 2 deletions buildin/__webpack_module.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
exports.deprecate = function() {};
exports.id = "webpack";
module.exports = function(module) {
if(!module.webpackPolyfill) {
module.deprecate = function() {};
module.id = "webpack";
module.webpackPolyfill = 1;
}
return module;
}
43 changes: 37 additions & 6 deletions buildin/__webpack_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,47 @@ if(Object.prototype.__defineGetter__) {
exports.title = window.title;
}
exports.version = exports.arch =
exports.platform = exports.execPath = "webpack";
exports.execPath = "webpack";
exports.platform = "browser";
// TODO stdin, stdout, stderr
exports.argv = ["webpack", "browser"];
exports.pid = 1;
exports.nextTick = function(func) {
setTimeout(func, 0);
exports.nextTick = (function(func) {
// from https://github.com/substack/node-browserify/blob/master/wrappers/process.js
var queue = [];
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;

if (canPost) {
window.addEventListener('message', function (ev) {
if (ev.source === window && ev.data === 'webpack-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
}

return function (fn) {
if (canPost) {
queue.push(fn);
window.postMessage('webpack-tick', '*');
}
else setTimeout(fn, 0);
};
}());
exports.cwd = function() {
return "/app";
}
exports.exit = exports.kill =
exports.chdir = exports.cwd =
exports.chdir =
exports.umask = exports.dlopen =
exports.uptime = exports.memoryUsage =
exports.uvCounters = exports.binding = function() {};
exports.features = {};
exports.uvCounters = function() {};
exports.features = {};
exports.binding = function(str) {
return {};
}
2 changes: 1 addition & 1 deletion buildin/web_modules/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function _deepEqual(actual, expected) {
if (actual === expected) {
return true;

} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
} else if (require("buffer").Buffer.isBuffer(actual) && require("buffer").Buffer.isBuffer(expected)) {
if (actual.length != expected.length) return false;

for (var i = 0; i < actual.length; i++) {
Expand Down
Loading

0 comments on commit ebd6488

Please sign in to comment.