webpack/lib/node/NodeSourcePlugin.js

53 lines
1.8 KiB
JavaScript
Raw Normal View History

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");
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) {
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) {
this.state.current.addVariable("global", "this");
return true;
2013-01-31 01:49:25 +08:00
});
compiler.parser.plugin("expression __filename", function(expr) {
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) {
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) {
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;
}, {});
}()))
);
};