A Vite plugin that automatically converts Day.js CommonJS imports to ESM format for better tree-shaking and build optimization.
- 🚀 Auto Transform: Automatically redirects
dayjsimports todayjs/esm - 🔌 Plugin Support: Supports multiple import styles for all Day.js plugins
- 🌍 Locale Support: Supports Day.js locale packages
- 📦 Smaller Bundle: Leverage ESM tree-shaking capabilities
- 💪 TypeScript: Full TypeScript support
- ⚡ Zero Config: Works out of the box, no configuration needed
# npm
npm install vite-plugin-dayjs -D
# yarn
yarn add vite-plugin-dayjs -D
# pnpm
pnpm add vite-plugin-dayjs -D
Add the plugin to your vite.config.ts:
import { defineConfig } from 'vite'
import vitePluginDayjs from 'vite-plugin-dayjs'
export default defineConfig({
plugins: [
vitePluginDayjs(),
],
})Then use Day.js as usual:
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')
console.log(dayjs().format('YYYY-MM-DD'))
console.log(dayjs().subtract(1, 'day').fromNow())The plugin automatically transforms the following imports:
// Original import
import dayjs from 'dayjs'
// Transformed to
import dayjs from 'dayjs/esm'Supports multiple import formats:
// Format 2: With .js extension
import advancedFormat from 'dayjs/plugin/advancedFormat.js'
// Transforms to: dayjs/esm/plugin/relativeTime
// Format 1: Without extension
import relativeTime from 'dayjs/plugin/relativeTime'
// Transforms to: dayjs/esm/plugin/advancedFormat/index.jsSupports multiple import formats:
// Format 1: Without extension
import 'dayjs/locale/zh-cn'
// Transforms to: dayjs/esm/locale/zh-cn
// Format 2: With .js extension
import 'dayjs/locale/es.js'
// Transforms to: dayjs/esm/locale/es.jsAll official Day.js plugins are supported, including but not limited to:
advancedFormat- Advanced formattingcustomParseFormat- Custom parsing formatduration- DurationisBetween- Check if a date is between two datesisSameOrAfter/isSameOrBefore- Date comparisonrelativeTime- Relative timetimezone- Timezone supportutc- UTC time- And more...
All official Day.js locales are supported, including but not limited to:
zh-cn- Simplified Chinesezh-tw- Traditional Chineseen- Englishes- Spanishfr- Frenchja- Japaneseko- Korean- And more...
Zero configuration required. The plugin works out of the box.
Day.js uses CommonJS format by default, which causes issues in modern build tools:
- No tree-shaking: Unused code will be included in the bundle
- Larger bundle size: Contains unnecessary dependencies
- Poor loading performance: CommonJS modules are less efficient
Using ESM format provides:
- ✅ Better tree-shaking support
- ✅ Smaller bundle size
- ✅ Faster loading speed
- ✅ Better code splitting
See tests/dayjs.test.ts for more usage examples.
# Run tests
pnpm test
# Development mode
pnpm dev
# Build
pnpm buildIssues and Pull Requests are welcome!