Path: blob/master/src/packages/frontend/app/dayjs-antd-plugins.ts
16567 views
/*1* This file is part of CoCalc: Copyright © 2026 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Register the dayjs plugins that antd's date/time components (DatePicker,7RangePicker, TimePicker, ...) rely on.89WHY THIS EXISTS10===============11antd's pickers are built on `rc-picker`, whose dayjs adapter12(rc-picker/es/generate/dayjs.js) calls `dayjs.extend(weekday)`,13`dayjs.extend(localeData)`, etc. on *its own* imported dayjs instance.1415In a production bundle there can be more than one dayjs module instance16(e.g. our app code resolves `[email protected]` while another dependency drags in17`[email protected]`, and ESM/CJS interop can further duplicate instances). When18that happens, the dayjs objects our app code creates with `import dayjs from19"dayjs"` and hands to a picker via its `value` prop come from an instance that20was NEVER extended by rc-picker. rc-picker then calls e.g.21`generateConfig.getWeekDay(value)` -> `value.weekday()` on our object and22throws:2324TypeError: t.weekday is not a function25at Object.getWeekDay ...2627In dev this is usually invisible because the bundler dedupes dayjs to a single28instance, so rc-picker's own `extend` calls happen to also cover our instance.29The bug only shows up in the minified production bundle.3031THE FIX32=======33Extend the same set of plugins rc-picker needs, on the dayjs instance our app34code uses. Importing this module for its side effects from each app entry point35(before any picker renders) guarantees that every dayjs object we pass to antd36has these plugin methods, regardless of how many dayjs instances the bundler37produced.3839Keep this list in sync with rc-picker/es/generate/dayjs.js.40*/4142import dayjs from "dayjs";43import advancedFormat from "dayjs/plugin/advancedFormat";44import customParseFormat from "dayjs/plugin/customParseFormat";45import localeData from "dayjs/plugin/localeData";46import weekday from "dayjs/plugin/weekday";47import weekOfYear from "dayjs/plugin/weekOfYear";48import weekYear from "dayjs/plugin/weekYear";4950// Register the plugins antd's pickers (via rc-picker) require on the value51// objects we pass in. Order matches rc-picker's own adapter for clarity.52dayjs.extend(customParseFormat);53dayjs.extend(advancedFormat);54dayjs.extend(weekday);55dayjs.extend(localeData);56dayjs.extend(weekOfYear);57dayjs.extend(weekYear);5859export default dayjs;606162