A Babel plugin that transforms cherry-picked imports from date-fns (e.g., import { format } from 'date-fns') into direct per-function imports (e.g., import format from 'date-fns/format'), reducing bundle size. Current stable version is 2.0.0, which supports date-fns v2 only; v1.x exists for date-fns v1. Release cadence is low, with major versions tied to date-fns major releases. Compared to manual per-function imports, this plugin automates the transformation with zero config. It is heavily inspired by babel-plugin-lodash and babel-plugin-recharts.
npm install babel-plugin-date-fnsVerified import paths — ran on the pinned version, not inferred.
Shows .babelrc config and how the plugin transforms named imports from date-fns to per-function imports.
For date-fns v1, use babel-plugin-date-fns@1.x (e.g., npm install babel-plugin-date-fns@1.0.0 --save-dev).
Use ES imports exclusively to benefit from the plugin. For CJS, manually import from subpaths (e.g., const format = require('date-fns/format')).Use static imports for automatic transformation. For dynamic imports, manually import from subpaths.
Manually re-export from subpaths (e.g., export { default as format } from 'date-fns/format').Upgrade to Babel 7 or later. If using Babel 6, continue using babel-plugin-date-fns@1.x.
Install date-fns as a runtime dependency: npm install date-fns --save
Ensure you are using Babel 7+ and the plugin is specified as 'date-fns' (short name) in .babelrc or babel.config.js.
Install the plugin: npm install babel-plugin-date-fns --save-dev
Add @babel/preset-env to your Babel presets (or ensure you have a preset that handles ES modules).