Build-Time MS Converter Macro
ms.macro is a Babel macro designed to convert human-readable time strings (e.g., "1 day", "2 hours") into their corresponding millisecond values directly at build time. This package, currently at version 2.0.0, aims to eliminate runtime dependencies and shift potential conversion errors from runtime to build time. It is a stable package that typically sees updates driven by changes in Babel's macro API or the underlying `ms` package's parsing logic. Its primary differentiator is its build-time execution, which means that the original `ms` package does not need to be shipped with your production code for these specific conversions, resulting in smaller bundle sizes and improved reliability by catching invalid time strings during compilation rather than during execution. Unlike the original `ms` package, `ms.macro` exclusively supports the string-to-number conversion; it does not offer the reverse conversion from milliseconds to a human-readable string.
Common errors
-
Error: Invalid time string: '1 da'
cause An incorrectly formatted time string was passed to ms.macro, which is caught during the Babel build process.fixCorrect the time string to a valid format understood by the `ms` library (e.g., '1 day', '2h', '30m'). -
TypeError: ms is not a function
cause This error often occurs when `babel-plugin-macros` is not correctly configured, or `ms.macro` is being used in a CommonJS context without proper transpilation, causing the import to not resolve or the macro transformation to fail.fixVerify that `babel-plugin-macros` is installed and properly configured in your Babel setup. Ensure you are using `import ms from 'ms.macro';` in an environment where Babel is processing your files.
Warnings
- gotcha ms.macro only supports converting time strings to milliseconds. It does NOT support converting milliseconds back into human-readable strings, which is a feature of the original `ms` package. Attempting this will result in a build-time error or unexpected behavior.
- breaking This macro requires `babel-plugin-macros` to be correctly configured in your Babel setup. Without it, the `ms.macro` import will not be processed, leading to a runtime error or an unresolved import.
- gotcha The macro only supports the single string-argument signature. Passing non-string arguments or multiple arguments will result in a build-time error as the macro expects a parseable time string.
Install
-
npm install ms.macro -
yarn add ms.macro -
pnpm add ms.macro
Imports
- ms
const ms = require('ms.macro');import ms from 'ms.macro';
- ms (tagged template)
import ms from 'ms.macro'; const ONE_DAY = ms`1 day`;
- ms (aliased)
import { ms } from 'ms.macro';import msm from 'ms.macro'; import ms from 'ms'; const value = msm('1h');
Quickstart
import ms from 'ms.macro';
// Basic function call usage
const ONE_HOUR = ms('1 hour');
const TWO_DAYS = ms('2 days');
const THIRTY_MINUTES = ms('30m');
// Tagged template literal usage
const ONE_WEEK = ms`1 week`;
const HALF_DAY = ms`0.5d`;
console.log(`One hour in milliseconds: ${ONE_HOUR}`); // -> 3600000
console.log(`Two days in milliseconds: ${TWO_DAYS}`); // -> 172800000
console.log(`Thirty minutes in milliseconds: ${THIRTY_MINUTES}`); // -> 1800000
console.log(`One week in milliseconds: ${ONE_WEEK}`); // -> 604800000
console.log(`Half day in milliseconds: ${HALF_DAY}`); // -> 43200000