Unverified Commit f055b167 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add TypeScript plugin (#41569)

This PR implements the TypeScript plugin (only applied to the app directory), and it will be automatically added to tsconfig.json when running the CLI. Due to how TS plugins work (a package name is required to specify), hence we can't use a subpath import like `next/typescript` for the plugin. To avoid installing a separate dependency, here I propose to reuse the `next` package: if the default export is called with the TypeScript option, we can say it's being used as a plugin. There is also environment variables we can check here, however it's specifically for VS Code (`VSCODE_CLI=1`) where this plugin should be working with other editors too.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent fe762d32
......@@ -187,6 +187,27 @@ export async function writeConfigurationDefaults(
)
}
// Enable the Next.js typescript plugin.
if (isAppDirEnabled) {
if (userTsConfig.compilerOptions) {
if (!('plugins' in userTsConfig.compilerOptions)) {
userTsConfig.compilerOptions.plugins = []
}
if (
!userTsConfig.compilerOptions.plugins.some(
(plugin: { name: string }) => plugin.name === 'next'
)
) {
userTsConfig.compilerOptions.plugins.push({ name: 'next' })
suggestedActions.push(
chalk.cyan('plugins') +
' was updated to add ' +
chalk.bold(`{ name: 'next' }`)
)
}
}
}
if (!('exclude' in rawConfig)) {
userTsConfig.exclude = ['node_modules']
suggestedActions.push(
......
import chalk from 'next/dist/compiled/chalk'
import path from 'path'
import {
hasNecessaryDependencies,
NecessaryDependencies,
......
This diff is collapsed.
......@@ -182,6 +182,15 @@ export class NextServer {
// This file is used for when users run `require('next')`
function createServer(options: NextServerOptions): NextServer {
// The package is used as a TypeScript plugin.
if (
options &&
'typescript' in options &&
'version' in (options as any).typescript
) {
return require('./next-typescript').createTSPlugin(options)
}
if (options == null) {
throw new Error(
'The server has not been instantiated properly. https://nextjs.org/docs/messages/invalid-server-options'
......
/* eslint-disable */
// 'use client'
import { useCallback, useState } from 'react'
export default function Page() {
return <div>hello</div>
}
export const config = {
runtime: 'nodejs',
}
export const runtime = 'nodejs'
export const dynamic = 'auto',
revalidate = 100,
fetchCache = 'force-no-store',
preferredRegion = 'auto'
export const dynamicParams = true
/* eslint-disable */
export const config = {
revalidate: 0,
}
// export const revalidate = -1
export const revalidate = -1
export default function Root({ children }) {
return (
......
......@@ -8,5 +8,8 @@
},
"keywords": [],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"typescript": "4.8.2"
}
}
/* eslint-disable */
import { useState } from 'react'
export default function Page() {
return <div>hello</div>
}
......@@ -13,7 +13,12 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment