Unverified Commit 6599f1dc authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

provide declarations for server-only/client-only (#80361)

We implement the behavior of `import 'server-only'` and `import
'client-only'` on the compiler level and thus don't require having them
installed as dependencies. By default it works fine with typescript,
because (surprisingly) TSC *doesn't check side-effecting imports*.
But this behavior can be overridden with
[`noUncheckedSideEffectImports`](https://www.typescriptlang.org/tsconfig/#noUncheckedSideEffectImports)
which'd cause `import 'server-only'` to start erroring. To prevent that,
we add declarations for them in next's global type declarations.
parent 94979526
...@@ -43,6 +43,30 @@ declare module '*.module.scss' { ...@@ -43,6 +43,30 @@ declare module '*.module.scss' {
export default classes export default classes
} }
// We implement the behavior of `import 'server-only'` and `import 'client-only'` on the compiler level
// and thus don't require having them installed as dependencies.
// By default it works fine with typescript, because (surprisingly) TSC *doesn't check side-effecting imports*.
// But this behavior can be overridden with `noUncheckedSideEffectImports`
// (https://www.typescriptlang.org/tsconfig/#noUncheckedSideEffectImports)
// which'd cause `import 'server-only'` to start erroring.
// To prevent that, we add declarations for them here.
declare module 'server-only' {
/**
* `import 'server-only'` marks your module as only usable on the server
* and prevents it from being used on the client.
* Read more: https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning
*/
}
declare module 'client-only' {
/**
* `import 'client-only'` marks your module as only usable on the client
* and prevents it from being used on the server.
* Read more: https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning
*/
}
interface Window { interface Window {
MSInputMethodContext?: unknown MSInputMethodContext?: unknown
/** @internal */ /** @internal */
......
# tsconfig.json in tests is excluded by default, but we customize it for this test, so it needs to be included
!tsconfig.json
\ No newline at end of file
'use client'
import 'client-only'
export default function Page() {
return <>Hello</>
}
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import 'server-only'
export default function Page() {
return <>Hello</>
}
import { nextTestSetup } from 'e2e-utils'
describe('Imports of server/client-only with noUncheckedSideEffectImports', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true, // No need to run this in deployment mode.
skipStart: true,
})
if (skipped) return
let buildResult: Awaited<ReturnType<(typeof next)['build']>>
beforeAll(async () => {
buildResult = await next.build()
})
it('Should build without typescript errors', async () => {
// If something's wrong with our declarations of these modules, TSC will error with:
// `Type error: Cannot find module 'server-only' or its corresponding type declarations.`
expect(buildResult.cliOutput).not.toContain('server-only')
expect(buildResult.cliOutput).not.toContain('client-only')
expect(buildResult.exitCode).toBe(0)
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
{
"compilerOptions": {
// the only change from the default tsconfig.
// this forces `import 'server-only'` and `import 'client-only'` to be checked.
"noUncheckedSideEffectImports": true,
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "**/*.test.ts", "**/*.test.tsx"]
}
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