{"id":22634,"library":"vite-plugin-dayjs","title":"Vite Plugin Dayjs","description":"Vite plugin that automatically converts Day.js CommonJS imports to ESM format for better tree-shaking and smaller bundle sizes. Current version 1.0.2 ships TypeScript types and works with all Day.js plugins and locales. Unlike manual import rewriting, this plugin provides zero-config conversion of dayjs, dayjs/plugin/*, and dayjs/locale/* imports to their ESM counterparts (dayjs/esm/...). It addresses the common footgun where Day.js CJS modules cannot be tree-shaken in Vite builds, leading to larger bundles. The plugin is maintained as part of the antdv-next ecosystem.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","typescript"],"install":[{"cmd":"npm install vite-plugin-dayjs","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-dayjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-dayjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a default export; named import does not exist.","wrong":"import { vitePluginDayjs } from 'vite-plugin-dayjs'","symbol":"vitePluginDayjs","correct":"import vitePluginDayjs from 'vite-plugin-dayjs'"},{"note":"The plugin converts 'dayjs' to 'dayjs/esm' automatically; manual import to 'dayjs/esm' defeats the plugin's purpose and may cause duplicate module instances.","wrong":"import dayjs from 'dayjs/esm'","symbol":"dayjs","correct":"import dayjs from 'dayjs'"},{"note":"Import plugins without the /esm prefix; the plugin handles the conversion. If you manually use /esm, tree-shaking may still work but you bypass the intended workflow.","wrong":"import relativeTime from 'dayjs/esm/plugin/relativeTime'","symbol":"plugin import","correct":"import relativeTime from 'dayjs/plugin/relativeTime'"},{"note":"Import locales without the /esm prefix; let the plugin rewrite them to ESM.","wrong":"import 'dayjs/esm/locale/zh-cn'","symbol":"locale import","correct":"import 'dayjs/locale/zh-cn'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite'\nimport vitePluginDayjs from 'vite-plugin-dayjs'\n\nexport default defineConfig({\n  plugins: [\n    vitePluginDayjs(),\n  ],\n})\n\n// Then in your source files:\nimport dayjs from 'dayjs'\nimport relativeTime from 'dayjs/plugin/relativeTime'\nimport 'dayjs/locale/zh-cn'\n\ndayjs.extend(relativeTime)\ndayjs.locale('zh-cn')\n\nconsole.log(dayjs().format('YYYY-MM-DD'))\nconsole.log(dayjs().subtract(1, 'day').fromNow())\n","lang":"typescript","description":"Configures vite-plugin-dayjs in vite.config.ts and demonstrates automatic ESM conversion of dayjs, plugin, and locale imports."},"warnings":[{"fix":"Use Vite as your build tool if you want to use this plugin.","message":"Plugin only works with Vite; not compatible with other bundlers like Webpack, Rollup, or esbuild standalone.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"For custom import handling, consider using vite-plugin-optimizer or patch-package with manual aliasing.","message":"Zero-config design means no customization: all dayjs imports are redirected to ESM. If you need selective conversion, this plugin may not fit.","severity":"deprecated","affected_versions":">=1.0"},{"fix":"Ensure all dayjs usage in your source code uses import statements; consider using optimizeDeps to pre-bundle dependent CJS packages.","message":"If dayjs is also imported from a dependency that uses CJS directly (not via import), those imports will not be converted.","severity":"gotcha","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Install dayjs: npm install dayjs. Ensure dayjs version is >=1.8 which includes ESM support.","cause":"The plugin redirects imports to dayjs/esm, but dayjs may not be installed or does not provide ESM exports in its package.json.","error":"Cannot find module 'dayjs/esm' or its corresponding type declarations."},{"fix":"Check available plugins at https://day.js.org/docs/en/plugin/plugin and use the correct name (e.g., 'relativeTime' not 'relativetime').","cause":"Importing a plugin that does not exist in dayjs or a typo in the plugin name.","error":"[vite] Internal server error: Failed to resolve import \"dayjs/esm/plugin/relativeTime\" from \"src/main.ts\". Does the file exist?"},{"fix":"Ensure the plugin is called: import vitePluginDayjs from 'vite-plugin-dayjs'; plugins: [vitePluginDayjs()].","cause":"The plugin was not properly invoked as a function: e.g., used as vitePluginDayjs instead of vitePluginDayjs().","error":"[vite] Internal server error: Plugin 'vite-plugin-dayjs' returned code when not called as a function."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}