Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locales dynamic import #3774

Open
vadimpopa opened this issue Apr 18, 2024 · 4 comments
Open

Locales dynamic import #3774

vadimpopa opened this issue Apr 18, 2024 · 4 comments

Comments

@vadimpopa
Copy link
Contributor

vadimpopa commented Apr 18, 2024

Hi guys,

I'm upgrading to v3 and I'm struggling with adjusting the dynamic import that works in previous version as in https://github.com/date-fns/date-fns/blob/main/docs/webpack.md .The doc is not updated to v3.

Used to import it with:

const localePromise = import(
			/* webpackMode: "lazy", webpackChunkName: "df-[index]", webpackExclude: /_lib/ */
			`date-fns/locale/${lang}/index.js`
		);

Can't see yet how locales dynamic import would work in v3, has anyone succeded ?

Vadim.

@vadimpopa
Copy link
Contributor Author

Seems to be a webpack issue first webpack/webpack#13865

@vadimpopa
Copy link
Contributor Author

vadimpopa commented Apr 18, 2024

I think I've handled it:

	async loadDateTranslations() {
		const { lang } = this;
		const localePromise = import(
			/* webpackMode: "lazy", webpackChunkName: "df-[request]", webpackExclude: /_lib/ */
			`date-fns-locale/locale/${lang}.mjs`
		);

		return localePromise.then((locale) => {
                       console.log(locale.default);
		});
	},
const locales = ['en-US', 'en-GB'];

module.exports = {
	resolve: {
		alias: {
			'date-fns-locale': path.dirname(
				require.resolve('date-fns/package.json')
			),
		},
	},
       plugins: [
         new webpack.ContextReplacementPlugin(
			/date-fns[/\\]locale/,
			new RegExp(`(${locales.join('|')})\.mjs$`)
		),
       ]
}

@joaopedrodcf
Copy link

Also facing the same problem: https://github.com/orgs/date-fns/discussions/3663

@joaopedrodcf
Copy link

Hey @kossnocorp just tagging you here as maybe this issue is important for more people that use v2 currently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants