Skip to content

Build your personal knowledge base with Trilium Notes

perf(client): stop bundling every dayjs plugin twice

Elian Doran1feb20edba79
diff --git a/packages/commons/src/lib/dayjs.ts b/packages/commons/src/lib/dayjs.ts
index 09fac0f5ee..4d1afbc94b 100644
--- a/packages/commons/src/lib/dayjs.ts
+++ b/packages/commons/src/lib/dayjs.ts
@@ -1,14 +1,10 @@
import { default as dayjs, type Dayjs } from "dayjs";
-import "dayjs/plugin/advancedFormat";
-import "dayjs/plugin/duration";
-import "dayjs/plugin/isBetween";
-import "dayjs/plugin/isoWeek";
-import "dayjs/plugin/isSameOrAfter";
-import "dayjs/plugin/isSameOrBefore";
-import "dayjs/plugin/quarterOfYear";
-import "dayjs/plugin/relativeTime";
-import "dayjs/plugin/utc";
+// Loads the dayjs plugin type augmentations without putting their bare import specifiers in the
+// runtime module graph (see dayjs_augmentations.ts for the why). This must be a type-only
+// re-export rather than a type-only import, so that it survives declaration emit and the
+// augmentations also reach consumers of the compiled .d.ts files.
+export type * from "./dayjs_augmentations.js";
//#region Plugins
import advancedFormat from "dayjs/plugin/advancedFormat.js";
diff --git a/packages/commons/src/lib/dayjs_augmentations.ts b/packages/commons/src/lib/dayjs_augmentations.ts
new file mode 100644
index 0000000000..a81d18637e
--- /dev/null
+++ b/packages/commons/src/lib/dayjs_augmentations.ts
@@ -0,0 +1,19 @@
+/*
+ * dayjs declares the plugin typings (and the Dayjs interface augmentations) under the suffix-less
+ * ambient module names, which bundler-resolution consumers (e.g. the client) can only load through
+ * these bare specifiers. The runtime imports in dayjs.ts must use the .js suffix instead, to
+ * satisfy nodenext module resolution.
+ *
+ * This module exists so that the bare specifiers stay out of the runtime module graph: dayjs.ts
+ * pulls it in with a type-only import, which is erased from the emitted JavaScript. Importing it
+ * as a regular module would make Vite bundle every plugin twice (once per specifier spelling).
+ */
+import "dayjs/plugin/advancedFormat";
+import "dayjs/plugin/duration";
+import "dayjs/plugin/isBetween";
+import "dayjs/plugin/isoWeek";
+import "dayjs/plugin/isSameOrAfter";
+import "dayjs/plugin/isSameOrBefore";
+import "dayjs/plugin/quarterOfYear";
+import "dayjs/plugin/relativeTime";
+import "dayjs/plugin/utc";