babel-plugin-global-define
Version:
The GlobalDefine plugin allows you to create global constants which is similar to Webpack's DefinePlugin.
50 lines (42 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (_ref) {
var t = _ref.types;
return {
visitor: {
MemberExpression: function MemberExpression(path, _ref2) {
var params = _ref2.opts;
Object.keys(params).forEach(function (key) {
if (memberExpressionMatcher(path, key)) {
replacer(path, params[key], t.valueToNode);
}
});
},
Identifier: function Identifier(path, _ref3) {
var params = _ref3.opts;
Object.keys(params).forEach(function (key) {
if (identifierMatcher(path, key)) {
replacer(path, params[key], t.valueToNode);
}
});
}
}
};
};
var memberExpressionMatcher = function memberExpressionMatcher(path, key) {
return path.matchesPattern(key);
};
var identifierMatcher = function identifierMatcher(path, key) {
return path.node.name === key;
};
var replacer = function replacer(path, value, valueToNode) {
path.replaceWith(valueToNode(value));
if (path.parentPath.isBinaryExpression()) {
var result = path.parentPath.evaluate();
if (result.confident) {
path.parentPath.replaceWith(valueToNode(result.value));
}
}
};