Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # webpack
  2. ## Removing unused languages from dynamic import
  3. If locale is required dynamically all languages in the date-fns are loaded by webpack into bundle (~160kb) or split across the chunks. This prolongs the build process and increases the amount of space taken. However, it is possible to use webpack to trim down languages using [ContextReplacementPlugin].
  4. Let's assume that we have a single point in which supported locales are present:
  5. `config.js`:
  6. ```js
  7. export const supportedLocales = ['en', 'de', 'pl', 'it']
  8. ```
  9. We could also have a function that formats the date:
  10. ```js
  11. const getLocale = locale => require(`date-fns/locale/${locale}/index.js`)
  12. const formatDate = (date, formatStyle, locale) => {
  13. return format(date, formatStyle, {
  14. locale: getLocale(locale)
  15. })
  16. }
  17. ```
  18. In order to exclude unused languages we can use webpacks [ContextReplacementPlugin].
  19. `webpack.config.js`:
  20. ```js
  21. import webpack from 'webpack'
  22. import { supportedLocales } from './config.js'
  23. export default const config = {
  24. plugins: [
  25. new webpack.ContextReplacementPlugin(
  26. /date\-fns[\/\\]/,
  27. new RegExp(`[/\\\\\](${supportedLocales.join('|')})[/\\\\\]`)
  28. )
  29. ]
  30. }
  31. ```
  32. This results in a language bundle of ~23kb .
  33. [ContextReplacementPlugin]: https://webpack.js.org/plugins/context-replacement-plugin/