Skip to content

Commit 65ff548

Browse files
authored
Ignore Moment.js locales by default (facebook#2187)
* Ignore Moment.js locales by default * Update README.md
1 parent 493e5a6 commit 65ff548

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

packages/react-scripts/config/webpack.config.dev.js

+6
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ module.exports = {
252252
// makes the discovery automatic so you don't have to restart.
253253
// See https://github.com/facebookincubator/create-react-app/issues/186
254254
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
255+
// Moment.js is an extremely popular library that bundles large locale files
256+
// by default due to how Webpack interprets its code. This is a practical
257+
// solution that requires the user to opt into importing specific locales.
258+
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
259+
// You can remove this if you don't use Moment.js:
260+
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
255261
],
256262
// Some libraries import Node modules but don't use them in the browser.
257263
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/config/webpack.config.prod.js

+6
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ module.exports = {
286286
new ManifestPlugin({
287287
fileName: 'asset-manifest.json',
288288
}),
289+
// Moment.js is an extremely popular library that bundles large locale files
290+
// by default due to how Webpack interprets its code. This is a practical
291+
// solution that requires the user to opt into importing specific locales.
292+
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
293+
// You can remove this if you don't use Moment.js:
294+
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
289295
],
290296
// Some libraries import Node modules but don't use them in the browser.
291297
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/template/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
8181
- [`npm test` hangs on macOS Sierra](#npm-test-hangs-on-macos-sierra)
8282
- [`npm run build` silently fails](#npm-run-build-silently-fails)
8383
- [`npm run build` fails on Heroku](#npm-run-build-fails-on-heroku)
84+
- [Moment.js locales are missing](#momentjs-locales-are-missing)
8485
- [Something Missing?](#something-missing)
8586

8687
## Updating to New Releases
@@ -1616,6 +1617,32 @@ It is reported that `npm run build` can fail on machines with no swap space, whi
16161617
This may be a problem with case sensitive filenames.
16171618
Please refer to [this section](#resolving-heroku-deployment-errors).
16181619
1620+
### Moment.js locales are missing
1621+
1622+
If you use a [Moment.js](https://momentjs.com/), you might notice that only the English locale is available by default. This is because the locale files are large, and you probably only need a subset of [all the locales provided by Moment.js](https://momentjs.com/#multiple-locale-support).
1623+
1624+
To add a specific Moment.js locale to your bundle, you need to import it explicitly.<br>
1625+
For example:
1626+
1627+
```js
1628+
import moment from 'moment';
1629+
import 'moment/locale/fr';
1630+
```
1631+
1632+
If import multiple locales this way, you can later switch between them by calling `moment.locale()` with the locale name:
1633+
1634+
```js
1635+
import moment from 'moment';
1636+
import 'moment/locale/fr';
1637+
import 'moment/locale/es';
1638+
1639+
// ...
1640+
1641+
moment.locale('fr');
1642+
```
1643+
1644+
This will only work for locales that have been explicitly imported before.
1645+
16191646
## Something Missing?
16201647

16211648
If you have ideas for more “How To” recipes that should be on this page, [let us know](https://github.com/facebookincubator/create-react-app/issues) or [contribute some!](https://github.com/facebookincubator/create-react-app/edit/master/packages/react-scripts/template/README.md)

0 commit comments

Comments
 (0)