UNPKG

7.73 kBMarkdownView Raw
1# Alias solution for rewired create-react-app
2
3This is more than simple alias. This is also a multi-project `src`
4directory. Currently, `create-react-app` (CRA) does not support more than one
5`src` directory in the project. Monorepo, multi-repo and library projects with
6examples require more than one directories like `src`.
7
8This is merely an alias and multi-source solution for CRA
9and this is not a replacement for multi-package management tools like
10[Lerna](https://github.com/lerna/lerna).
11
12This project requires the use of **[react-app-rewired](https://github.com/timarney/react-app-rewired)**,
13which allows to overwrite the Webpack configuration
14of CRA projects without ejecting them.
15
16[![Npm package](https://img.shields.io/npm/v/react-app-rewire-alias.svg?style=flat)](https://npmjs.com/package/react-app-rewire-alias)
17[![Npm downloads](https://img.shields.io/npm/dm/react-app-rewire-alias.svg?style=flat)](https://npmjs.com/package/react-app-rewire-alias)
18[![Dependency Status](https://david-dm.org/oklas/react-app-rewire-alias.svg)](https://david-dm.org/oklas/react-app-rewire-alias)
19[![Dependency Status](https://img.shields.io/github/stars/oklas/react-app-rewire-alias.svg?style=social&label=Star)](https://github.com/oklas/react-app-rewire-alias)
20[![Dependency Status](https://img.shields.io/twitter/follow/oklaspec.svg?style=social&label=Follow)](https://twitter.com/oklaspec)
21
22#### This allows:
23
24* quality and secure exports from outside `src`
25* absolute imports
26* any `./directory` at root outside of `src` with Babel and CRA features
27
28#### This is designed for:
29
30* monorepo projects
31* multi-repo projects
32* library projects with examples
33
34#### Advantages over other solutions:
35
36 * provided fully functional aliases and allows the use of Babel, JSX, etc.
37 outside of `src`
38
39 * provided fully secure aliases and uses the same module scope plugin from
40 the original create-react-app package for modules (instead of removing it),
41 to minimize the probability of including unwanted code
42
43#### Installation
44
45```sh
46yarn add --dev react-app-rewired react-app-rewire-alias
47```
48
49or
50
51```sh
52npm install --save-dev react-app-rewired react-app-rewire-alias
53```
54
55#### Usage
56
57Place for alias foldes is recommended near to **src**.
58Alias folders outside of the root of project is not recommended.
59
60* configure `create-app-rewired` if not yet (short brief below)
61* modify **config-overrides.js** to add `react-app-rewire-alias`
62* add **extends** section to `jsconfig.json` or `tsconfig.json`
63* configure alias in `jsconfig.paths.json` or `tsconfig.paths.json`
64
65#### Modify config-overrides.js to add `react-app-rewire-alias`
66
67```js
68const {alias, configPaths} = require('react-app-rewire-alias')
69
70module.exports = function override(config) {
71 alias(configPaths('./tsconfig.paths.json'))(config)
72
73 return config
74}
75```
76
77This is compatible with [customize-cra](https://github.com/arackaf/customize-cra),
78just insert it into the override chain.
79
80#### Add **extends** section to **jsconfig.json** or **tsconfig.json**
81
82The **paths** section must not be configured directly in `jsconfig.json` or `tsconfig.json`
83but in separated extends file.
84
85Specify **extends** section
86
87```js
88// jsconfig.json or tsconfig.json
89{
90 "compilerOptions": {
91 // ...
92 "extends": "./tsconfig.paths.json", // or "./tsconfig.paths.json"
93 }
94}
95```
96
97#### Configure alias in **jsconfig.paths.json** or **tsconfig.paths.json**
98
99Create separated file `jsconfig.paths.json` or `tsconfig.paths.json`, like this:
100
101```js
102// jsconfig.paths.json or tsconfig.paths.json
103{
104 "compilerOptions": {
105 "baseUrl": ".",
106 "paths": {
107 "example/*": ["example/src/*"],
108 "@library/*": ["library/src/*"]
109 }
110 }
111}
112```
113
114#### Using react-app-rewired
115
116Integrating `react-app-rewired` into your project is very simple
117(see [its documentation](https://github.com/timarney/react-app-rewired#readme)):
118Create `config-overrides.js` mentioned above in the project's root directory
119(the same including the `package.json` and `src` directory)
120and rewrite the `package.json` like this:
121
122```diff
123 "scripts": {
124- "start": "react-scripts start",
125+ "start": "react-app-rewired start",
126- "build": "react-scripts build",
127+ "build": "react-app-rewired build",
128- "test": "react-scripts test",
129+ "test": "react-app-rewired test",
130 "eject": "react-scripts eject"
131}
132```
133
134That is all. Now you can continue to use `yarn` or `npm` start/build/test commands as usual.
135
136#### API
137
138* **alias()**
139
140The function `alias()` accepts aliases declared in form:
141
142```js
143{
144 example: 'example/src',
145 '@library': 'library/src',
146}
147```
148
149To make all things worked, aliases must be declared in `jsconfig.json` or `tsconfig.json`.
150However it must beclared in separated extends file (see section `Workaround for
151"aliased imports are not supported"` below)
152
153* **configPaths()**
154
155The function `configPaths()` loads paths from file compatible with `jsconfig.json`
156or `tsconfig.json` and returns path in form acceptable for `alias()` function.
157The `tsconfig.json` is prioritized over the `jsconfig.json` in loading sequence.
158
159#### Workaround for "aliased imports are not supported"
160
161CRA [overwrites](/blob/v3.4.1/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L242)
162your `tsconfig.json` at runtime and removes `paths` from the `tsconfig.json`,
163which is not officially supported, with this message:
164
165> ```
166> The following changes are being made to your tsconfig.json file:
167> - compilerOptions.paths must not be set (aliased imports are not supported)
168> ```
169
170The [suggested workaround](https://github.com/facebook/create-react-app/issues/5645#issuecomment-435201019)
171is to move the paths to a different `.json` file, e.g. `tsconfig.paths.json`, like this:
172
173```json
174/* tsconfig.paths.json */
175{
176 "compilerOptions": {
177 "baseUrl": ".",
178 "paths": {
179 "example/*": ["example/src/*"],
180 "@library/*": ["library/src/*"]
181 }
182 }
183}
184```
185
186with that file's subsequent inclusion in the `tsconfig.json` using `extends`:
187
188```json
189/* tsconfig.json */
190{
191 "extends": "./tsconfig.paths.json"
192}
193```
194
195## Tips
196
197* **keep only one `node_modules` directory**
198
199Confusions in deps versions may bring unclear errors or problems. For example application
200is not working without any error. Or another example is error in `react-router` - `<Route>`
201component do not see `<Router>` when actually code is correct and it falls with:
202
203> should not use Route or withRouter() outside a Router
204
205This may be a result of some confusions in `node_modules` folders of multirepo projects.
206Same take place in plain `create-react-app` if some how one or more additional
207`node_modulest` directory appers in `src`.
208
209To avoid this problems **use only one main project `node_modules` directory**.
210
211* **keep away from working with nested project**
212
213Default bundler configuration doesn't assume your configuration and may mix deps from
214`node_modules` from different projects (top project and nested project) so this may
215bring mentioned above confusions with deps versions. To avoid problems:
216**do not install and run within nested project directly when it is nested or integrated
217in another one - but only independent toplevel configuration** Or consider to eject
218or configure webpack manually.
219
220* **do not relay to deps versions synchronization**
221
222Some libraryes uses `instanceof` and other type comparisions. For example two objects
223created with same params in same code of same library version but installed in
224differenent `node_modules` and bundled separately - will mostly have same data and same
225behaviour but differen instance type. Such libraries will be unable to recognize its own
226objects and will lead to unpredictable behaviour. So **use only one main project
227`node_modules` directory**.