|
1 | 1 | # Babel Configurations
|
2 | 2 | Standard configurations for [Babel](https://babeljs.io/).
|
3 | 3 |
|
4 |
| -**Why?** — We use Babel to support latest JavaScript specifications in our projects, and to allow for some fancy features in our code. The standard Babel configurations help to ensure that selected code patterns are supported in all of our apps. |
| 4 | +**Why?** — We use Babel to support latest JavaScript specifications in our projects, and to allow for some fancy features in our code. The standard Babel configurations ensure that the same code patterns are supported in all of our apps. |
5 | 5 |
|
6 | 6 | Several Babel configurations are provided by this package:
|
7 | 7 |
|
8 |
| -[**config/babel/webpack.json**](#webpack) — Babel config to use for JS and JSX files compilation during the Webpack build process; |
| 8 | +- [**config/babel/node-ssr**](#node-ssr) — Babel configuration for NodeJS compilation of JS and JSX modules (server-side ReactJS rendering); |
9 | 9 |
|
10 |
| -[**config/babel/node-ssr.json**](#node-ssr) — Server-side configuration with support of server-side rendering; |
| 10 | +- [**config/babel/webpack**](#webpack) — Babel configuration for Webpack compilation of JS and JSX modules. |
11 | 11 |
|
12 |
| -To use any of these configurations in your code use `extend` Babel option in your `.babelrc` file, or whenever you need it, e.g. |
| 12 | +Use them as presets in your `.babelrc`, e.g. |
13 | 13 | ```json
|
14 | 14 | {
|
15 |
| - "extend": "topcoder-react-utils/config/babel/node-ssr.json" |
| 15 | + "presets": ["topcoder-react-utils/config/babel/node-ssr"] |
16 | 16 | }
|
17 | 17 | ```
|
18 |
| -This way you can also easily tune some of Babel options in the way specific to your project; however if you feel that an option change you need is necessary for any generic react-based project, consider to submit an update to the configuration provided by `topcoder-react-utils`. |
| 18 | + |
| 19 | +To set options provided by a preset: |
| 20 | +```json |
| 21 | +{ |
| 22 | + "presets": [ |
| 23 | + ["topcoder-react-utils/config/babel/node-ssr", { |
| 24 | + "someOption": "optionValue" |
| 25 | + }] |
| 26 | + ] |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +As the presets are JS modules, you can also wrap them into your own JS code to create a special preset that customizes Babel configuration in any necessary way. If you feel that the modification you need is useful for any generic ReactJS app, feel free to open GitHub issue ticket to discuss an update of our default configurations. |
| 31 | + |
| 32 | +These presets may behave differently in different Babel environments. You can set the environment with `BABEL_ENV` environment variable, or with `forceEnv` option of the Webpack's `babel-loader`. |
19 | 33 |
|
20 | 34 | ### Configuration Details
|
21 | 35 |
|
22 |
| -- <a name="webpack">**`config/babel/webpack.json`**</a> — Babel config to use for JS and JSX files compilation during the Webpack build process; |
| 36 | +- <a name="node-ssr">**`config/babel/node-ssr.json`**</a> — Babel configuration for NodeJS compilation of JS and JSX modules (server-side ReactJS rendering); |
23 | 37 |
|
24 | 38 | - Presets: [env](https://www.npmjs.com/package/babel-preset-env), [react](https://www.npmjs.com/package/babel-preset-react), [stage-2](https://www.npmjs.com/package/babel-preset-stage-2);
|
25 | 39 |
|
26 |
| - - Uses [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules) to transform `styleName` props of ReactJS components into `className` props. A style name is transformed into: (i) `.path-to-the-stylesheet-and-stylesheet-file-name___original-style-name` class in the *development* Babel environment; (ii) `.6-symbols-hash-of-the-style` in the *production* environment. The class names generated in the first cases are verbose, thus good for debugging; and in the second case they are compact which results in smaller size of JS and CSS bundles. |
| 40 | + - Uses [`babel-plugin-css-modules-transform`](https://www.npmjs.com/package/babel-plugin-css-modules-transform) to convert CSS and SCSS imports into objects with keys and values being the original and transformed class names. The transformed class names will match the class names generated by the `babel-plugin-react-css-modules` plugin, mentioned below; |
27 | 41 |
|
28 |
| - - Uses [`babel-plugin-inline-react-svg`](https://www.npmjs.com/package/babel-plugin-inline-react-svg) to embed imported SVG files into complied JS code; |
| 42 | + - Uses [`babel-plugin-dynamic-import-node`](https://www.npmjs.com/package/babel-plugin-dynamic-import-node) to support dynamic `import(..)` statemens; |
29 | 43 |
|
30 |
| - - Uses [`babel-plugin-transform-runtime`](https://ww.npmjs.com/package/babel-plugin-transform-runtime) to generate more optimal JS bundles; |
| 44 | + - Uses [`babel-plugin-inline-react-svg`](https://www.npmjs.com/package/babel-plugin-inline-react-svg) to embed imported SVG files into the complied JS modules; |
31 | 45 |
|
32 |
| - - Uses [`babel-plugin-module-resolver`](https://www.npmjs.com/package/babel-plugin-module-resolver) to setup the following paths as the roots for resolution of relative imports: `./src/shared`, `./src`; |
| 46 | + - Uses [`babel-plugin-module-resolver`](https://www.npmjs.com/package/babel-plugin-module-resolver) to setup the following paths as the roots for resolution of relative imports: `./src/shared`, `./src`. It will also apply path resolution to the first argument of `resolveWeak(..)` functions encountered in the compiled code; |
| 47 | + |
| 48 | + - Uses [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules) to transform `styleName` props of ReactJS components into globally unique `className` props. Generated class names are verbose in *development* and *test* Babel environment, for the ease of debugging; and they are short 6-symbols hashes in *production* Babel environment, for compactness of the compiled JS and CSS modules; |
| 49 | + |
| 50 | + - Uses [`babel-plugin-tranform-assets`](https://www.npmjs.com/package/babel-plugin-transform-assets) to convert GIF, JPEG, JPG, and PNG imports into `/images/[FILE_HASH].[FILE_EXTENSION]` URL strings. Use the **`baseAssetsOutputPath`** preset option to prefix generated paths with an arbitrary path; |
33 | 51 |
|
34 |
| - - Uses [`react-hot-loader/babel`](https://www.npmjs.com/package/react-hot-loader) to support hot module reloading in *development* environment. |
| 52 | + - Uses [`babel-plugin-transform-runtime`](https://www.npmjs.com/package/babel-plugin-transform-runtime) to optimized complied JS modules (it externalizes references to helpers and builtins, automatically polyfilling the code without polluting globals). |
35 | 53 |
|
36 |
| -- <a name="node-ssr">**`config/babel/node-ssr.json`**</a> — Server-side configuration with support of server-side rendering. It extends [`config/babel/webpack.json`](#webpack) configuration with the following: |
| 54 | +- <a name="webpack">**`config/babel/webpack`**</a> — Babel configuration for Webpack compilation of JS and JSX modules. |
37 | 55 |
|
38 |
| - - In the *test* Babel environment [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules) transforms a style name into `.path-to-the-stylesheet-___stylesheet-file-name__origina-style-name___5-symbols-hash-of-the-style` class. Resuling class names are verbose and good for debugging, the difference from *development* environment is for historic reasons. |
| 56 | + - Presets: [env](https://www.npmjs.com/package/babel-preset-env), [react](https://www.npmjs.com/package/babel-preset-react), [stage-2](https://www.npmjs.com/package/babel-preset-stage-2); |
| 57 | + |
| 58 | + - Uses [`babel-plugin-inline-react-svg`](https://www.npmjs.com/package/babel-plugin-inline-react-svg) to embed imported SVG files into the complied JS modules; |
39 | 59 |
|
40 |
| - - Uses [`babel-plugin-css-modules-transform`](https://www.npmjs.com/package/babel-plugin-css-modules-transform) to convert CSS and SCSS imports into objects with keys and values being the original and generated class names. The class names are generated by the same rules being used to transform style names into classes; |
| 60 | + - Uses [`babel-plugin-module-resolver`](https://www.npmjs.com/package/babel-plugin-module-resolver) to setup the following paths as the roots for resolution of relative imports: `./src/shared`, `./src`; |
41 | 61 |
|
42 |
| - - Uses [`babel-plugin-tranform-assets`](https://www.npmjs.com/package/babel-plugin-transform-assets) to convert GIF, JPEG, JPG, and PNG imports into URL strings like `/images/hash-of-the-asset-file.original-file-extension`, where `/images/` is a fixed path prefix; |
| 62 | + - Uses [`babel-plugin-react-css-modules`](https://www.npmjs.com/package/babel-plugin-react-css-modules) to transform `styleName` props of ReactJS components into globally unique `className` props. Generated class names are verbose in *development* and *test* Babel environment, for the ease of debugging; and they are short 6-symbols hashes in *production* Babel environment, for compactness of the compiled JS and CSS modules; |
43 | 63 |
|
44 |
| - - Uses [`babel-plugin-dynamic-import-node`](https://www.npmjs.com/package/babel-plugin-dynamic-import-node) to support dynamic `import(..)` statement; |
| 64 | + - Uses [`react-hot-loader/babel`](https://www.npmjs.com/package/react-hot-loader) to support hot module reloading in the *development* Babel environment; |
45 | 65 |
|
46 |
| - - Avoids using [`react-hot-loader/babel`](https://www.npmjs.com/package/react-hot-loader) in the *development* Babel environment. |
| 66 | + - Uses [`babel-plugin-transform-runtime`](https://www.npmjs.com/package/babel-plugin-transform-runtime) to optimized complied JS modules (it externalizes references to helpers and builtins, automatically polyfilling the code without polluting globals). |
0 commit comments