Skip to content

Commit f5651c8

Browse files
committed
added alias, added support for http/vm-browserify
1 parent 8a8c8b0 commit f5651c8

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,15 @@ if invoked without arguments it prints a usage:
173173
Usage: webpack <options> <input> <output>
174174
175175
Options:
176-
--single Disable Code Splitting [boolean] [default: false]
177-
--min Minimize it with uglifyjs [boolean] [default: false]
178-
--filenames Output Filenames Into File [boolean] [default: false]
179-
--options Options JSON File [string]
180-
--script-src-prefix Path Prefix For JavaScript Loading [string]
181-
--libary Stores the exports into this variable [string]
182-
--colors Output Stats with colors [boolean] [default: false]
176+
--single Disable Code Splitting [boolean] [default: false]
177+
--min Minimize it with uglifyjs [boolean] [default: false]
178+
--filenames Output Filenames Into File [boolean] [default: false]
179+
--options Options JSON File [string]
180+
--script-src-prefix Path Prefix For JavaScript Loading [string]
181+
--libary Stores the exports into this variable [string]
182+
--colors Output Stats with colors [boolean] [default: false]
183+
--json Output Stats as JSON [boolean] [default: false]
184+
--alias Set a alias name for a module. ex. http=http-browserify [string]
183185
```
184186

185187
### Programmatically Usage

bin/webpack.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ var argv = require("optimist")
3939
.boolean("json")
4040
.describe("json", "Output Stats as JSON")
4141
.default("json", false)
42+
43+
.string("alias")
44+
.describe("alias", "Set a alias name for a module. ex. http=http-browserify")
4245

4346
.demand(1)
4447
.argv;
@@ -75,6 +78,18 @@ if(argv.libary) {
7578
options.libary = argv.libary;
7679
}
7780

81+
if(argv.alias) {
82+
if(typeof argv.alias === "string")
83+
argv.alias = [argv.alias];
84+
options.resolve = options.resolve || {};
85+
options.resolve.alias = options.resolve.alias || {};
86+
var aliasObj = options.resolve.alias;
87+
argv.alias.forEach(function(alias) {
88+
alias = alias.split("=");
89+
aliasObj[alias[0]] = alias[1];
90+
});
91+
}
92+
7893
var webpack = require("../lib/webpack.js");
7994

8095
if(argv.single) {

lib/resolve.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module.exports = function resolve(context, identifier, options, callback) {
2525
options.extensions = [".web.js", ".js"];
2626
if(!options.paths)
2727
options.paths = [];
28+
if(!options.alias)
29+
options.alias = {};
2830
function finalResult(err, absoluteFilename) {
2931
if(err) {
3032
callback("Module \"" + identifier + "\" not found in context \"" +
@@ -35,6 +37,13 @@ module.exports = function resolve(context, identifier, options, callback) {
3537
}
3638
var identArray = split(identifier);
3739
var contextArray = split(context);
40+
while(options.alias[identArray[0]]) {
41+
var old = identArray[0];
42+
identArray[0] = options.alias[identArray[0]];
43+
identArray = split(path.join.apply(path, identArray));
44+
if(identArray[0] === old)
45+
break;
46+
}
3847
if(identArray[0] === "." || identArray[0] === ".." || identArray[0] === "" || identArray[0].match(/^[A-Z]:$/i)) {
3948
var pathname = identArray[0][0] === "." ? join(contextArray, identArray) : path.join.apply(path, identArray);
4049
loadAsFile(pathname, options, function(err, absoluteFilename) {

lib/webpack.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ var templateSingle = require("fs").readFileSync(path.join(__dirname, "templateSi
3636
minimize outputs with uglify-js
3737
- includeFilenames
3838
add absolute filenames of input files as comments
39+
- resolve.alias (object)
40+
replace a module. ex {"old-module": "new-module"}
41+
- resolve.extensions (object)
42+
possible extentions for files
43+
- resolve.paths (array)
44+
search paths
45+
- parse.overwrites (object)
46+
free module varables which are replaced with a module
47+
ex. { "$": "jquery" }
3948
*/
4049
module.exports = function(context, moduleName, options, callback) {
4150
if(typeof moduleName === "object") {
@@ -62,8 +71,12 @@ module.exports = function(context, moduleName, options, callback) {
6271
options.parse.overwrites.global = options.parse.overwrites.global || ("__webpack_global");
6372
options.resolve = options.resolve || {};
6473
options.resolve.paths = options.resolve.paths || [];
65-
options.resolve.paths.unshift(path.join(path.dirname(__dirname), "buildin"));
66-
options.resolve.paths.unshift(path.join(path.dirname(__dirname), "buildin", "web_modules"));
74+
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin"));
75+
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin", "web_modules"));
76+
options.resolve.paths.push(path.join(path.dirname(__dirname), "buildin", "node_modules"));
77+
options.resolve.alias = options.resolve.alias || {};
78+
options.resolve.alias.http = options.resolve.alias.http || path.join(path.dirname(__dirname), "node_modules", "http-browserify")
79+
options.resolve.alias.vm = options.resolve.alias.vm || path.join(path.dirname(__dirname), "node_modules", "vm-browserify")
6780
buildDeps(context, moduleName, options, function(err, depTree) {
6881
if(err) {
6982
callback(err);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand.",
66
"dependencies": {
@@ -9,6 +9,10 @@
99
"uglify-js": "1.2.5",
1010
"sprintf": "0.1.x"
1111
},
12+
"optionalDependencies": {
13+
"http-browserify": "*",
14+
"vm-browserify": "*"
15+
},
1216
"licenses": [
1317
{
1418
"type": "MIT",

0 commit comments

Comments
 (0)