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 2 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
23 changes: 18 additions & 5 deletions packages/next/src/lib/verify-typescript-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ const requiredPackages = [
pkg: 'typescript',
exportsRestrict: true,
},
{
file: '@types/react/index.d.ts',
pkg: '@types/react',
exportsRestrict: true,
},
{
file: '@types/node/index.d.ts',
pkg: '@types/node',
Expand Down Expand Up @@ -63,6 +58,24 @@ export async function verifyTypeScriptSetup({
return { version: null }
}

if (
(
await hasNecessaryDependencies(dir, [
{
file: 'react/index.js',
pkg: 'react',
exportsRestrict: false,
},
])
).resolved.has('react')
) {
requiredPackages.push({
file: '@types/react/index.d.ts',
pkg: '@types/react',
exportsRestrict: true,
})
}

// Ensure TypeScript and necessary `@types/*` are installed:
let deps: NecessaryDependencies = await hasNecessaryDependencies(
dir,
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('no-react', () => {
const { next } = nextTestSetup({
files: __dirname,
installCommand: 'pnpm remove react react-dom',
})

it('should work using cheerio without react, react-dom', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this test work? Shouldn't it just do a regular pnpm dev and then check no react or @types/react is in dependencies or devDependencies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we always install react and react-dom, this manually removes after install.

const finalDependencies = {
react: reactVersion,
'react-dom': reactVersion,
'@types/react': 'latest',
'@types/react-dom': 'latest',
typescript: 'latest',
'@types/node': 'latest',
...this.dependencies,
...this.packageJson?.dependencies,
}

Screenshot 2024-08-17 at 12 15 04 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But where in the assertion do I see that?

})
Loading