Skip to content

Commit c6c20f5

Browse files
committed
feat: migrate to alias options #58
BREAKING CHANGE: Functions `aliasWebpack` and `aliasJest` will receive object with options instead of alias map. Alias map will be option `alias` in object of options.
1 parent d1ff6b3 commit c6c20f5

File tree

5 files changed

+94
-72
lines changed

5 files changed

+94
-72
lines changed

README.md

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This requires to modify the CRA webpack configuration in runtime
2525

2626
* quality and secure exports from outside `src`
2727
* 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
2929

3030
#### This is designed for:
3131

@@ -46,26 +46,26 @@ This requires to modify the CRA webpack configuration in runtime
4646
#### Installation
4747

4848
```sh
49-
yarn add --dev react-app-rewire-alias
49+
yarn add --dev react-app-alias
5050
```
5151

5252
or
5353

5454
```sh
55-
npm install --save-dev react-app-rewire-alias
55+
npm install --save-dev react-app-alias
5656
```
5757

5858
### Usage
5959

6060
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`).
6262

6363
Usage steps:
6464

6565
* enumerate aliases in *jsconfig.paths.json* or *tsconfig.paths.json*
6666
* include it in *jsconfig.json* or *tsconfig.json*
6767
* 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
6969

7070
#### Enumerate aliases in **jsconfig.paths.json** or **tsconfig.paths.json**
7171

@@ -100,43 +100,43 @@ Now include this file in **extends** section, like this:
100100
}
101101
```
102102

103-
### Configure plugin for react-app-rewired
103+
### Configure plugin for craco or react-app-rewired
104+
105+
* **`react-app-rewired`**
104106

105107
```js
106108
// config-overrides.js
107-
const {aliasWebpack, configPaths} = require('react-app-rewire-alias')
109+
const {aliasWebpack, aliasJest} = require('react-app-alias')
108110

109-
const aliasMap = configPaths('./tsconfig.paths.json') // or jsconfig.paths.json
111+
const options = {} // default is empty for most cases
110112

111-
module.exports = aliasWebpack(aliasMap)
112-
module.exports.jest = aliasJest(aliasMap)
113+
module.exports = aliasWebpack(options)
114+
module.exports.jest = aliasJest(options)
113115
```
114116

115-
*aliasMap may be filled manually, for non-typescript only, see api*
116-
117-
### Configure plugin for craco
117+
* **`craco`**
118118

119119
```js
120120
// craco.config.js
121121

122-
const {CracoAliasPlugin, configPaths} = require('react-app-rewire-alias')
122+
const {CracoAliasPlugin} = require('react-app-rewire-alias')
123123

124-
const aliasMap = configPaths('./tsconfig.paths.json') // or jsconfig.paths.json
124+
const options = {} // default is empty for most cases
125125

126126
module.exports = {
127127
plugins: [
128128
{
129129
plugin: CracoAliasPlugin,
130-
options: {alias: aliasMap}
130+
options: {}
131131
}
132132
]
133133
}
134134
```
135135

136-
*aliasMap may be filled manually, for non-typescript only, see api*
137136

137+
#### Enable craco or react-app-rewired
138138

139-
#### Enable react-app-rewired
139+
* **`react-app-rewired`**
140140

