Skip to content

Commit

Permalink
Get rid of CommonJS dependency
Browse files Browse the repository at this point in the history
Use dynamic imports, even though Vite does not quite yet support this
for node_modules. See vitejs/vite#14102

Tried different approaches, including `import.meta.glob` to get all the
locale names first. But the static analysis still can't figure out all
the possible imports, so I get the dreaded warning.
  • Loading branch information
klimeryk committed Jun 21, 2024
1 parent 46f30f3 commit 0e55193
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 19 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "tsc && vite build && mkdir -p dist/node_modules/dayjs/esm && cp -r node_modules/dayjs/esm/locale dist/node_modules/dayjs/esm/",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
Expand Down Expand Up @@ -45,7 +45,6 @@
"prettier": "^3.3.2",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vite-plugin-commonjs": "^0.10.1",
"vite-plugin-dynamic-import": "^1.5.0",
"vite-plugin-i18next-loader": "^2.0.12"
}
Expand Down
4 changes: 3 additions & 1 deletion src/config/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function getPartiallySupportedLocales() {
}

export function handleLanguageChange( newLanguage, firstDayOfWeek = 1 ) {
require( 'dayjs/esm/locale/' + newLanguage + '.js' );
// Silence the warning until https://github.com/vitejs/vite/issues/14102 is fixed
// Until then, we copy these files to /dist during build
import( /* @vite-ignore */ '../../node_modules/dayjs/esm/locale/' + newLanguage + '.js' );
dayjs.locale( newLanguage );
dayjs.updateLocale( newLanguage, {
weekStart: firstDayOfWeek,
Expand Down
3 changes: 0 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from 'path';

import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import commonjs from 'vite-plugin-commonjs';
import i18nextLoader from 'vite-plugin-i18next-loader';

// https://vitejs.dev/config/
Expand All @@ -15,7 +14,6 @@ export default defineConfig( {
},
},
plugins: [
commonjs(),
i18nextLoader( { paths: [ './src/locales' ], namespaceResolution: 'basename' } ),
react(),
],
Expand All @@ -26,7 +24,6 @@ export default defineConfig( {
},
worker: {
plugins: () => [
commonjs(),
i18nextLoader( { paths: [ './src/locales' ], namespaceResolution: 'basename' } ),
react(),
],
Expand Down

0 comments on commit 0e55193

Please sign in to comment.