1 | # Alias solution for rewired create-react-app
|
2 |
|
3 | This is more than simple alias. This is also a multi-project `src`
|
4 | directory. Currently, `create-react-app` (CRA) does not support more than one
|
5 | `src` directory in the project. Monorepo, multi-repo and library projects with
|
6 | examples require more than one directories like `src`.
|
7 |
|
8 | This is merely an alias and multi-source solution for CRA
|
9 | and this is not a replacement for multi-package management tools like
|
10 | [Lerna](https://github.com/lerna/lerna).
|
11 |
|
12 | This project requires the use of **[react-app-rewired](https://github.com/timarney/react-app-rewired)**,
|
13 | which allows to overwrite the Webpack configuration
|
14 | of 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
|
46 | yarn add --dev react-app-rewired react-app-rewire-alias
|
47 | ```
|
48 |
|
49 | or
|
50 |
|
51 | ```sh
|
52 | npm install --save-dev react-app-rewired react-app-rewire-alias
|
53 | ```
|
54 |
|
55 | #### Usage
|
56 |
|
57 | Place for alias foldes is recommended near to **src**.
|
58 | Alias 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
|
68 | const {alias, configPaths} = require('react-app-rewire-alias')
|
69 |
|
70 | module.exports = function override(config) {
|
71 | alias(configPaths('./tsconfig.paths.json'))(config)
|
72 |
|
73 | return config
|
74 | }
|
75 | ```
|
76 |
|
77 | This is compatible with [customize-cra](https://github.com/arackaf/customize-cra),
|
78 | just insert it into the override chain.
|
79 |
|
80 | #### Add **extends** section to **jsconfig.json** or **tsconfig.json**
|
81 |
|
82 | The **paths** section must not be configured directly in `jsconfig.json` or `tsconfig.json`
|
83 | but in separated extends file.
|
84 |
|
85 | Specify **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 |
|
99 | Create 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 |
|
116 | Integrating `react-app-rewired` into your project is very simple
|
117 | (see [its documentation](https://github.com/timarney/react-app-rewired#readme)):
|
118 | Create `config-overrides.js` mentioned above in the project's root directory
|
119 | (the same including the `package.json` and `src` directory)
|
120 | and 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 |
|
134 | That is all. Now you can continue to use `yarn` or `npm` start/build/test commands as usual.
|
135 |
|
136 | #### API
|
137 |
|
138 | * **alias()**
|
139 |
|
140 | The function `alias()` accepts aliases declared in form:
|
141 |
|
142 | ```js
|
143 | {
|
144 | example: 'example/src',
|
145 | '@library': 'library/src',
|
146 | }
|
147 | ```
|
148 |
|
149 | To make all things worked, aliases must be declared in `jsconfig.json` or `tsconfig.json`.
|
150 | However it must beclared in separated extends file (see section `Workaround for
|
151 | "aliased imports are not supported"` below)
|
152 |
|
153 | * **configPaths()**
|
154 |
|
155 | The function `configPaths()` loads paths from file compatible with `jsconfig.json`
|
156 | or `tsconfig.json` and returns path in form acceptable for `alias()` function.
|
157 | The `tsconfig.json` is prioritized over the `jsconfig.json` in loading sequence.
|
158 |
|
159 | #### Workaround for "aliased imports are not supported"
|
160 |
|
161 | CRA [overwrites](/blob/v3.4.1/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L242)
|
162 | your `tsconfig.json` at runtime and removes `paths` from the `tsconfig.json`,
|
163 | which 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 |
|
170 | The [suggested workaround](https://github.com/facebook/create-react-app/issues/5645#issuecomment-435201019)
|
171 | is 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 |
|
186 | with 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 |
|
199 | Confusions in deps versions may bring unclear errors or problems. For example application
|
200 | is not working without any error. Or another example is error in `react-router` - `<Route>`
|
201 | component 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 |
|
205 | This may be a result of some confusions in `node_modules` folders of multirepo projects.
|
206 | Same take place in plain `create-react-app` if some how one or more additional
|
207 | `node_modulest` directory appers in `src`.
|
208 |
|
209 | To avoid this problems **use only one main project `node_modules` directory**.
|
210 |
|
211 | * **keep away from working with nested project**
|
212 |
|
213 | Default 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
|
215 | bring 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
|
217 | in another one - but only independent toplevel configuration** Or consider to eject
|
218 | or configure webpack manually.
|
219 |
|
220 | * **do not relay to deps versions synchronization**
|
221 |
|
222 | Some libraryes uses `instanceof` and other type comparisions. For example two objects
|
223 | created with same params in same code of same library version but installed in
|
224 | differenent `node_modules` and bundled separately - will mostly have same data and same
|
225 | behaviour but differen instance type. Such libraries will be unable to recognize its own
|
226 | objects and will lead to unpredictable behaviour. So **use only one main project
|
227 | `node_modules` directory**.
|