Skip to content

Commit

Permalink
Update webpack documentation (#3777)
Browse files Browse the repository at this point in the history
Co-authored-by: Sasha Koss <koss@nocorp.me>
  • Loading branch information
vadimpopa and kossnocorp committed May 13, 2024
1 parent 8345ba1 commit c13e2c6
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions docs/webpack.md
Original file line number Diff line number Diff line change
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

0 comments on commit c13e2c6

Please sign in to comment.