Skip to content

Commit 6f136f6

Browse files
maxproskeijjk
andauthored
Convert with-absolute-imports example to TypeScript (vercel#42529)
Converted example to TypeScript to match Contribution docs. - ~~Renamed `with-absolute-imports` example to `absolute-imports-and-aliases` to match Contribution docs~~ - ~~Replaced deprecated example with a `README.md` file~~ - Used module path aliases in example, to help developers decide which import strategy to use ## Documentation / Examples - [X] Make sure the linting passes by running `pnpm build && pnpm lint` - [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper <[email protected]>
1 parent 22b449b commit 6f136f6

File tree

9 files changed

+67
-17
lines changed

9 files changed

+67
-17
lines changed

docs/advanced-features/module-path-aliases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Configure module path aliases that allow you to remap certain impor
77
<details>
88
<summary><b>Examples</b></summary>
99
<ul>
10-
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports">Absolute Imports</a></li>
10+
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports">Absolute Imports and Aliases</a></li>
1111
</ul>
1212
</details>
1313

examples/with-absolute-imports/README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
# Example app with absolute imports
1+
# Absolute Imports and Aliases
22

3-
This example shows how to configure Babel to have absolute imports instead of relative imports without modifying the Webpack configuration.
3+
This example shows how to configure [Absolute imports and Module path aliases](https://nextjs.org/docs/advanced-features/module-path-aliases) in `tsconfig.json` (or `jsconfig.json` for JavaScript projects). These options will allow absolute imports from `.` (the root directory), and allow you to create custom import aliases.
4+
5+
If you’re working on a large project, your relative import statements might suffer from `../../../` spaghetti:
6+
7+
```tsx
8+
import Button from '../../../components/button'
9+
```
10+
11+
In such cases, we might want to setup absolute imports using the `baseUrl` option, for clearer and shorter imports:
12+
13+
```tsx
14+
import Button from 'components/button'
15+
```
16+
17+
Furthermore, TypeScript also supports the `paths` option, which allows you to configure custom module aliases. You can then use your alias like so:
18+
19+
```tsx
20+
import Button from '@/components/button'
21+
```
422

523
## Deploy your own
624

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Button() {
2+
return <button>👋 Hello</button>
3+
}

examples/with-absolute-imports/jsconfig.json

-5
This file was deleted.

examples/with-absolute-imports/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
"next": "latest",
1010
"react": "^18.2.0",
1111
"react-dom": "^18.2.0"
12+
},
13+
"devDependencies": {
14+
"@types/node": "^18.11.9",
15+
"@types/react": "^18.0.25",
16+
"@types/react-dom": "^18.0.8",
17+
"typescript": "^4.8.4"
1218
}
1319
}

examples/with-absolute-imports/pages/index.js

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Header from 'components/header'
2+
import Button from '@/components/button'
3+
4+
export default function Home() {
5+
return (
6+
<>
7+
<Header />
8+
<Button />
9+
</>
10+
)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true,
17+
// Using "baseUrl" allows you to use absolute paths
18+
"baseUrl": ".",
19+
// Using "paths" allows you to configure module path aliases
20+
"paths": {
21+
"@/components/*": ["components/*"]
22+
}
23+
},
24+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
25+
"exclude": ["node_modules"]
26+
}

0 commit comments

Comments
 (0)