Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: auto-install @types/react only if react is installed #68928

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/next/src/lib/verify-typescript-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,31 @@ const requiredPackages = [
pkg: 'typescript',
exportsRestrict: true,
},
{
file: '@types/node/index.d.ts',
pkg: '@types/node',
exportsRestrict: true,
},
// Added react and react-dom here to check if
// @types/react and @types/react-dom are needed.
{
file: 'react/index.js',
pkg: 'react',
exportsRestrict: false,
},
{
file: 'react-dom/index.js',
pkg: 'react-dom',
exportsRestrict: false,
},
{
file: '@types/react/index.d.ts',
pkg: '@types/react',
exportsRestrict: true,
},
{
file: '@types/node/index.d.ts',
pkg: '@types/node',
file: '@types/react-dom/index.d.ts',
pkg: '@types/react-dom',
exportsRestrict: true,
},
]
Expand Down Expand Up @@ -91,6 +108,19 @@ export async function verifyTypeScriptSetup({
) +
'\n'
)

// TODO(jiwon): refactor to reduce loop
if (!deps.resolved.has('react')) {
deps.missing = deps.missing.filter(
(dep) => dep.pkg !== '@types/react' && dep.pkg !== 'react'
)
}
if (!deps.resolved.has('react-dom')) {
deps.missing = deps.missing.filter(
(dep) => dep.pkg !== '@types/react-dom' && dep.pkg !== 'react-dom'
)
}

await installDependencies(dir, deps.missing, true).catch((err) => {
if (err && typeof err === 'object' && 'command' in err) {
console.error(
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/app-dir/no-react/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/no-react/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/no-react/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
13 changes: 13 additions & 0 deletions test/e2e/app-dir/no-react/no-react.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { nextTestSetup } from 'e2e-utils'

describe('app-dir no-react', () => {
const { next } = nextTestSetup({
files: __dirname,
skippedDependencies: ['react', 'react-dom'],
})

it('should render without react, react-dom in App Router', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
})
6 changes: 6 additions & 0 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type PackageJson = {
export interface NextInstanceOpts {
files: FileRef | string | { [filename: string]: string | FileRef }
dependencies?: { [name: string]: string }
skippedDependencies?: string[]
resolutions?: { [name: string]: string }
packageJson?: PackageJson
nextConfig?: NextConfig
Expand Down Expand Up @@ -56,6 +57,7 @@ export class NextInstance {
protected buildCommand?: string
protected startCommand?: string
protected dependencies?: PackageJson['dependencies'] = {}
protected skippedDependencies?: string[] = []
protected resolutions?: PackageJson['resolutions']
protected events: { [eventName: string]: Set<any> } = {}
public testDir: string
Expand Down Expand Up @@ -175,6 +177,10 @@ export class NextInstance {
...this.packageJson?.dependencies,
}

for (const skippedDependency of this.skippedDependencies) {
delete finalDependencies[skippedDependency]
}

if (skipInstall || skipIsolatedNext) {
const pkgScripts = (this.packageJson['scripts'] as {}) || {}
await fs.mkdir(this.testDir, { recursive: true })
Expand Down
Loading