Skip to content

Commit a36afba

Browse files
committed
fix(i18n): use .js extensions on dayjs subpath imports for valid Node ESM
dayjs ships no "exports" map, so Node's native ESM resolver requires a file extension on subpath imports. Because vite externalizes dayjs, the built dist/es/*.mjs preserved the extensionless specifiers verbatim, producing invalid ESM: bundlers (Turbopack/Webpack/Vite) resolve them fine, but Node's native loader throws ERR_MODULE_NOT_FOUND -- e.g. Next.js 16 loading the SDK as a server external during "Collecting page data". Add .js to all dayjs plugin and locale subpath imports (8 plugins + 12 locales) so the emitted ESM is valid under Node's native loader. Also fixes a stale JSDoc example and the registerTranslation log message.
1 parent ca5ed16 commit a36afba

4 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/context/TranslationContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useContext } from 'react';
22
import Dayjs from 'dayjs';
3-
import calendar from 'dayjs/plugin/calendar';
4-
import localizedFormat from 'dayjs/plugin/localizedFormat';
3+
import calendar from 'dayjs/plugin/calendar.js';
4+
import localizedFormat from 'dayjs/plugin/localizedFormat.js';
55
import type { PropsWithChildren } from 'react';
66
import type { TFunction } from 'i18next';
77
import type { TranslationLanguages } from 'stream-chat';

src/i18n/Streami18n.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import i18n from 'i18next';
22
import Dayjs from 'dayjs';
3-
import calendar from 'dayjs/plugin/calendar';
4-
import updateLocale from 'dayjs/plugin/updateLocale';
5-
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
6-
import localeData from 'dayjs/plugin/localeData';
7-
import relativeTime from 'dayjs/plugin/relativeTime';
8-
import duration from 'dayjs/plugin/duration';
9-
import utc from 'dayjs/plugin/utc';
10-
import timezone from 'dayjs/plugin/timezone';
3+
import calendar from 'dayjs/plugin/calendar.js';
4+
import updateLocale from 'dayjs/plugin/updateLocale.js';
5+
import LocalizedFormat from 'dayjs/plugin/localizedFormat.js';
6+
import localeData from 'dayjs/plugin/localeData.js';
7+
import relativeTime from 'dayjs/plugin/relativeTime.js';
8+
import duration from 'dayjs/plugin/duration.js';
9+
import utc from 'dayjs/plugin/utc.js';
10+
import timezone from 'dayjs/plugin/timezone.js';
1111
import { NotificationTranslationTopic, TranslationBuilder } from './TranslationBuilder';
1212
import { defaultTranslatorFunction, predefinedFormatters } from './utils';
1313

@@ -34,21 +34,21 @@ import {
3434
trTranslations,
3535
} from './translations';
3636

37-
import 'dayjs/locale/de';
38-
import 'dayjs/locale/es';
39-
import 'dayjs/locale/fr';
40-
import 'dayjs/locale/hi';
41-
import 'dayjs/locale/it';
42-
import 'dayjs/locale/ja';
43-
import 'dayjs/locale/ko';
44-
import 'dayjs/locale/nl';
45-
import 'dayjs/locale/pt';
46-
import 'dayjs/locale/ru';
47-
import 'dayjs/locale/tr';
37+
import 'dayjs/locale/de.js';
38+
import 'dayjs/locale/es.js';
39+
import 'dayjs/locale/fr.js';
40+
import 'dayjs/locale/hi.js';
41+
import 'dayjs/locale/it.js';
42+
import 'dayjs/locale/ja.js';
43+
import 'dayjs/locale/ko.js';
44+
import 'dayjs/locale/nl.js';
45+
import 'dayjs/locale/pt.js';
46+
import 'dayjs/locale/ru.js';
47+
import 'dayjs/locale/tr.js';
4848
// These locale imports also set these locale globally.
4949
// So As a last step I am going to import english locale
5050
// to make sure I don't mess up language at other places in app.
51-
import 'dayjs/locale/en';
51+
import 'dayjs/locale/en.js';
5252

5353
const defaultNS = 'translation';
5454
const defaultLng = 'en';
@@ -407,10 +407,8 @@ export type Streami18nOptions = {
407407
* ```js
408408
* import Dayjs from 'dayjs'
409409
*
410-
* import 'dayjs/locale/nl';
411-
* import 'dayjs/locale/it';
412-
* // or if you want to include all locales
413-
* import 'dayjs/min/locales';
410+
* import 'dayjs/locale/nl.js';
411+
* import 'dayjs/locale/it.js';
414412
*
415413
* const i18n = new Streami18n({
416414
* language: 'nl',
@@ -745,7 +743,7 @@ export class Streami18n {
745743
this.logger(
746744
`Streami18n: registerTranslation(language, translation, customDayjsLocale) - ` +
747745
`Locale config for ${language} does not exist in Dayjs.` +
748-
`Please import the locale file using "import 'dayjs/locale/${language}';" in your app or ` +
746+
`Please import the locale file using "import 'dayjs/locale/${language}.js';" in your app or ` +
749747
`register the locale config with Streami18n using registerTranslation(language, translation, customDayjsLocale)`,
750748
);
751749
}

src/i18n/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type TimestampFormatterOptions = {
5050

5151
/**
5252
* import dayjs from 'dayjs';
53-
* import duration from 'dayjs/plugin/duration';
53+
* import duration from 'dayjs/plugin/duration.js';
5454
*
5555
* dayjs.extend(duration);
5656
*

src/i18n/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Dayjs from 'dayjs';
2-
import type { Duration as DayjsDuration } from 'dayjs/plugin/duration';
2+
import type { Duration as DayjsDuration } from 'dayjs/plugin/duration.js';
33

44
import type { TFunction } from 'i18next';
55
import type { Moment } from 'moment-timezone';

0 commit comments

Comments
 (0)