Skip to content

Commit 58943cb

Browse files
committed
Conclusion
1 parent e18df9e commit 58943cb

File tree

16 files changed

+74
-47
lines changed

16 files changed

+74
-47
lines changed

.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "cra-module-import-aliasing"
4+
}
5+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
.firebase

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Create React App: import modules using aliases with Webpack and Typescript
22

3+
<img src="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%"/>
4+
35
## Initialize project using Create React App
46

57
Execute the following commands:
@@ -9,7 +11,7 @@ npx create-react-app cra-with-module-alias --template typescript
911
cd cra-with-module-alias
1012
```
1113

12-
## Setup the environment and intall dependencies
14+
## Setup the environment and install dependencies
1315

1416
Execute:
1517

@@ -75,15 +77,15 @@ Create the following folders inside the **src** one:
7577
- **services**.
7678

7779
and inside of all of these folders, create an `index.ts` file.
78-
Inside of every `index.ts` file we are going to export the contained sub-folders. The sintax that we are going to use will be something like:
80+
Inside of every `index.ts` file, we are going to export the contained subfolders. The syntax that we are going to use will be something like:
7981

8082
```javascript
8183
export { default as ComponentName } from "./ComponentName/ComponentName";
8284
```
8385

8486
## Update Webpack configuration to use aliases instead of relative paths
8587

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

8890
```javascript
8991
// config/webpack.config.js
@@ -115,7 +117,7 @@ import { ServiceName } from 'Services';
115117

116118
## Update Typescript configuration to use aliases instead of relative paths
117119

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

120122
```json
121123
// tsconfig.json
@@ -157,9 +159,8 @@ export { default as Logo } from './images/logo.svg';
157159

158160
In the end, we need to update `src/index.tsx` as the following:
159161

160-
`src/index.js:`
161-
162162
```javascript
163+
// src/index.js
163164
import React from 'react';
164165
import ReactDOM from 'react-dom';
165166
import './index.css';
@@ -205,7 +206,6 @@ To execute the test with modules as aliases, we need to update the jest configur
205206
}
206207
```
207208

208-
209209
## Visual Studio Code Tips
210210

211211
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
223223

224224
## Conclusion
225225

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.
227227

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).
229229

230230
You can take a look at a demo [here](https://cra-module-import-aliasing.web.app/).
231231

232232
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 <a href="https://unsplash.com/@camwitney?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Cameron Witney</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>
239+
- <a href='https://www.freepik.com/vectors/light'>Light vector created by stories - www.freepik.com</a>

cra-module-import-aliasing.jpg

1.1 MB
Loading

firebase.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"hosting": {
3+
"public": "build",
4+
"ignore": [
5+
"firebase.json",
6+
"**/.*",
7+
"**/node_modules/**"
8+
],
9+
"rewrites": [
10+
{
11+
"source": "**",
12+
"destination": "/index.html"
13+
}
14+
]
15+
}
16+
}
17+

public/favicon.ico

13.5 KB
Binary file not shown.

public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="theme-color" content="#000000" />
88
<meta
99
name="description"
10-
content="Web site created using create-react-app"
10+
content="Create React App: import modules using aliases with Webpack and Typescript"
1111
/>
1212
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
1313
<!--
@@ -24,7 +24,7 @@
2424
work correctly both with client-side routing and a non-root public URL.
2525
Learn how to configure a non-root public URL by running `npm run build`.
2626
-->
27-
<title>React App</title>
27+
<title>Example | Create React App: import modules using aliases with Webpack and Typescript</title>
2828
</head>
2929
<body>
3030
<noscript>You need to enable JavaScript to run this app.</noscript>

public/logo192.png

12.9 KB
Loading

public/logo512.png

45.7 KB
Loading

public/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"short_name": "React App",
3-
"name": "Create React App Sample",
2+
"short_name": "CRA: webpack alias",
3+
"name": "Create React App: import modules using aliases with Webpack and Typescript",
44
"icons": [
55
{
66
"src": "favicon.ico",

0 commit comments

Comments
 (0)