ms.macro
Version:
Convert various time formats to milliseconds at build time in Babel.
40 lines (32 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _babelPluginMacros = require('babel-plugin-macros');
var _ms = require('ms');
var _ms2 = _interopRequireDefault(_ms);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const getValue = path => {
if (path.type === 'CallExpression') {
return path.node.arguments[0].value;
}
if (path.type === 'TaggedTemplateExpression') {
return path.node.quasi.quasis[0].value.cooked;
}
return null;
};
exports.default = (0, _babelPluginMacros.createMacro)(({ babel: { types: t }, references: { default: paths } }) => {
paths.forEach(({ parentPath }) => {
const value = getValue(parentPath);
if (value) {
const newValue = (0, _ms2.default)(value);
if (newValue) {
parentPath.replaceWith(t.numericLiteral(newValue));
} else {
const line = parentPath.node.loc.start.line;
throw new _babelPluginMacros.MacroError(`Invalid input given to ms.macro at line ${line}`);
}
}
});
});
module.exports = exports['default'];