@@ -25,7 +25,7 @@ This requires to modify the CRA webpack configuration in runtime
25
25
26
26
* quality and secure exports from outside ` src `
27
27
* absolute imports
28
- * any ` ./directory ` at root outside of ` src ` with Babel and CRA features
28
+ * any ` ./directory ` at root outside of ` src ` with Babel/Typescript and all CRA features
29
29
30
30
#### This is designed for:
31
31
@@ -46,26 +46,26 @@ This requires to modify the CRA webpack configuration in runtime
46
46
#### Installation
47
47
48
48
``` sh
49
- yarn add --dev react-app-rewire -alias
49
+ yarn add --dev react-app-alias
50
50
```
51
51
52
52
or
53
53
54
54
``` sh
55
- npm install --save-dev react-app-rewire- alias
55
+ npm install --save-dev react-app-alias
56
56
```
57
57
58
58
### Usage
59
59
60
60
By default folders for alias may be near to ** src** folder or in it.
61
- Outside of project ` root ` is enabled with special way, see below .
61
+ Outside of project ` root ` is implemented in ` react-app-alias-ex ` (suffix ` -ex ` ) .
62
62
63
63
Usage steps:
64
64
65
65
* enumerate aliases in * jsconfig.paths.json* or * tsconfig.paths.json*
66
66
* include it in * jsconfig.json* or * tsconfig.json*
67
67
* enable your favorite any of * react-app-rewired* or * craco*
68
- * apply this package plugin in config of * react-app-rewired * or * craco *
68
+ * apply react-app-alias in config
69
69
70
70
#### Enumerate aliases in ** jsconfig.paths.json** or ** tsconfig.paths.json**
71
71
@@ -100,43 +100,43 @@ Now include this file in **extends** section, like this:
100
100
}
101
101
```
102
102
103
- ### Configure plugin for react-app-rewired
103
+ ### Configure plugin for craco or react-app-rewired
104
+
105
+ * ** ` react-app-rewired ` **
104
106
105
107
``` js
106
108
// config-overrides.js
107
- const {aliasWebpack , configPaths } = require (' react-app-rewire -alias' )
109
+ const {aliasWebpack , aliasJest } = require (' react-app-alias' )
108
110
109
- const aliasMap = configPaths ( ' ./tsconfig.paths.json ' ) // or jsconfig.paths.json
111
+ const options = {} // default is empty for most cases
110
112
111
- module .exports = aliasWebpack (aliasMap )
112
- module .exports .jest = aliasJest (aliasMap )
113
+ module .exports = aliasWebpack (options )
114
+ module .exports .jest = aliasJest (options )
113
115
```
114
116
115
- * aliasMap may be filled manually, for non-typescript only, see api*
116
-
117
- ### Configure plugin for craco
117
+ * ** ` craco ` **
118
118
119
119
``` js
120
120
// craco.config.js
121
121
122
- const {CracoAliasPlugin , configPaths } = require (' react-app-rewire-alias' )
122
+ const {CracoAliasPlugin } = require (' react-app-rewire-alias' )
123
123
124
- const aliasMap = configPaths ( ' ./tsconfig.paths.json ' ) // or jsconfig.paths.json
124
+ const options = {} // default is empty for most cases
125
125
126
126
module .exports = {
127
127
plugins: [
128
128
{
129
129
plugin: CracoAliasPlugin,
130
- options: {alias : aliasMap }
130
+ options: {}
131
131
}
132
132
]
133
133
}
134
134
```
135
135
136
- * aliasMap may be filled manually, for non-typescript only, see api*
137
136
137
+ #### Enable craco or react-app-rewired
138
138
139
- #### Enable react-app-rewired
139
+ * ** ` react-app-rewired ` **
140
140
141
141
Integrating ` react-app-rewired ` into your project is simple
142
142
(see [ its documentation] ( https://github.com/timarney/react-app-rewired#readme ) ):
@@ -159,7 +159,7 @@ and rewrite the `package.json` like this:
159
159
}
160
160
```
161
161
162
- #### Enable craco
162
+ * ** ` craco ` **
163
163
164
164
According to [ craco] ( https://github.com/gsoft-inc/craco/blob/master/packages/craco/README.md#installation )
165
165
docs install craco:
@@ -182,7 +182,24 @@ and replace `react-scripts` in `package.json`:
182
182
183
183
#### API
184
184
185
- * ** aliasWebpack(aliasMap)(webpackConfig)**
185
+ * ** options**
186
+
187
+ ``` ts
188
+ Options {
189
+ alias ?: { [alias : string ]: string }; // optional alias map
190
+ tsconfig ?: string , // optinal tsconfig.json path
191
+ jsconfig ?: string , // options jsconfig.json path
192
+ }
193
+ ```
194
+
195
+ // optional alias map has following form:
196
+ const alias = {
197
+ example: 'example/src',
198
+ '@library ': 'library/src',
199
+ }
200
+
201
+
202
+ * ** aliasWebpack(options)(webpackConfig)**
186
203
187
204
The function ` aliasWebpack() ` accepts aliases declared in form:
188
205
@@ -192,7 +209,12 @@ const aliasMap = {
192
209
' @library' : ' library/src' ,
193
210
}
194
211
195
- module .exports = aliasWebpack (aliasMap)
212
+ const options = {
213
+ alias: aliasMap,
214
+ }
215
+
216
+ module .exports = aliasWebpack (options)
217
+ module .exports .jest = aliasJest (options)
196
218
```
197
219
198
220
To make all things worked, aliases must be declared in ` jsconfig.json ` or ` tsconfig.json ` .
@@ -210,6 +232,10 @@ The `tsconfig.json` is prioritized over the `jsconfig.json` in the loading seque
210
232
``` js
211
233
const aliasMap = configPaths (' ./tsconfig.paths.json' )
212
234
235
+ const options = {
236
+ alias: aliasMap,
237
+ }
238
+
213
239
module .exports = aliasWebpack (aliasMap)
214
240
```
215
241
@@ -270,39 +296,17 @@ with that file's subsequent inclusion in the `tsconfig.json` using `extends`:
270
296
## Outside of root
271
297
272
298
Alias folders outside of the root of the project currently fully functional and
273
- works fine but are not recommended. It has more complicated implementation which
274
- currently named * dangerous* and exported from package separately. Due to complicity
275
- it has a higher probability of being incompatible with the next release of
276
- * create-react-app* until an update is released, since these are different systems.
277
- However same is for the base implementation but with less probability of being
278
- incompatibe with next cra release.
299
+ works fine but are not recommended. It may bring hard-to-detect errors.
279
300
280
- It provides aliases with the same feature set as the original ` create-react-app ` .
281
- ` create-react-app ` does not support aliases and additional ` src ` -like directories as
282
- it does not supports aliases outside of the ` root ` project directory.
301
+ Alias with support of ** Outside of root** is implemented in separated library:
283
302
284
- Aliases outside or project ` root ` directory may be implemented with some
285
- [ limitation] ( https://github.com/oklas/react-app-rewire-alias/issues/3#issuecomment-633947385 )
286
- of feature set. That is solved by disabling ESLint checking.
303
+ ` react-app-alias-ex `
287
304
288
- This implementation is moved to separated code set named ` aliasDangerous ` to be not confused
289
- with ` alias ` . To use it just replace import like this:
305
+ with identical API, just install and add suffix ` -ex ` in import statement:
290
306
291
307
``` diff
292
- - const {alias, configPaths, CracoAliasPlugin} = require('react-app-rewire-alias')
293
- + const {aliasDangerous, configPaths, CracoAliasPlugin} = require('react-app-rewire-alias/lib/aliasDangerous')
294
- ```
295
-
296
- And replace ` alias ` with ` aliasDangerous ` :
297
-
298
- ``` js
299
- module .exports = function override (config ) {
300
- aliasDangerous ({
301
- ... configPaths (' tsconfig.paths.json' )
302
- })(config)
303
-
304
- return config
305
- }
308
+ - const {aliasWebpack, CracoAliasPlugin} = require('react-app-alias')
309
+ + const {aliasWebpack, CracoAliasPlugin} = require('react-app-alias-ex')
306
310
```
307
311
308
312
## Tips
0 commit comments