| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-01-02 07:21:54 +08:00
										 |  |  | var AliasPlugin = require("enhanced-resolve/lib/AliasPlugin"); | 
					
						
							| 
									
										
										
										
											2013-02-05 15:44:37 +08:00
										 |  |  | var ModuleParserHelpers = require("../ModuleParserHelpers"); | 
					
						
							| 
									
										
										
										
											2013-02-25 18:34:33 +08:00
										 |  |  | var nodeLibsBrowser = require("node-libs-browser"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | function NodeSourcePlugin(options) { | 
					
						
							|  |  |  | 	this.options = options; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = NodeSourcePlugin; | 
					
						
							|  |  |  | NodeSourcePlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 	function getPathToModule(module, type) { | 
					
						
							|  |  |  | 		if(type === true || (type === undefined && nodeLibsBrowser[module])) { | 
					
						
							|  |  |  | 			if(!nodeLibsBrowser[module]) throw new Error("No browser version for node.js core module '" + module + "' available"); | 
					
						
							|  |  |  | 			return nodeLibsBrowser[module]; | 
					
						
							|  |  |  | 		} else if(type === "mock") { | 
					
						
							|  |  |  | 			return require.resolve("node-libs-browser/mock/" + module); | 
					
						
							| 
									
										
										
										
											2014-08-21 20:43:34 +08:00
										 |  |  | 		} else if(type === "empty") { | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 			return require.resolve("node-libs-browser/mock/empty"); | 
					
						
							|  |  |  | 		} else return module; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(this.options.process) { | 
					
						
							|  |  |  | 		var processType = this.options.process; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression process", function() { | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 			return ModuleParserHelpers.addParsedVariable(this, "process", "require(" + JSON.stringify(getPathToModule("process", processType)) + ")"); | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(this.options.global) { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression global", function() { | 
					
						
							| 
									
										
										
										
											2013-10-01 16:17:04 +08:00
										 |  |  | 			this.state.module.addVariable("global", "(function() { return this; }())"); | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 	if(this.options.console) { | 
					
						
							|  |  |  | 		var consoleType = this.options.console; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression console", function() { | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 			return ModuleParserHelpers.addParsedVariable(this, "console", "require(" + JSON.stringify(getPathToModule("console", consoleType)) + ")"); | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 	var bufferType = this.options.Buffer; | 
					
						
							| 
									
										
										
										
											2015-01-22 03:25:25 +08:00
										 |  |  | 	if(bufferType) { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression Buffer", function() { | 
					
						
							| 
									
										
										
										
											2014-03-19 05:34:35 +08:00
										 |  |  | 			return ModuleParserHelpers.addParsedVariable(this, "Buffer", "require(" + JSON.stringify(getPathToModule("buffer", bufferType)) + ").Buffer"); | 
					
						
							| 
									
										
										
										
											2013-03-01 21:59:38 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-07 01:20:45 +08:00
										 |  |  | 	if(this.options.setImmediate) { | 
					
						
							|  |  |  | 		var setImmediateType = this.options.setImmediate; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression setImmediate", function() { | 
					
						
							| 
									
										
										
										
											2015-01-07 01:20:45 +08:00
										 |  |  | 			return ModuleParserHelpers.addParsedVariable(this, "setImmediate", "require(" + JSON.stringify(getPathToModule("timers", setImmediateType)) + ").setImmediate"); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.parser.plugin("expression clearImmediate", function() { | 
					
						
							| 
									
										
										
										
											2015-01-07 01:20:45 +08:00
										 |  |  | 			return ModuleParserHelpers.addParsedVariable(this, "clearImmediate", "require(" + JSON.stringify(getPathToModule("timers", setImmediateType)) + ").clearImmediate"); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 	var options = this.options; | 
					
						
							| 
									
										
										
										
											2013-02-25 18:34:33 +08:00
										 |  |  | 	compiler.plugin("after-resolvers", function(compiler) { | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 		var alias = {}; | 
					
						
							|  |  |  | 		Object.keys(nodeLibsBrowser).forEach(function(lib) { | 
					
						
							| 
									
										
										
										
											2016-01-02 07:21:54 +08:00
										 |  |  | 			if(options[lib] !== false) { | 
					
						
							|  |  |  | 				compiler.resolvers.normal.apply( | 
					
						
							|  |  |  | 					new AliasPlugin("described-resolve", { | 
					
						
							|  |  |  | 						name: lib, | 
					
						
							|  |  |  | 						onlyModule: true, | 
					
						
							|  |  |  | 						alias: getPathToModule(lib, options[lib]) | 
					
						
							|  |  |  | 					}, "resolve") | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-26 20:31:05 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-02-25 18:34:33 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-22 03:25:25 +08:00
										 |  |  | }; |