Skip to content

Commit 3e74d93

Browse files
committed
Working setup for i18next + dayjs + vite
It's not perfect, but for the small files here, it's an acceptable compromise.
1 parent 5ab3ac7 commit 3e74d93

19 files changed

Lines changed: 36 additions & 81 deletions

dayjs-locale

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 0 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "tsc && vite build && cp -r node_modules/dayjs/esm/locale dist/dayjs-locale",
8+
"build": "tsc && vite build",
99
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1010
"preview": "vite preview"
1111
},
@@ -45,7 +45,6 @@
4545
"prettier": "^3.3.2",
4646
"typescript": "^5.2.2",
4747
"vite": "^5.2.0",
48-
"vite-plugin-dynamic-import": "^1.5.0",
4948
"vite-plugin-i18next-loader": "^2.0.12"
5049
}
5150
}

src/config/dayjs.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import dayjs from 'dayjs';
2-
import advancedFormat from 'dayjs/plugin/advancedFormat';
3-
import customParseFormat from 'dayjs/plugin/customParseFormat';
4-
import isoWeek from 'dayjs/plugin/isoWeek';
5-
import localeData from 'dayjs/plugin/localeData';
6-
import objectSupport from 'dayjs/plugin/objectSupport';
7-
import updateLocale from 'dayjs/plugin/updateLocale';
8-
import utc from 'dayjs/plugin/utc';
9-
import weekday from 'dayjs/plugin/weekday';
1+
import dayjs from 'dayjs/esm';
2+
import advancedFormat from 'dayjs/esm/plugin/advancedFormat';
3+
import customParseFormat from 'dayjs/esm/plugin/customParseFormat';
4+
import isoWeek from 'dayjs/esm/plugin/isoWeek';
5+
import localeData from 'dayjs/esm/plugin/localeData';
6+
import objectSupport from 'dayjs/esm/plugin/objectSupport';
7+
import updateLocale from 'dayjs/esm/plugin/updateLocale';
8+
import utc from 'dayjs/esm/plugin/utc';
9+
import weekday from 'dayjs/esm/plugin/weekday';
1010

1111
dayjs.extend( advancedFormat );
1212
dayjs.extend( customParseFormat );

src/config/i18n.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from 'dayjs';
1+
import dayjs from 'dayjs/esm';
22
import dayjsLocales from 'dayjs/locale.json';
33

44
export function i18nConfiguration( namespaces ) {
@@ -34,10 +34,12 @@ export function getPartiallySupportedLocales() {
3434
.sort( ( languageA, languageB ) => languageA.name.localeCompare( languageB.name ) );
3535
}
3636

37+
const DAYJS_LOCALES = import.meta.glob( '../../node_modules/dayjs/esm/locale/*.js', { eager: true } );
38+
3739
export function handleLanguageChange( newLanguage, firstDayOfWeek = 1 ) {
38-
// Silence the warning until https://github.com/vitejs/vite/issues/14102 is fixed
39-
// Until then, we copy these files to /dist during build
40-
import( /* @vite-ignore */ '../../dayjs-locale/' + newLanguage + '.js' );
40+
// Hacky workaround until https://github.com/vitejs/vite/issues/14102 is fixed
41+
const localeData = DAYJS_LOCALES[ `../../node_modules/dayjs/esm/locale/${newLanguage}.js` ];
42+
dayjs.locale( newLanguage, localeData.default, false );
4143
dayjs.locale( newLanguage );
4244
dayjs.updateLocale( newLanguage, {
4345
weekStart: firstDayOfWeek,

src/configuration-form/special-dates.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from 'dayjs';
1+
import dayjs from 'dayjs/esm';
22
import ICAL from 'ical.js';
33
import PropTypes from 'prop-types';
44
import React from 'react';

src/configuration.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { arrayMove } from '@dnd-kit/sortable';
2-
import dayjs from 'dayjs';
2+
import dayjs from 'dayjs/esm';
33
import { saveAs } from 'file-saver';
44
import i18n, { changeLanguage } from 'i18next';
55
import PropTypes from 'prop-types';

src/index.jsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import i18n from 'i18next';
22
import LanguageDetector from 'i18next-browser-languagedetector';
3-
import React, { Suspense } from 'react';
4-
import Spinner from 'react-bootstrap/Spinner';
3+
import React from 'react';
54
import { createRoot } from 'react-dom/client';
65
import { initReactI18next } from 'react-i18next';
76
import resources from 'virtual:i18next-loader';
@@ -12,34 +11,21 @@ import '~/config/dayjs';
1211
import { handleLanguageChange, i18nConfiguration } from '~/config/i18n';
1312
import Loader from '~/loader';
1413

14+
i18n.on( 'languageChanged', ( newLanguage ) => {
15+
handleLanguageChange( newLanguage );
16+
} );
17+
1518
i18n
1619
.use( LanguageDetector )
1720
.use( initReactI18next )
1821
.init(
1922
{ ...i18nConfiguration( [ 'app', 'pdf', 'config' ] ), resources },
2023
);
2124

22-
i18n.on( 'languageChanged', ( newLanguage ) => {
23-
handleLanguageChange( newLanguage );
24-
} );
25-
26-
const loadingComponent = (
27-
<Spinner
28-
className="position-absolute top-50 start-50"
29-
animation="border"
30-
variant="primary"
31-
role="status"
32-
>
33-
<span className="visually-hidden">Loading...</span>
34-
</Spinner>
35-
);
36-
3725
const container = document.getElementById( 'root' );
3826
const root = createRoot( container );
3927
root.render(
4028
<React.StrictMode>
41-
<Suspense fallback={ loadingComponent }>
42-
<Loader />
43-
</Suspense>
29+
<Loader />
4430
</React.StrictMode>,
4531
);

src/lib/config-compat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from 'dayjs';
1+
import dayjs from 'dayjs/esm';
22

33
import {
44
DATE_FORMAT as SPECIAL_DATES_DATE_FORMAT,

src/lib/date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from 'dayjs';
1+
import dayjs from 'dayjs/esm';
22

33
export function getWeekdays() {
44
const weekdaysFull = dayjs.weekdays();

0 commit comments

Comments
 (0)