Skip to content

Commit

Permalink
Tweak styled-jsx type declarations (#39474)
Browse files Browse the repository at this point in the history
* Tweak styled-jsx type declarations

* fix lint
  • Loading branch information
ijjk committed Aug 10, 2022
1 parent f5cab2f commit c9aa2ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit c9aa2ea

Please sign in to comment.