From 87acfd8637116c72fd22118265444e7aecbea780 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 10 Aug 2022 14:05:18 -0500 Subject: [PATCH 1/2] Tweak styled-jsx type declarations --- packages/next/taskfile.js | 17 +++++++++++++++-- .../typescript-basic/app/pages/index.tsx | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/next/taskfile.js b/packages/next/taskfile.js index 67f660129e6e..b38d7f37756a 100644 --- a/packages/next/taskfile.js +++ b/packages/next/taskfile.js @@ -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 += `/// \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') diff --git a/test/production/typescript-basic/app/pages/index.tsx b/test/production/typescript-basic/app/pages/index.tsx index 85950f2e3452..ce26e3d47855 100644 --- a/test/production/typescript-basic/app/pages/index.tsx +++ b/test/production/typescript-basic/app/pages/index.tsx @@ -1,6 +1,7 @@ import { useRouter } from 'next/router' import Link from 'next/link' import { type PageConfig } from 'next' +import { StyleRegistry, createStyleRegistry } from 'styled-jsx' export const config: PageConfig = {} From e75a029e730db843f8bccb0d37850bbb9bae3e41 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 10 Aug 2022 14:13:44 -0500 Subject: [PATCH 2/2] fix lint --- test/production/typescript-basic/app/pages/index.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/test/production/typescript-basic/app/pages/index.tsx b/test/production/typescript-basic/app/pages/index.tsx index ce26e3d47855..e974da66bbaf 100644 --- a/test/production/typescript-basic/app/pages/index.tsx +++ b/test/production/typescript-basic/app/pages/index.tsx @@ -1,6 +1,7 @@ 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 = {}