Skip to content

Commit 58586f8

Browse files
committed
make it webpack-able
1 parent 6c1e98f commit 58586f8

14 files changed

+141
-57
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2012 Tobias Koppers
3+
Copyright (c) 2012 - 2013 Tobias Koppers
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

lib/APIPlugin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ConstDependency = require("./dependencies/ConstDependency");
6+
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
67

78
function APIPlugin() {
89
}
@@ -14,6 +15,12 @@ var REPLACEMENTS = {
1415
__webpack_modules__: "require.modules",
1516
__webpack_chunk_load__: "require.e",
1617
};
18+
var REPLACEMENT_TYPES = {
19+
__webpack_public_path__: "string",
20+
__webpack_require__: "function",
21+
__webpack_modules__: "object",
22+
__webpack_chunk_load__: "function",
23+
};
1724
var IGNORES = [
1825
"call require.valueOf",
1926
"expression require.onError",
@@ -26,6 +33,9 @@ APIPlugin.prototype.apply = function(compiler) {
2633
this.state.current.addDependency(dep);
2734
return true;
2835
});
36+
compiler.parser.plugin("evaluate typeof "+key, function(expr) {
37+
return new BasicEvaluatedExpression().setString(REPLACEMENT_TYPES[key]).setRange(expr.range);
38+
});
2939
});
3040
IGNORES.forEach(function(key) {
3141
compiler.parser.plugin(key, function(expr) {

lib/Compilation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,14 @@ Compilation.prototype.createHash = function createHash() {
508508
var hashFunction = outputOptions.hashFunction;
509509
var hashDigest = outputOptions.hashDigest;
510510
var hashDigestLength = outputOptions.hashDigestLength;
511-
var hash = new (require("crypto").Hash)(hashFunction);
511+
var hash = require("crypto").createHash(hashFunction);
512512
this.mainTemplate.updateHash(hash);
513513
this.chunkTemplate.updateHash(hash);
514514
this.moduleTemplate.updateHash(hash);
515515
var i, chunk;
516516
for(i = 0; i < this.chunks.length; i++) {
517517
var chunk = this.chunks[i];
518-
var chunkHash = new (require("crypto").Hash)(hashFunction);
518+
var chunkHash = require("crypto").createHash(hashFunction);
519519
chunk.updateHash(chunkHash);
520520
this.chunkTemplate.updateHash(chunkHash);
521521
chunk.hash = chunkHash.digest(hashDigest);
@@ -531,7 +531,7 @@ Compilation.prototype.modifyHash = function modifyHash(update) {
531531
var hashFunction = outputOptions.hashFunction;
532532
var hashDigest = outputOptions.hashDigest;
533533
var hashDigestLength = outputOptions.hashDigestLength;
534-
var hash = new (require("crypto").Hash)(hashFunction);
534+
var hash = require("crypto").createHash(hashFunction);
535535
hash.update(this.fullHash);
536536
hash.update(update);
537537
this.fullHash = hash.digest(hashDigest);

lib/HotModuleReplacementPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
3636
records.moduleHashs = {};
3737
this.modules.forEach(function(module) {
3838
var identifier = module.identifier();
39-
var hash = new (require("crypto")).Hash("md5");
39+
var hash = require("crypto").createHash("md5");
4040
module.updateHash(hash);
4141
records.moduleHashs[identifier] = hash.digest("hex");
4242
});
@@ -70,7 +70,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
7070
var moduleHashs = {};
7171
this.modules.forEach(function(module) {
7272
var identifier = module.identifier();
73-
var hash = new (require("crypto")).Hash("md5");
73+
var hash = require("crypto").createHash("md5");
7474
module.updateHash(hash);
7575
hash = hash.digest("hex");
7676
module.hotUpdate = records.moduleHashs[identifier] !== hash;

lib/NormalModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ NormalModule.prototype.updateHash = function(hash) {
157157

158158
NormalModule.prototype.getSourceHash = function() {
159159
if(!this._source) return "";
160-
var hash = new (require("crypto").Hash)("md5");
160+
var hash = require("crypto").createHash("md5");
161161
hash.update(this._source.source());
162162
return hash.digest("hex");
163163
};

lib/RequestShortener.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,32 @@ var path = require("path");
66

77
function RequestShortener(directory) {
88
var parentDirectory = path.dirname(directory);
9-
var buildins = path.join(__dirname, "..");
10-
var currentDirectoryRegExp = directory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
11-
currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExp + "|(!)" + currentDirectoryRegExp, "g");
12-
var buildinsAsModule = currentDirectoryRegExp.test(buildins);
13-
var parentDirectoryRegExp = parentDirectory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
14-
parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExp + "|(!)" + parentDirectoryRegExp, "g");
15-
var buildinsRegExp = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
16-
buildinsRegExp = new RegExp("^" + buildinsRegExp + "|(!)" + buildinsRegExp, "g");
17-
18-
this.buildinsAsModule = buildinsAsModule;
19-
this.currentDirectoryRegExp = currentDirectoryRegExp;
20-
this.parentDirectoryRegExp = parentDirectoryRegExp;
21-
this.buildinsRegExp = buildinsRegExp;
9+
if(/[\/\\]$/.test(directory)) directory = directory.substr(0, directory.length - 1);
10+
if(directory) {
11+
var currentDirectoryRegExp = directory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
12+
currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExp + "|(!)" + currentDirectoryRegExp, "g");
13+
14+
this.currentDirectoryRegExp = currentDirectoryRegExp;
15+
}
16+
17+
if(/[\/\\]$/.test(parentDirectory)) parentDirectory = parentDirectory.substr(0, parentDirectory.length - 1);
18+
if(parentDirectory && parentDirectory !== directory) {
19+
var parentDirectoryRegExp = parentDirectory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
20+
parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExp + "|(!)" + parentDirectoryRegExp, "g");
21+
22+
this.parentDirectoryRegExp = parentDirectoryRegExp;
23+
}
24+
25+
if(__dirname.length >= 2) {
26+
var buildins = path.join(__dirname, "..");
27+
var buildinsAsModule = currentDirectoryRegExp.test(buildins);
28+
var buildinsRegExp = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
29+
buildinsRegExp = new RegExp("^" + buildinsRegExp + "|(!)" + buildinsRegExp, "g");
30+
31+
this.buildinsAsModule = buildinsAsModule;
32+
this.buildinsRegExp = buildinsRegExp;
33+
}
34+
2235
this.node_modulesRegExp = /\/node_modules\//g;
2336
this.index_jsRegExp = /\/index.js(!|\?|\(query\))/g;
2437
}
@@ -27,11 +40,13 @@ module.exports = RequestShortener;
2740
RequestShortener.prototype.shorten = function(request) {
2841
if(!request)
2942
return request;
30-
if(this.buildinsAsModule)
43+
if(this.buildinsAsModule && this.buildinsRegExp)
3144
request = request.replace(this.buildinsRegExp, "!(webpack)");
32-
request = request.replace(this.currentDirectoryRegExp, "!.");
33-
request = request.replace(this.parentDirectoryRegExp, "!..");
34-
if(!this.buildinsAsModule)
45+
if(this.currentDirectoryRegExp)
46+
request = request.replace(this.currentDirectoryRegExp, "!.");
47+
if(this.parentDirectoryRegExp)
48+
request = request.replace(this.parentDirectoryRegExp, "!..");
49+
if(!this.buildinsAsModule && this.buildinsRegExp)
3550
request = request.replace(this.buildinsRegExp, "!(webpack)");
3651
request = request.replace(/\\/g, "/");
3752
request = request.replace(this.node_modulesRegExp, "/~/");

lib/Stats.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
191191
};
192192

193193
Stats.prototype.toString = function toString(options) {
194+
if(!options) options = {};
194195
function d(v, d) { return v === undefined ? d : v }
195196
var useColors = d(options.colors, false);
196197

lib/WebpackOptionsApply.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,18 @@
55
var OptionsApply = require("./OptionsApply");
66

77
var FunctionModulePlugin = require("./FunctionModulePlugin");
8-
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
9-
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
10-
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
118
var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
129
var SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
13-
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
14-
var HotModuleReplacementPlugin = require("./HotModuleReplacementPlugin");
15-
var NoHotModuleReplacementPlugin = require("./NoHotModuleReplacementPlugin");
1610

17-
var PrefetchPlugin = require("./PrefetchPlugin");
1811
var SingleEntryPlugin = require("./SingleEntryPlugin");
1912
var MultiEntryPlugin = require("./MultiEntryPlugin");
20-
var CachePlugin = require("./CachePlugin");
2113
var RecordIdsPlugin = require("./RecordIdsPlugin");
2214

2315
var APIPlugin = require("./APIPlugin");
2416
var ConstPlugin = require("./ConstPlugin");
2517
var RequireJsStuffPlugin = require("./RequireJsStuffPlugin");
2618
var NodeStuffPlugin = require("./NodeStuffPlugin");
2719
var CompatibilityPlugin = require("./CompatibilityPlugin");
28-
var ProvidePlugin = require("./ProvidePlugin");
29-
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
30-
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
3120

3221
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
3322
var AMDPlugin = require("./dependencies/AMDPlugin");
@@ -36,15 +25,10 @@ var RequireContextPlugin = require("./dependencies/RequireContextPlugin");
3625
var RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
3726
var RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
3827

39-
var UglifyJsPlugin = require("./optimize/UglifyJsPlugin");
40-
var OccurenceOrderPlugin = require("./optimize/OccurenceOrderPlugin");
41-
var LimitChunkCountPlugin = require("./optimize/LimitChunkCountPlugin");
42-
var MinChunkSizePlugin = require("./optimize/MinChunkSizePlugin");
4328
var RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
4429
var RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
4530
var MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
4631
var FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
47-
var DedupePlugin = require("./optimize/DedupePlugin");
4832

4933
var UnsafeCachePlugin = require("enhanced-resolve/lib/UnsafeCachePlugin");
5034
var ModulesInDirectoriesPlugin = require("enhanced-resolve/lib/ModulesInDirectoriesPlugin");
@@ -74,20 +58,26 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
7458
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
7559
switch(options.target) {
7660
case "web":
61+
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
62+
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
7763
compiler.apply(
7864
new JsonpTemplatePlugin(options.output),
7965
new FunctionModulePlugin(options.context, options.output),
8066
new NodeSourcePlugin(options.node)
8167
);
8268
break;
8369
case "webworker":
70+
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
71+
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
8472
compiler.apply(
8573
new WebWorkerTemplatePlugin(options.output),
8674
new FunctionModulePlugin(options.context, options.output),
8775
new NodeSourcePlugin(options.node)
8876
);
8977
break;
9078
case "node":
79+
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
80+
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
9181
compiler.apply(
9282
new NodeTemplatePlugin(options.output),
9383
new FunctionModulePlugin(options.context, options.output),
@@ -96,12 +86,15 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
9686
break;
9787
}
9888
if(options.output.library || options.output.libraryTarget != "var") {
89+
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
9990
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget));
10091
}
10192

10293
if(options.hot) {
94+
var HotModuleReplacementPlugin = require("./HotModuleReplacementPlugin");
10395
compiler.apply(new HotModuleReplacementPlugin(options.output));
10496
} else {
97+
var NoHotModuleReplacementPlugin = require("./NoHotModuleReplacementPlugin");
10598
compiler.apply(new NoHotModuleReplacementPlugin());
10699
}
107100

@@ -153,6 +146,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
153146
}
154147

155148
if(options.prefetch) {
149+
var PrefetchPlugin = require("./PrefetchPlugin");
156150
options.prefetch.map(function(request) {
157151
compiler.apply(new PrefetchPlugin(options.context, request));
158152
});
@@ -180,27 +174,41 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
180174

181175
compiler.apply(new RecordIdsPlugin());
182176

183-
if(options.optimize && options.optimize.occurenceOrder)
177+
if(options.optimize && options.optimize.occurenceOrder) {
178+
var OccurenceOrderPlugin = require("./optimize/OccurenceOrderPlugin");
184179
compiler.apply(new OccurenceOrderPlugin(options.optimize.occurenceOrderPreferEntry));
180+
}
185181

186-
if(options.optimize && options.optimize.minChunkSize)
182+
if(options.optimize && options.optimize.minChunkSize) {
183+
var MinChunkSizePlugin = require("./optimize/MinChunkSizePlugin");
187184
compiler.apply(new MinChunkSizePlugin(options.optimize));
185+
}
188186

189-
if(options.optimize && options.optimize.maxChunks)
187+
if(options.optimize && options.optimize.maxChunks) {
188+
var LimitChunkCountPlugin = require("./optimize/LimitChunkCountPlugin");
190189
compiler.apply(new LimitChunkCountPlugin(options.optimize));
190+
}
191191

192-
if(options.optimize.minimize === true)
193-
compiler.apply(new UglifyJsPlugin());
194-
else if(options.optimize.minimize)
195-
compiler.apply(new UglifyJsPlugin(options.optimize.minimize));
192+
if(options.optimize.minimize) {
193+
var UglifyJsPlugin = require("./optimize/UglifyJsPlugin");
194+
if(options.optimize.minimize === true)
195+
compiler.apply(new UglifyJsPlugin());
196+
else
197+
compiler.apply(new UglifyJsPlugin(options.optimize.minimize));
198+
}
196199

197-
if(options.optimize.dedupe === true)
200+
if(options.optimize.dedupe === true) {
201+
var DedupePlugin = require("./optimize/DedupePlugin");
198202
compiler.apply(new DedupePlugin());
203+
}
199204

200-
if(options.cache === undefined ? options.watch : options.cache)
205+
if(options.cache === undefined ? options.watch : options.cache) {
206+
var CachePlugin = require("./CachePlugin");
201207
compiler.apply(new CachePlugin(typeof options.cache == "object" ? options.cache : null));
208+
}
202209

203210
if(typeof options.provide === "object") {
211+
var ProvidePlugin = require("./ProvidePlugin");
204212
for(var name in options.provide) {
205213
compiler.apply(new ProvidePlugin(name, options.provide[name]));
206214
}

lib/dependencies/AMDDefineDependencyParserPlugin.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@ module.exports = AbstractPlugin.create({
1515
switch(expr.arguments.length) {
1616
case 1:
1717
if(expr.arguments[0].type == "FunctionExpression") {
18+
// define(f() {...})
1819
fn = expr.arguments[0];
1920
} else {
21+
// define({...})
2022
var dep = new AMDDefineDependency(expr.range, expr.arguments[0].range);
2123
dep.loc = expr.loc;
2224
this.state.current.addDependency(dep);
2325
return true;
2426
}
2527
break;
2628
case 2:
27-
if(expr.arguments[0].type == "ArrayExpression" && expr.arguments[1].type == "FunctionExpression") {
29+
if(expr.arguments[0].type == "ArrayExpression") {
30+
// define([...], f() {...})
2831
array = expr.arguments[0];
2932
fn = expr.arguments[1];
30-
} else if(expr.arguments[0].type == "Literal" && expr.arguments[1].type == "FunctionExpression") {
31-
fn = expr.arguments[1];
3233
} else if(expr.arguments[0].type == "Literal" && expr.arguments[1].type == "ArrayExpression") {
34+
// define("...", [...])
3335
array = expr.arguments[1];
36+
} else if(expr.arguments[0].type == "Literal" && expr.arguments[1].type == "FunctionExpression") {
37+
// define("...", f() {...})
38+
fn = expr.arguments[1];
3439
} else if(expr.arguments[0].type == "Literal") {
40+
// define("...", {...})
3541
var dep = new AMDDefineDependency(expr.range, expr.arguments[1].range);
3642
dep.loc = expr.loc;
3743
this.state.current.addDependency(dep);
@@ -40,8 +46,8 @@ module.exports = AbstractPlugin.create({
4046
break;
4147
case 3:
4248
if(expr.arguments[0].type == "Literal" &&
43-
expr.arguments[1].type == "ArrayExpression" &&
44-
expr.arguments[2].type == "FunctionExpression") {
49+
expr.arguments[1].type == "ArrayExpression") {
50+
// define("...", [...], f() {...})
4551
array = expr.arguments[1];
4652
fn = expr.arguments[2];
4753
}
@@ -57,7 +63,7 @@ module.exports = AbstractPlugin.create({
5763
}
5864
}, this);
5965
}
60-
if(fn) {
66+
if(fn && fn.type === "FunctionExpression") {
6167
var inTry = this.scope.inTry;
6268
this.inScope(fn.params.filter(function(i) {
6369
return ["require", "module", "exports"].indexOf(i.name) < 0;

lib/web/WebEnvironmentPlugin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function WebEnvironmentPlugin(inputFileSystem, outputFileSystem) {
6+
this.inputFileSystem = inputFileSystem;
7+
this.outputFileSystem = outputFileSystem;
8+
}
9+
module.exports = WebEnvironmentPlugin;
10+
WebEnvironmentPlugin.prototype.apply = function(compiler) {
11+
var inputFileSystem = compiler.inputFileSystem = this.inputFileSystem;
12+
compiler.resolvers.normal.fileSystem = compiler.inputFileSystem;
13+
compiler.resolvers.context.fileSystem = compiler.inputFileSystem;
14+
compiler.resolvers.loader.fileSystem = compiler.inputFileSystem;
15+
compiler.outputFileSystem = this.outputFileSystem;
16+
};

0 commit comments

Comments
 (0)