Skip to content

Commit

Permalink
Convert with-absolute-imports example to TypeScript (#42529)
Browse files Browse the repository at this point in the history
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 <jj@jjsweb.site>
  • Loading branch information
maxproske and ijjk committed Nov 7, 2022
1 parent 22b449b commit 6f136f6
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/module-path-aliases.md
Expand Up @@ -7,7 +7,7 @@ description: Configure module path aliases that allow you to remap certain impor
<details>
<summary><b>Examples</b></summary>
<ul>
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports">Absolute Imports</a></li>
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports">Absolute Imports and Aliases</a></li>
</ul>
</details>

Expand Down
22 changes: 20 additions & 2 deletions examples/with-absolute-imports/README.md
@@ -1,6 +1,24 @@
# Example app with absolute imports
# Absolute Imports and Aliases

This example shows how to configure Babel to have absolute imports instead of relative imports without modifying the Webpack configuration.
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.

If you’re working on a large project, your relative import statements might suffer from `../../../` spaghetti:

```tsx
import Button from '../../../components/button'
```

In such cases, we might want to setup absolute imports using the `baseUrl` option, for clearer and shorter imports:

```tsx
import Button from 'components/button'
```

Furthermore, TypeScript also supports the `paths` option, which allows you to configure custom module aliases. You can then use your alias like so:

```tsx
import Button from '@/components/button'
```

## Deploy your own

Expand Down
3 changes: 3 additions & 0 deletions examples/with-absolute-imports/components/button.tsx
@@ -0,0 +1,3 @@
export default function Button() {
return <button>👋 Hello</button>
}
5 changes: 0 additions & 5 deletions examples/with-absolute-imports/jsconfig.json

This file was deleted.

6 changes: 6 additions & 0 deletions examples/with-absolute-imports/package.json
Expand Up @@ -9,5 +9,11 @@
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"typescript": "^4.8.4"
}
}
9 changes: 0 additions & 9 deletions examples/with-absolute-imports/pages/index.js

This file was deleted.

11 changes: 11 additions & 0 deletions examples/with-absolute-imports/pages/index.tsx
@@ -0,0 +1,11 @@
import Header from 'components/header'
import Button from '@/components/button'

export default function Home() {
return (
<>
<Header />
<Button />
</>
)
}
26 changes: 26 additions & 0 deletions examples/with-absolute-imports/tsconfig.json
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
// Using "baseUrl" allows you to use absolute paths
"baseUrl": ".",
// Using "paths" allows you to configure module path aliases
"paths": {
"@/components/*": ["components/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

0 comments on commit 6f136f6

Please sign in to comment.