Skip to content

Commit 3364a26

Browse files
committed
upgrade node browser replacements
1 parent 48ace94 commit 3364a26

4 files changed

Lines changed: 25 additions & 28 deletions

File tree

‎lib/node/NodeSourcePlugin.js‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ function NodeSourcePlugin(options) {
1414
module.exports = NodeSourcePlugin;
1515
NodeSourcePlugin.prototype.apply = function(compiler) {
1616
function ignore() { return true; }
17-
if(this.options.process == "mock") {
18-
compiler.parser.plugin("expression process", function(expr) {
19-
return ModuleParserHelpers.addParsedVariable(this, "process", "require(" + JSON.stringify(require.resolve("node-libs-browser/mock/_process")) + ")");
20-
});
21-
} else if(this.options.process) {
17+
function getPathToModule(module, type) {
18+
if(type === true || (type === undefined && nodeLibsBrowser[module])) {
19+
if(!nodeLibsBrowser[module]) throw new Error("No browser version for node.js core module '" + module + "' available");
20+
return nodeLibsBrowser[module];
21+
} else if(type === "mock") {
22+
return require.resolve("node-libs-browser/mock/" + module);
23+
} else if(type === "empty" || type === undefined) {
24+
return require.resolve("node-libs-browser/mock/empty");
25+
} else return module;
26+
}
27+
if(this.options.process) {
28+
var processType = this.options.process;
2229
compiler.parser.plugin("expression process", function(expr) {
23-
return ModuleParserHelpers.addParsedVariable(this, "process", "require(" + JSON.stringify(nodeLibsBrowser._process) + ")");
30+
return ModuleParserHelpers.addParsedVariable(this, "process", "require(" + JSON.stringify(getPathToModule("process", processType)) + ")");
2431
});
2532
}
2633
if(this.options.global) {
@@ -29,33 +36,24 @@ NodeSourcePlugin.prototype.apply = function(compiler) {
2936
return true;
3037
});
3138
}
32-
if(this.options.console == "mock") {
39+
if(this.options.console) {
40+
var consoleType = this.options.console;
3341
compiler.parser.plugin("expression console", function(expr) {
34-
return ModuleParserHelpers.addParsedVariable(this, "console", "require(" + JSON.stringify(require.resolve("node-libs-browser/mock/_console")) + ")");
35-
});
36-
} else if(this.options.console) {
37-
compiler.parser.plugin("expression console", function(expr) {
38-
return ModuleParserHelpers.addParsedVariable(this, "console", "require(" + JSON.stringify(require("node-libs-browser")._console) + ")");
42+
return ModuleParserHelpers.addParsedVariable(this, "console", "require(" + JSON.stringify(getPathToModule("console", consoleType)) + ")");
3943
});
4044
}
41-
if(this.options.buffer == "mock") {
42-
compiler.parser.plugin("expression Buffer", function(expr) {
43-
return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(require.resolve("node-libs-browser/mock/buffer")) + ").Buffer");
44-
});
45-
} else if(this.options.buffer) {
45+
if(this.options.buffer) {
46+
var bufferType = this.options.buffer;
4647
compiler.parser.plugin("expression Buffer", function(expr) {
47-
return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(require("node-libs-browser").buffer) + ").Buffer");
48+
return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(getPathToModule("buffer", bufferType)) + ").Buffer");
4849
});
4950
}
5051
var options = this.options;
5152
compiler.plugin("after-resolvers", function(compiler) {
5253
var alias = {};
5354
Object.keys(nodeLibsBrowser).forEach(function(lib) {
54-
if(/^_/.test(lib)) return;
55-
if(options[lib] === "mock")
56-
alias[lib] = require.resolve("node-libs-browser/mock/" + lib);
57-
else if(options[lib] || options[lib] === undefined)
58-
alias[lib] = require("node-libs-browser")[lib];
55+
if(options[lib] !== false)
56+
alias[lib] = getPathToModule(lib, options[lib]);
5957
});
6058
if(Object.keys(alias).length > 0) {
6159
compiler.resolvers.normal.apply(

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"enhanced-resolve": "0.7.x",
1313
"clone": "0.1.x",
1414
"webpack-core": "0.3.x",
15-
"node-libs-browser": "0.1.x",
15+
"node-libs-browser": "0.2.x",
1616
"tapable": "0.1.x",
1717
"base64-encode": "1.0.x"
1818
},

‎test/Integration.test.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("Integration", function() {
88
it("should compile library1", function(done) {
99
webpack({
1010
entry: "library1",
11+
bail: true,
1112
context: path.join(__dirname, "browsertest"),
1213
output: {
1314
pathinfo: true,
@@ -33,6 +34,7 @@ describe("Integration", function() {
3334
publicPath: "js/",
3435
library: "library2"
3536
},
37+
bail: true,
3638
module: {
3739
postLoaders: [
3840
{

‎test/browsertest/lib/index.web.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ describe("main", function() {
9494
sum2.should.be.eql(2);
9595
done();
9696
});
97-
process.on("xyz", function() {
98-
sum2++;
99-
});
100-
process.emit("xyz");
97+
sum2++;
10198
test(global === window, "global === window");
10299
});
103100
});

0 commit comments

Comments
 (0)