Skip to content

Commit

Permalink
feat(cli): generate tsconfig.json in prepare command (nuxt#822)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: pooya parsa <pyapar@gmail.com>
  • Loading branch information
3 people authored Oct 13, 2021
1 parent 39f7b83 commit 31b12d0
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/content/1.getting-started/3.bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ export default defineNuxtConfig({
})
```

### Update `tsconfig.json`

If you are using TypeScript, you can edit your `tsconfig.json` to benefit from autogenerated Nuxt types:

**tsconfig.json**

```diff
{
+ "extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
```

### Avoid CommonJS syntax

Nuxt 3 natively supports TypeScript and ECMAScript Modules.
Expand Down
3 changes: 3 additions & 0 deletions examples/hello-world/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/use-async-data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/use-fetch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/use-meta/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/use-state/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/with-components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/with-layouts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/with-pages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
3 changes: 3 additions & 0 deletions examples/with-wasm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"lodash.template": "^4.5.0",
"mlly": "^0.2.6",
"pathe": "^0.2.0",
"pkg-types": "^0.1.3",
"rc9": "^1.2.0",
"scule": "^0.2.1",
"semver": "^7.3.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { HookCallback } from 'hookable'
import type { Compiler, Configuration, Stats } from 'webpack'
import type { TSConfig } from 'pkg-types'
import type { NuxtConfig, NuxtOptions } from '..'
import type { ModuleContainer } from '../module/container'
import type { NuxtTemplate, Nuxt, NuxtApp } from '../types/nuxt'
Expand Down Expand Up @@ -61,7 +62,7 @@ export interface NuxtHooks extends Record<string, HookCallback> {
'run:before': (options: { argv: string[], cmd: { name: string, usage: string, description: string, options: Record<string, any> }, rootDir: string }) => HookResult

// nuxi
'prepare:types': (options: { references: TSReference[], declarations: string[] }) => HookResult
'prepare:types': (options: { references: TSReference[], declarations: string[], tsConfig: TSConfig }) => HookResult

// @nuxt/core
'ready': (nuxt: Nuxt) => HookResult
Expand Down
1 change: 1 addition & 0 deletions packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"mri": "^1.2.0",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"pkg-types": "^0.1.3",
"scule": "^0.2.1",
"superb": "^4.0.0",
"unbuild": "latest"
Expand Down
39 changes: 38 additions & 1 deletion packages/nuxi/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,46 @@ import { promises as fsp } from 'fs'
import { relative, resolve } from 'pathe'
import { cyan } from 'colorette'
import { Nuxt, TSReference } from '@nuxt/kit'
import type { TSConfig } from 'pkg-types'
import consola from 'consola'
import { getModulePaths, getNearestPackage } from './cjs'

export const writeTypes = async (nuxt: Nuxt) => {
const modulePaths = getModulePaths(nuxt.options.modulesDir)
const rootDir = nuxt.options.rootDir

const tsConfig: TSConfig = {
compilerOptions: {
target: 'ESNext',
module: 'ESNext',
moduleResolution: 'Node',
strict: true,
allowJs: true,
noEmit: true,
resolveJsonModule: true,
types: ['node'],
baseUrl: relative(nuxt.options.buildDir, nuxt.options.rootDir),
paths: {}
}
}

const aliases = {
...nuxt.options.alias,
'#build': nuxt.options.buildDir
}

for (const alias in aliases) {
const relativePath = relative(nuxt.options.rootDir, aliases[alias]).replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ || '.'
tsConfig.compilerOptions.paths[alias] = [relativePath]

try {
const { isDirectory } = await fsp.stat(resolve(nuxt.options.rootDir, relativePath))
if (isDirectory) {
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${relativePath}/*`]
}
} catch { }
}

const references: TSReference[] = [
...nuxt.options.buildModules,
...nuxt.options.modules,
Expand All @@ -20,7 +53,7 @@ export const writeTypes = async (nuxt: Nuxt) => {
const declarations: string[] = []

await nuxt.callHook('builder:generateApp')
await nuxt.callHook('prepare:types', { references, declarations })
await nuxt.callHook('prepare:types', { references, declarations, tsConfig })

const declarationPath = resolve(`${rootDir}/nuxt.d.ts`)

Expand All @@ -42,6 +75,10 @@ export const writeTypes = async (nuxt: Nuxt) => {
await fsp.writeFile(declarationPath, declaration)

consola.success('Generated', cyan(relative(process.cwd(), declarationPath)))

const tsConfigPath = resolve(nuxt.options.buildDir, 'tsconfig.json')
await fsp.mkdir(nuxt.options.buildDir, { recursive: true })
await fsp.writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2))
}

function renderAttrs (obj: Record<string, string>) {
Expand Down
3 changes: 3 additions & 0 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,7 @@ __metadata:
lodash.template: ^4.5.0
mlly: ^0.2.6
pathe: ^0.2.0
pkg-types: ^0.1.3
rc9: ^1.2.0
scule: ^0.2.1
semver: ^7.3.5
Expand Down Expand Up @@ -13679,6 +13680,7 @@ fsevents@~2.3.2:
mri: ^1.2.0
p-debounce: ^4.0.0
pathe: ^0.2.0
pkg-types: ^0.1.3
scule: ^0.2.1
superb: ^4.0.0
unbuild: latest
Expand Down Expand Up @@ -14568,6 +14570,13 @@ fsevents@~2.3.2:
languageName: node
linkType: hard

"pkg-types@npm:^0.1.3":
version: 0.1.3
resolution: "pkg-types@npm:0.1.3"
checksum: 3d1d9c9c39e8ed5299dbd1e729bca16154ae59b2dfe3703a8f24b1fe58280662663f7531b48a431feeecf4b5a7785a42f37cfe1541ff612ec6394bc5a1f61060
languageName: node
linkType: hard

"pkg-up@npm:^2.0.0":
version: 2.0.0
resolution: "pkg-up@npm:2.0.0"
Expand Down

0 comments on commit 31b12d0

Please sign in to comment.