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

Tweak styled-jsx type declarations #39474

Merged
merged 3 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/next/taskfile.js
Expand Up @@ -57,18 +57,31 @@ export async function copy_styled_jsx_assets(task, opts) {
// dev dep `styled-jsx` and `next/dist/styled-jsx` for duplicated declare modules
const typesDir = join(outputDir, 'types')
let typeReferences = ''
let globalTypesContent = ''

await fs.ensureDir(outputDir)
await fs.ensureDir(typesDir)

for (const file of typeFiles) {
const fileNoExt = file.replace(/\.d\.ts/, '')
const content = await fs.readFile(join(styledJsxPath, file), 'utf8')
let content = await fs.readFile(join(styledJsxPath, file), 'utf8')

if (file === 'index.d.ts') {
const styledJsxIdx = content.indexOf(`declare module 'styled-jsx' {`)
globalTypesContent = content.substring(0, styledJsxIdx)
content = content
.substring(styledJsxIdx)
.replace('React.', `import('react').`)
}

await fs.writeFile(join(typesDir, file), content)
typeReferences += `/// <reference types="./${fileNoExt}" />\n`
}

await fs.writeFile(join(typesDir, 'global.d.ts'), typeReferences)
await fs.writeFile(
join(typesDir, 'global.d.ts'),
`${typeReferences}\n${globalTypesContent}`
)

for (const file of jsFiles) {
const content = await fs.readFile(join(styledJsxPath, file), 'utf8')
Expand Down
2 changes: 2 additions & 0 deletions test/production/typescript-basic/app/pages/index.tsx
@@ -1,6 +1,8 @@
import { useRouter } from 'next/router'
import Link from 'next/link'
import { type PageConfig } from 'next'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'

export const config: PageConfig = {}

Expand Down