webpack/lib/APIPlugin.js

50 lines
1.6 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 ConstDependency = require("./dependencies/ConstDependency");
2013-07-11 05:20:07 +08:00
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
2013-01-31 01:49:25 +08:00
var NullFactory = require("./NullFactory");
2013-01-31 01:49:25 +08:00
function APIPlugin() {
}
module.exports = APIPlugin;
var REPLACEMENTS = {
__webpack_public_path__: "__webpack_require__.p",
__webpack_modules__: "__webpack_require__.m",
__webpack_chunk_load__: "__webpack_require__.e",
2014-03-10 20:40:20 +08:00
__non_webpack_require__: "require",
"require.onError": "__webpack_require__.onError",
2013-01-31 01:49:25 +08:00
};
2013-07-11 05:20:07 +08:00
var REPLACEMENT_TYPES = {
__webpack_public_path__: "string",
__webpack_require__: "function",
__webpack_modules__: "object",
__webpack_chunk_load__: "function",
};
2013-01-31 01:49:25 +08:00
var IGNORES = [
];
APIPlugin.prototype.apply = function(compiler) {
compiler.plugin("compilation", function(compilation, params) {
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
});
2013-01-31 01:49:25 +08:00
Object.keys(REPLACEMENTS).forEach(function(key) {
compiler.parser.plugin("expression "+key, function(expr) {
var dep = new ConstDependency(REPLACEMENTS[key], expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
});
2013-07-11 05:20:07 +08:00
compiler.parser.plugin("evaluate typeof "+key, function(expr) {
return new BasicEvaluatedExpression().setString(REPLACEMENT_TYPES[key]).setRange(expr.range);
});
2013-01-31 01:49:25 +08:00
});
IGNORES.forEach(function(key) {
compiler.parser.plugin(key, function(expr) {
return true;
});
});
};