141141
Integrating `react-app-rewired` into your project is simple
142142
(see [its documentation](https://github.com/timarney/react-app-rewired#readme)):
@@ -159,7 +159,7 @@ and rewrite the `package.json` like this:
159159
}
160160
```
161161

162-
#### Enable craco
162+
* **`craco`**
163163

164164
According to [craco](https://github.com/gsoft-inc/craco/blob/master/packages/craco/README.md#installation)
165165
docs install craco:
@@ -182,7 +182,24 @@ and replace `react-scripts` in `package.json`:
182182

183183
#### API
184184

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)**
186203

187204
The function `aliasWebpack()` accepts aliases declared in form:
188205

@@ -192,7 +209,12 @@ const aliasMap = {
192209
'@library': 'library/src',
193210
}
194211

195-
module.exports = aliasWebpack(aliasMap)
212+
const options = {
213+
alias: aliasMap,
214+
}
215+
216+
module.exports = aliasWebpack(options)
217+
module.exports.jest = aliasJest(options)
196218
```
197219

198220
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
210232
```js
211233
const aliasMap = configPaths('./tsconfig.paths.json')
212234

235+
const options = {
236+
alias: aliasMap,
237+
}
238+
213239
module.exports = aliasWebpack(aliasMap)
214240
```
215241

@@ -270,39 +296,17 @@ with that file's subsequent inclusion in the `tsconfig.json` using `extends`:
270296
## Outside of root
271297

272298
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.
279300

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:
283302

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`
287304

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:
290306

291307
```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')
306310
```
307311

308312
## Tips

example/above/craco.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/* eslint-disable */
2-
const { CracoAliasPlugin, configPaths } = require('react-app-alias-ex');
3-
const aliasPaths = configPaths('./tsconfig.paths.json')
2+
const { CracoAliasPlugin } = require('react-app-alias-ex');
43

54
module.exports = {
65
plugins: [
76
{
87
plugin: CracoAliasPlugin,
98
options: {
10-
alias: aliasPaths,
119
},
1210
},
1311
],

example/main/config-overrides.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const {aliasWebpack, aliasJest, configPaths} = require('react-app-alias')
1+
const {aliasWebpack, aliasJest} = require('react-app-alias')
22

3-
const aliasPaths = configPaths('./tsconfig.paths.json')
3+
const options = {}
44

5-
module.exports = aliasWebpack(aliasPaths)
6-
module.exports.jest = aliasJest(aliasPaths)
5+
module.exports = aliasWebpack(options)
6+
module.exports.jest = aliasJest(options)

packages/react-app-alias-ex/src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
configFilePath,
66
configPathsRaw,
77
configPaths,
8+
defaultOptions,
89
expandResolveAlias,
910
expandPluginsScope,
1011
} = require('react-app-alias')
@@ -106,7 +107,8 @@ function expandPluginsTsChecker(plugins, configPath) {
106107
}
107108
}
108109

109-
function aliasJest(aliasMap) {
110+
function aliasJest(options) {
111+
const aliasMap = defaultOptions(options).aliasMap
110112
const aliasJestInstance = aliasJestSafe(aliasMap)
111113
return function(config) {
112114
const expanded = aliasJestInstance(config)
@@ -120,7 +122,8 @@ function aliasJest(aliasMap) {
120122
}
121123
}
122124

123-
function aliasWebpack(aliasMap) {
125+
function aliasWebpack(options) {
126+
const aliasMap = defaultOptions(options).aliasMap
124127
const aliasLocal = Object.keys(aliasMap).reduce( (a,i) => {
125128
a[i] = path.resolve(paths.appPath, aliasMap[i])
126129
return a

packages/react-app-alias/src/index.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ const path = require('path')
33
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
44
const paths = require('react-scripts/config/paths')
55

6+
const guessConfigFiles = [
7+
'tsconfig.paths.json',
8+
'tsconfig.json',
9+
'jsconfig.paths.json',
10+
'jsconfig.json',
11+
]
12+
613
function expandResolveAlias(resolve, alias) {
714
resolve.alias = Object.assign(resolve.alias || {}, alias)
815
}
@@ -63,7 +70,8 @@ function checkOutside(aliasMap) {
6370
}
6471
}
6572

66-
function aliasWebpack(aliasMap) {
73+
function aliasWebpack(options) {
74+
const aliasMap = defaultOptions(options).aliasMap
6775
checkOutside(aliasMap)
6876
const aliasLocal = Object.keys(aliasMap).reduce( (a,i) => {
6977
a[i] = path.resolve(paths.appPath, aliasMap[i])
@@ -77,7 +85,8 @@ function aliasWebpack(aliasMap) {
7785
}
7886
}
7987

80-
function aliasJest(aliasMap) {
88+
function aliasJest(options) {
89+
const aliasMap = defaultOptions(options).aliasMap
8190
const jestAliasMap = aliasMapForJest(aliasMap)
8291
return function(config) {
8392
return {
@@ -91,19 +100,14 @@ function aliasJest(aliasMap) {
91100
}
92101

93102
function configFilePath(configPath = '') {
94-
return (
95-
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath)) ?
96-
path.resolve(paths.appPath, configPath) :
97-
fs.existsSync(path.resolve(paths.appPath, 'tsconfig.paths.json')) ?
98-
path.resolve(paths.appPath, 'tsconfig.paths.json') :
99-
fs.existsSync(path.resolve(paths.appPath, 'tsconfig.json')) ?
100-
path.resolve(paths.appPath, 'tsconfig.json') :
101-
fs.existsSync(path.resolve(paths.appPath, 'jsconfig.paths.json')) ?
102-
path.resolve(paths.appPath, 'jsconfig.paths.json') :
103-
fs.existsSync(path.resolve(paths.appPath, 'jsconfig.json')) ?
104-
path.resolve(paths.appPath, 'jsconfig.json') :
105-
''
103+
if(
104+
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath))
105+
) return path.resolve(paths.appPath, configPath)
106+
const existsPaths = (guessConfigFiles
107+
.map(filename => path.resolve(paths.appPath, filename))
108+
.filter(pathname => fs.existsSync(pathname))
106109
)
110+
return existsPaths.length ? existsPaths[0] : ''
107111
}
108112

109113
function configPathsRaw(confPath) {
@@ -134,6 +138,18 @@ function configPaths(configPath = '') {
134138
}, {})
135139
}
136140

141+
function defaultOptions(options) {
142+
const configPath = configFilePath(
143+
options.tsconfig || options.jsconfig
144+
)
145+
const aliasMap = options.alias || configPaths(configPath)
146+
const opts = {
147+
...options,
148+
aliasMap,
149+
}
150+
return opts
151+
}
152+
137153
function aliasMapForJest(aliasMap) {
138154
return Object.keys(aliasMap).reduce( (a, i) => {
139155
const outside = isOutsideOfRoot(aliasMap[i])
@@ -159,6 +175,7 @@ module.exports = {
159175
configFilePath,
160176
configPathsRaw,
161177
configPaths,
178+
defaultOptions,
162179
expandResolveAlias,
163180
expandRulesInclude,
164181
expandPluginsScope,

0 commit comments

Comments
 (0)