{"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,"search_vec":"'1.0.2':26 'address':61 'antdv':90 'antdv-next':89 'automat':7 'better':16 'build':76 'bundl':22,80 'cannot':69 'cjs':67 'common':63 'commonj':10 'config':47 'convers':48 'convert':8 'counterpart':58 'current':24 'day.js':9,34,66 'dayj':3,50 'dayjs/esm':59 'dayjs/locale':53 'dayjs/plugin':51 'ecosystem':92 'esm':13,57 'footgun':64 'format':14 'import':11,40,54 'javascript':93 'larger':79 'lead':77 'local':37 'maintain':84 'manual':39 'modul':68 'next':91 'part':86 'plugin':2,5,35,43,82 'provid':44 'rewrit':41 'shake':19 'shaken':73 'ship':27 'size':23 'smaller':21 'tree':18,72 'tree-shak':17 'tree-shaken':71 'type':29 'typescript':28,94 'unlik':38 'version':25 'vite':1,4,75 'work':31 'zero':46 'zero-config':45","created_at":"2026-04-27T17:06:12.996419+00:00","updated_at":"2026-04-27T17:06:12.996419+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/vite-plugin-dayjs","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-18","next_check":"2026-07-26","install_tag":null}}