You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Create React App: import modules using aliases with Webpack and Typescript
2
2
3
+
<imgsrc="https://github.com/Wavelop/cra-module-import-aliasing/blob/master/cra-module-import-aliasing.jpg"alt="Cover - Create React App: import modules using aliases with Webpack and Typescript"width="100%"/>
## Update Webpack configuration to use aliases instead of relative paths
85
87
86
-
Add to **[config/webpack.config.js]()** file - in particular in the **resolve.alias** variables of the return object - the following lines:
88
+
Add to **[config/webpack.config.js](https://github.com/Wavelop/cra-with-module-alias/blob/master/config/webpack.config.js)** file - in particular in the **[resolve.alias](https://github.com/Wavelop/cra-with-module-alias/blob/master/config/webpack.config.js#L291)** variables of the return object - the following lines:
87
89
88
90
```javascript
89
91
// config/webpack.config.js
@@ -115,7 +117,7 @@ import { ServiceName } from 'Services';
115
117
116
118
## Update Typescript configuration to use aliases instead of relative paths
117
119
118
-
Second step to use aliasing is update Typescript configuration. Add to **[tsconfig.json]()** file - in particular in the **resolve.alias**variables of the return object - the following lines:
120
+
The second step, to use aliasing, is to update the Typescript configuration. Add to **[tsconfig.json](https://github.com/Wavelop/cra-with-module-alias/blob/master/tsconfig.json)**file the following lines:
119
121
120
122
```json
121
123
// tsconfig.json
@@ -157,9 +159,8 @@ export { default as Logo } from './images/logo.svg';
157
159
158
160
In the end, we need to update `src/index.tsx` as the following:
159
161
160
-
`src/index.js:`
161
-
162
162
```javascript
163
+
// src/index.js
163
164
importReactfrom'react';
164
165
importReactDOMfrom'react-dom';
165
166
import'./index.css';
@@ -205,7 +206,6 @@ To execute the test with modules as aliases, we need to update the jest configur
205
206
}
206
207
```
207
208
208
-
209
209
## Visual Studio Code Tips
210
210
211
211
Using Visual Studio Code as editor, you can get component names via autocomplete using `CTRL+Space` (or using your combinations).
@@ -223,10 +223,17 @@ Using Visual Studio Code as editor, you can get component names via autocomplete
223
223
224
224
## Conclusion
225
225
226
-
Doing these semple steps you will be able to forget the relative paths and make your folders structure more flexible to the changes.
226
+
Doing these simple steps you will be able to forget the relative paths and make your folders structure more flexible to the changes.
227
227
228
-
You can read the post of our blog[here](https://wavelop.com/en/story/create-react-app-module-import-path-aliasing-with-webpack-typescript/).
228
+
You can find the code of this tutorial[here](https://github.com/Wavelop/cra-with-module-alias), and follow the guide, step by step, in this [PR](https://github.com/Wavelop/cra-with-module-alias/pull/1).
229
229
230
230
You can take a look at a demo [here](https://cra-module-import-aliasing.web.app/).
231
231
232
232
If you have questions, please write to us on the chat or an email to [info@wavelop.com](mailto:info@wavelop.com).
233
+
234
+
Originally published at https://wavelop.com/en/story/create-react-app-module-import-path-aliasing-with-webpack-typescript/ on August 28, 2020.
235
+
236
+
## Credits
237
+
238
+
- <span>Photo by <ahref="https://unsplash.com/@camwitney?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Cameron Witney</a> on <ahref="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span>
239
+
- <ahref='https://www.freepik.com/vectors/light'>Light vector created by stories - www.freepik.com</a>
0 commit comments