1 | # Babel Configurations
|
2 | Standard configurations for [Babel](https://babeljs.io/).
|
3 |
|
4 | **Why?** — We use Babel to support latest JavaScript specifications in our
|
5 | projects, and to allow for some fancy features in our code. The standard Babel
|
6 | configurations ensure that the same code patterns are supported in all of our apps.
|
7 |
|
8 | Several Babel configurations are provided by this package:
|
9 |
|
10 | - [**config/babel/node-ssr**](#node-ssr) — Babel configuration for NodeJS
|
11 | compilation of JS and JSX modules (server-side ReactJS rendering);
|
12 |
|
13 | - [**config/babel/webpack**](#webpack) — Babel configuration for Webpack
|
14 | compilation of JS and JSX modules.
|
15 |
|
16 | Use them as presets in your `.babelrc`, e.g.
|
17 | ```json
|
18 | {
|
19 | "presets": ["topcoder-react-utils/config/babel/node-ssr"]
|
20 | }
|
21 | ```
|
22 |
|
23 | To set options provided by a preset:
|
24 | ```json
|
25 | {
|
26 | "presets": [
|
27 | ["topcoder-react-utils/config/babel/node-ssr", {
|
28 | "someOption": "optionValue"
|
29 | }]
|
30 | ]
|
31 | }
|
32 | ```
|
33 |
|
34 | As the presets are JS modules, you can also wrap them into your own JS code to
|
35 | create a special preset that customizes Babel configuration in any necessary
|
36 | way. If you feel that the modification you need is useful for any generic
|
37 | ReactJS app, feel free to open GitHub issue ticket to discuss an update of our
|
38 | default configurations.
|
39 |
|
40 | These presets may behave differently in different Babel environments. You can
|
41 | set the environment with `BABEL_ENV` environment variable, or with `forceEnv`
|
42 | option of the Webpack's `babel-loader`.
|
43 |
|
44 | ### Configuration Details
|
45 |
|
46 | - <a name="node-ssr">**`config/babel/node-ssr.json`**</a> — Babel
|
47 | configuration for NodeJS compilation of JS and JSX modules (server-side ReactJS
|
48 | rendering);
|
49 |
|
50 | - Presets: [env](https://www.npmjs.com/package/babel-preset-env),
|
51 | [react](https://www.npmjs.com/package/babel-preset-react),
|
52 | [stage-2](https://www.npmjs.com/package/babel-preset-stage-2);
|
53 |
|
54 | - Uses [`babel-plugin-css-modules-transform`](https://www.npmjs.com/package/babel-plugin-css-modules-transform)
|
55 | to convertCSS and SCSS imports into objects with keys and values being the
|
56 | original and transformed class names. The transformed class names will match
|
57 | the class names generated by the `babel-plugin-react-css-modules` plugin,
|
58 | mentioned below;
|
59 |
|
60 | - Uses [`babel-plugin-dynamic-import-node`](https://www.npmjs.com/package/babel-plugin-dynamic-import-node)
|
61 | to support dynamic `import(..)` statemens;
|
62 |
|
63 | - Uses [`babel-plugin-inline-react-svg`](https://www.npmjs.com/package/babel-plugin-inline-react-svg)
|
64 | to embed imported SVG files into the complied JS modules;
|
65 |
|
66 | - Uses [`babel-plugin-module-resolver`](https://www.npmjs.com/package/babel-plugin-module-resolver)
|
67 | to setup the following paths as the roots for resolution of relative imports:
|
68 | `./src/shared`, `./src`. It will also apply path resolution to the first
|
69 | argument of `resolveWeak(..)` functions encountered in the compiled code;
|
70 |
|
71 | - Uses [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules)
|
72 | to transform `styleName` props of ReactJS components into globally unique
|
73 | `className` props. Generated class names are verbose in *development* and
|
74 | *test* Babel environments, for the ease of debugging; and they are short
|
75 | 6-symbols hashes in the *production* Babel environment, for compactness of the
|
76 | compiled JS and CSS modules;
|
77 |
|
78 | - Uses [`babel-plugin-tranform-assets`](https://www.npmjs.com/package/babel-plugin-transform-assets)
|
79 | to convert GIF, JPEG, JPG, and PNG imports into
|
80 | `/images/[FILE_HASH].[FILE_EXTENSION]` URL strings. Use the
|
81 | **`baseAssetsOutputPath`** preset option to prefix generated paths with an
|
82 | arbitrary path;
|
83 |
|
84 | - Uses [`babel-plugin-transform-runtime`](https://www.npmjs.com/package/babel-plugin-transform-runtime)
|
85 | to optimize complied JS modules (it externalizes references to helpers and
|
86 | builtins, automatically polyfilling the code without polluting globals).
|
87 |
|
88 | - <a name="webpack">**`config/babel/webpack`**</a> — Babel configuration
|
89 | for Webpack compilation of JS and JSX modules.
|
90 |
|
91 | - Presets: [env](https://www.npmjs.com/package/babel-preset-env),
|
92 | [react](https://www.npmjs.com/package/babel-preset-react),
|
93 | [stage-2](https://www.npmjs.com/package/babel-preset-stage-2);
|
94 |
|
95 | - Uses [`babel-plugin-inline-react-svg`](https://www.npmjs.com/package/babel-plugin-inline-react-svg)
|
96 | to embed imported SVG files into the complied JS modules;
|
97 |
|
98 | - Uses [`babel-plugin-module-resolver`](https://www.npmjs.com/package/babel-plugin-module-resolver)
|
99 | to setup the following paths as the roots for resolution of relative imports:
|
100 | `./src/shared`, `./src`;
|
101 |
|
102 | - Uses [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules)
|
103 | to transform `styleName` props of ReactJS components into globally unique
|
104 | `className` props. Generated class names are verbose in *development* and
|
105 | *test* Babel environments, for the ease of debugging; and they are short
|
106 | 6-symbols hashes in the *production* Babel environment, for compactness of the
|
107 | compiled JS and CSS modules;
|
108 |
|
109 | - Uses [`react-hot-loader/babel`](https://www.npmjs.com/package/react-hot-loader)
|
110 | to support hot module reloading in the *development* Babel environment;
|
111 |
|
112 | - Uses [`babel-plugin-transform-runtime`](https://www.npmjs.com/package/babel-plugin-transform-runtime)
|
113 | to optimize complied JS modules (it externalizes references to helpers and
|
114 | builtins, automatically polyfilling the code without polluting globals).
|