Skip to content

Commit 16777f6

Browse files
committed
Merge pull request webpack#663 from webpack/add/setimmediate
Add node-independent shim for global setImmediate()
2 parents 153f937 + 08454a6 commit 16777f6

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

lib/WebpackOptionsDefaulter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function WebpackOptionsDefaulter() {
4242
this.set("node.console", false);
4343
this.set("node.process", true);
4444
this.set("node.global", true);
45-
this.set("node.buffer", true);
45+
this.set("node.Buffer", true);
46+
this.set("node.setImmediate", true);
4647
this.set("node.__filename", "mock");
4748
this.set("node.__dirname", "mock");
4849

lib/node/NodeSourcePlugin.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ NodeSourcePlugin.prototype.apply = function(compiler) {
4242
return ModuleParserHelpers.addParsedVariable(this, "console", "require(" + JSON.stringify(getPathToModule("console", consoleType)) + ")");
4343
});
4444
}
45-
if(this.options.buffer) {
46-
var bufferType = this.options.buffer;
45+
if(this.options.Buffer) {
46+
var bufferType = this.options.Buffer;
4747
compiler.parser.plugin("expression Buffer", function(expr) {
4848
return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(getPathToModule("buffer", bufferType)) + ").Buffer");
4949
});
5050
}
51+
if(this.options.setImmediate) {
52+
var setImmediateType = this.options.setImmediate;
53+
compiler.parser.plugin("expression setImmediate", function(expr) {
54+
return ModuleParserHelpers.addParsedVariable(this, "setImmediate", "require(" + JSON.stringify(getPathToModule("timers", setImmediateType)) + ").setImmediate");
55+
});
56+
compiler.parser.plugin("expression clearImmediate", function(expr) {
57+
return ModuleParserHelpers.addParsedVariable(this, "clearImmediate", "require(" + JSON.stringify(getPathToModule("timers", setImmediateType)) + ").clearImmediate");
58+
});
59+
}
5160
var options = this.options;
5261
compiler.plugin("after-resolvers", function(compiler) {
5362
var alias = {};

test/ConfigTestCases.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ describe("ConfigTestCases", function() {
5353
function _require(module) {
5454
if(module.substr(0, 2) === "./") {
5555
var p = path.join(outputDirectory, module);
56-
var fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
56+
var fn;
57+
if (options.target === "web") {
58+
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it) {" + fs.readFileSync(p, "utf-8") + "\n})", {}, p);
59+
} else {
60+
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
61+
}
5762
var module = { exports: {} };
5863
fn.call(module.exports, _require, module, module.exports, outputDirectory, p, _it);
5964
return module.exports;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require("should");
2+
3+
// shimming global window object so the http-module is happy.
4+
// window is assigned without var on purpose.
5+
window = {
6+
XMLHttpRequest: function() {}
7+
};
8+
9+
it("should provide a global Buffer constructor", function() {
10+
Buffer.should.be.a.Function;
11+
});
12+
13+
// Webpack is not providing a console shim by default
14+
// @see lib/WebpackOptionsDefaulter.js
15+
// Uncomment this when defaults are changed
16+
//it("should provide a global console shim", function () {
17+
// console.should.be.an.Object;
18+
// console.time.should.be.a.Function;
19+
//});
20+
21+
it("should provide a global process shim", function () {
22+
process.should.be.an.Object;
23+
});
24+
25+
it("should provide a global setImmediate shim", function () {
26+
setImmediate.should.be.a.Function;
27+
});
28+
29+
it("should provide a global clearImmediate shim", function () {
30+
clearImmediate.should.be.a.Function;
31+
});
32+
33+
it("should provide an assert shim", function () {
34+
require("assert").should.be.a.Function;
35+
});
36+
37+
it("should provide a buffer shim", function () {
38+
require("buffer").should.be.an.Object;
39+
});
40+
41+
it("should provide a crypto shim", function () {
42+
require("crypto").should.be.an.Object;
43+
});
44+
45+
it("should provide a domain shim", function () {
46+
require("domain").should.be.an.Object;
47+
});
48+
49+
it("should provide an events shim", function () {
50+
require("events").should.be.a.Function;
51+
});
52+
53+
it("should provide an http shim", function () {
54+
require("http").should.be.an.Object;
55+
});
56+
57+
it("should provide an https shim", function () {
58+
require("https").should.be.an.Object;
59+
});
60+
61+
it("should provide an os shim", function () {
62+
require("os").should.be.an.Object;
63+
});
64+
65+
it("should provide a path shim", function () {
66+
require("path").should.be.an.Object;
67+
});
68+
69+
it("should provide a punycode shim", function () {
70+
require("punycode").should.be.an.Object;
71+
});
72+
73+
it("should provide a stream shim", function () {
74+
require("stream").should.be.a.Function;
75+
});
76+
77+
it("should provide a tty shim", function () {
78+
require("tty").should.be.an.Object;
79+
});
80+
81+
it("should provide a url shim", function () {
82+
require("url").should.be.an.Object;
83+
});
84+
85+
it("should provide a util shim", function () {
86+
require("util").should.be.an.Object;
87+
});
88+
89+
it("should provide a vm shim", function () {
90+
require("vm").should.be.an.Object;
91+
});
92+
93+
it("should provide a zlib shim", function () {
94+
require("zlib").should.be.an.Object;
95+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
target: "web"
3+
};

0 commit comments

Comments
 (0)