Skip to content

Commit 7da31b2

Browse files
committed
change node.Buffer in a way that is backward-compatible
webpack#709
1 parent 52f9dcd commit 7da31b2

10 files changed

Lines changed: 71 additions & 5 deletions

File tree

lib/WebpackOptionsDefaulter.js

Lines changed: 3 additions & 2 deletions
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+
// TODO: add this in next major version
46+
// this.set("node.Buffer", true);
4647
this.set("node.setImmediate", true);
4748
this.set("node.__filename", "mock");
4849
this.set("node.__dirname", "mock");
@@ -135,4 +136,4 @@ WebpackOptionsDefaulter.prototype.process = function(options) {
135136
["*-webpack-node-loader", "*-node-loader", "*-loader", "*"],
136137
["*-webpack-node-webkit-loader", "*-webpack-node-loader", "*-node-loader", "*-loader", "*-web-loader", "*"],
137138
["*-loader", "*"]);
138-
};
139+
};

lib/node/NodeSourcePlugin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ 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+
var bufferType = this.options.Buffer
46+
if(typeof bufferType === "undefined") {
47+
bufferType = this.options.buffer;
48+
if(typeof bufferType === "undefined")
49+
bufferType = true;
50+
}
51+
if(bufferType) {
4752
compiler.parser.plugin("expression Buffer", function(expr) {
4853
return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(getPathToModule("buffer", bufferType)) + ").Buffer");
4954
});
@@ -70,4 +75,4 @@ NodeSourcePlugin.prototype.apply = function(compiler) {
7075
);
7176
}
7277
});
73-
};
78+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require("should");
2+
3+
it("should provide a global Buffer shim", function () {
4+
(typeof Buffer).should.be.eql("undefined");
5+
});
6+
7+
it("should fail on the buffer module", function () {
8+
(function(argument) {
9+
try {
10+
require("buffer");
11+
} catch(e) { throw e; }
12+
}).should.throw();
13+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/Cannot resolve module 'buffer'/],
3+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
target: "web",
3+
node: {
4+
buffer: false
5+
}
6+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require("should");
2+
3+
it("should provide a global Buffer shim", function () {
4+
Buffer.should.be.a.Function;
5+
});
6+
7+
it("should provide the buffer module", function () {
8+
require("buffer").should.be.an.Object;
9+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
target: "web",
3+
node: {
4+
buffer: true
5+
}
6+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require("should");
2+
3+
it("should provide a global Buffer shim", function () {
4+
Buffer.should.be.an.Function;
5+
});
6+
7+
it("should fail on the buffer module", function () {
8+
(function(argument) {
9+
try {
10+
require("buffer");
11+
} catch(e) { throw e; }
12+
}).should.throw();
13+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/Cannot resolve module 'buffer'/],
3+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
target: "web",
3+
node: {
4+
Buffer: true,
5+
buffer: false
6+
}
7+
};

0 commit comments

Comments
 (0)