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

Fix webpack documentation #3777

Merged
merged 3 commits into from May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 16 additions & 11 deletions docs/webpack.md
Expand Up @@ -16,11 +16,11 @@ export const supportedLocales = ["en-US", "de", "pl", "it"];
We could also have a function that formats the date:

```js
const getLocale = (locale) => import(`date-fns/locale/${locale}/index.js`); // or require() if using CommonJS
const getLocale = (locale) => import(`date-fns-locale/locale/${locale}.mjs`); // or require() if using CommonJS

const formatDate = (date, formatStyle, locale) => {
return format(date, formatStyle, {
locale: getLocale(locale),
locale: getLocale(locale).default,
});
};
```
Expand All @@ -30,17 +30,22 @@ In order to exclude unused languages we can use webpacks [ContextReplacementPlug
`webpack.config.js`:

```js
import webpack from 'webpack'
import { supportedLocales } from './config.js'

export default const config = {
import webpack from "webpack";
import { supportedLocales } from "./config.js";

export default config = {
resolve: {
alias: {
"date-fns-locale": path.dirname(require.resolve("date-fns/package.json")),
},
},
plugins: [
new webpack.ContextReplacementPlugin(
/^date-fns[/\\]locale$/,
new RegExp(`\\.[/\\\\](${supportedLocales.join('|')})[/\\\\]index\\.js$`)
)
]
}
/date-fns[/\\]locale/,
new RegExp(`(${locales.join("|")})\.mjs$`),
),
],
};
```

This results in a language bundle of ~23kb .
Expand Down