2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var path = require("path");
|
|
|
|
var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
|
2013-02-05 15:31:46 +08:00
|
|
|
var ModuleParserHelpers = require("./ModuleParserHelpers");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
function NodeSourcePlugin() {
|
|
|
|
}
|
|
|
|
module.exports = NodeSourcePlugin;
|
|
|
|
NodeSourcePlugin.prototype.apply = function(compiler) {
|
|
|
|
function ignore() { return true; }
|
|
|
|
compiler.parser.plugin("expression process", function(expr) {
|
2013-02-05 15:31:46 +08:00
|
|
|
return ModuleParserHelpers.addParsedVariable(this, "process", "require(" + JSON.stringify(path.join(__dirname, "..", "..", "buildin", "process.js")) + ")");
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression global", function(expr) {
|
2013-02-05 15:31:46 +08:00
|
|
|
this.state.current.addVariable("global", "this");
|
|
|
|
return true;
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression __filename", function(expr) {
|
2013-02-05 15:33:32 +08:00
|
|
|
this.state.current.addVariable("__filename", JSON.stringify("/index.js"));
|
|
|
|
return true;
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression __dirname", function(expr) {
|
2013-02-05 15:33:32 +08:00
|
|
|
this.state.current.addVariable("__dirname", JSON.stringify("/"));
|
|
|
|
return true;
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression module.exports", ignore);
|
|
|
|
compiler.parser.plugin("expression module.loaded", ignore);
|
|
|
|
compiler.parser.plugin("expression module.id", ignore);
|
|
|
|
compiler.parser.plugin("expression module", function(expr) {
|
2013-02-05 15:31:46 +08:00
|
|
|
return ModuleParserHelpers.addParsedVariable(this, "module", "require(" + JSON.stringify(path.join(__dirname, "..", "..", "buildin", "module.js")) + ")(module)");
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
compiler.resolvers.normal.apply(
|
|
|
|
new ModuleAliasPlugin((function() {
|
|
|
|
return [
|
|
|
|
"assert",
|
|
|
|
"buffer",
|
|
|
|
"child_process",
|
|
|
|
"events",
|
|
|
|
"path",
|
|
|
|
"punycode",
|
|
|
|
"querystring",
|
|
|
|
"url",
|
|
|
|
"util"
|
|
|
|
].reduce(function(o, v) {
|
|
|
|
o[v] = path.join(__dirname, "..", "..", "buildin", "web_modules", v + ".js")
|
|
|
|
return o;
|
|
|
|
}, {});
|
|
|
|
}()))
|
|
|
|
);
|
|
|
|
};
|