2013-02-14 00:00:07 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var path = require("path");
|
2017-01-09 02:11:26 +08:00
|
|
|
var ParserHelpers = require("./ParserHelpers");
|
2013-02-14 00:00:07 +08:00
|
|
|
var ConstDependency = require("./dependencies/ConstDependency");
|
2013-02-17 05:23:22 +08:00
|
|
|
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
|
2013-02-14 00:00:07 +08:00
|
|
|
|
2014-06-16 21:18:49 +08:00
|
|
|
var NullFactory = require("./NullFactory");
|
|
|
|
|
2013-12-18 06:21:49 +08:00
|
|
|
function NodeStuffPlugin(options) {
|
2013-02-26 20:31:05 +08:00
|
|
|
this.options = options;
|
2013-02-14 00:00:07 +08:00
|
|
|
}
|
|
|
|
module.exports = NodeStuffPlugin;
|
|
|
|
NodeStuffPlugin.prototype.apply = function(compiler) {
|
2016-09-14 18:04:42 +08:00
|
|
|
var options = this.options;
|
|
|
|
compiler.plugin("compilation", function(compilation, params) {
|
2014-06-16 21:18:49 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2016-09-14 18:04:42 +08:00
|
|
|
params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2016-09-14 18:04:42 +08:00
|
|
|
if(parserOptions.node === false)
|
|
|
|
return;
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2016-09-14 18:04:42 +08:00
|
|
|
var localOptions = options;
|
|
|
|
if(parserOptions.node)
|
2016-12-29 03:07:28 +08:00
|
|
|
localOptions = Object.assign({}, localOptions, parserOptions.node);
|
2016-09-14 18:04:42 +08:00
|
|
|
|
|
|
|
function ignore() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setConstant(expressionName, value) {
|
|
|
|
parser.plugin("expression " + expressionName, function() {
|
|
|
|
this.state.current.addVariable(expressionName, JSON.stringify(value));
|
|
|
|
return true;
|
|
|
|
});
|
2015-11-21 01:10:39 +08:00
|
|
|
}
|
2016-09-14 18:04:42 +08:00
|
|
|
|
|
|
|
function setModuleConstant(expressionName, fn) {
|
|
|
|
parser.plugin("expression " + expressionName, function() {
|
|
|
|
this.state.current.addVariable(expressionName, JSON.stringify(fn(this.state.module)));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var context = compiler.context;
|
|
|
|
if(localOptions.__filename === "mock") {
|
|
|
|
setConstant("__filename", "/index.js");
|
|
|
|
} else if(localOptions.__filename) {
|
|
|
|
setModuleConstant("__filename", function(module) {
|
|
|
|
return path.relative(context, module.resource);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
parser.plugin("evaluate Identifier __filename", function(expr) {
|
|
|
|
if(!this.state.module) return;
|
|
|
|
var res = new BasicEvaluatedExpression();
|
|
|
|
var resource = this.state.module.resource;
|
|
|
|
var i = resource.indexOf("?");
|
|
|
|
res.setString(i < 0 ? resource : resource.substr(0, i));
|
|
|
|
res.setRange(expr.range);
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
if(localOptions.__dirname === "mock") {
|
|
|
|
setConstant("__dirname", "/");
|
|
|
|
} else if(localOptions.__dirname) {
|
|
|
|
setModuleConstant("__dirname", function(module) {
|
|
|
|
return path.relative(context, module.context);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
parser.plugin("evaluate Identifier __dirname", function(expr) {
|
|
|
|
if(!this.state.module) return;
|
|
|
|
var res = new BasicEvaluatedExpression();
|
|
|
|
res.setString(this.state.module.context);
|
|
|
|
res.setRange(expr.range);
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
parser.plugin("expression require.main", function(expr) {
|
|
|
|
var dep = new ConstDependency("__webpack_require__.c[__webpack_require__.s]", expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
2017-01-09 02:11:26 +08:00
|
|
|
parser.plugin(
|
|
|
|
"expression require.extensions",
|
|
|
|
ParserHelpers.expressionIsUnsupported("require.extensions is not supported by webpack. Use a loader instead.")
|
|
|
|
);
|
2016-09-14 18:04:42 +08:00
|
|
|
parser.plugin("expression module.loaded", function(expr) {
|
|
|
|
var dep = new ConstDependency("module.l", expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.plugin("expression module.id", function(expr) {
|
|
|
|
var dep = new ConstDependency("module.i", expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.plugin("expression module.exports", ignore);
|
|
|
|
parser.plugin("evaluate Identifier module.hot", function(expr) {
|
|
|
|
return new BasicEvaluatedExpression().setBoolean(false).setRange(expr.range);
|
|
|
|
});
|
|
|
|
parser.plugin("expression module", function() {
|
|
|
|
var moduleJsPath = path.join(__dirname, "..", "buildin", "module.js");
|
|
|
|
if(this.state.module.context) {
|
|
|
|
moduleJsPath = path.relative(this.state.module.context, moduleJsPath);
|
|
|
|
if(!/^[A-Z]:/i.test(moduleJsPath)) {
|
|
|
|
moduleJsPath = "./" + moduleJsPath.replace(/\\/g, "/");
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 02:11:26 +08:00
|
|
|
return ParserHelpers.addParsedVariableToModule(this, "module", "require(" + JSON.stringify(moduleJsPath) + ")(module)");
|
2016-09-14 18:04:42 +08:00
|
|
|
});
|
|
|
|
});
|
2013-02-14 00:00:07 +08:00
|
|
|
});
|
2015-07-08 20:39:02 +08:00
|
|
|
};
|