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

Fix styled-jsx types when package is not hoisted #37902

Merged
merged 6 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 0 additions & 1 deletion examples/with-react-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@formatjs/cli": "4.2.31",
"@types/node": "16.6.2",
"@types/react": "17.0.19",
"@types/styled-jsx": "3.4.4",
"babel-plugin-formatjs": "10.3.5",
"eslint-plugin-formatjs": "2.17.4",
"typescript": "4.3.5"
Expand Down
2 changes: 2 additions & 0 deletions packages/next/lib/typescript/writeAppTypeDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export async function writeAppTypeDeclarations(
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/dist/styled-jsx-types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
Expand Down
33 changes: 33 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ export async function copy_regenerator_runtime(task, opts) {
.target('compiled/regenerator-runtime')
}

// eslint-disable-next-line camelcase
export async function copy_styled_jsx_types(task, opts) {
// we copy the styled-jsx types so that we can reference them
// in the next-env.d.ts file so it doesn't matter if the styled-jsx
// package is hoisted out of Next.js' node_modules or not
const styledJsxPath = dirname(require.resolve('styled-jsx/package.json'))
const typeFiles = glob.sync('*.d.ts', { cwd: styledJsxPath })
const outputDir = join(__dirname, 'dist/styled-jsx-types')
let typeReferences = ``

await fs.ensureDir(outputDir)

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

await fs.writeFile(
join(outputDir, file),
`${content.substring(0, exportsIndex)}\n` +
`declare module 'styled-jsx${
file === 'index.d.ts' ? '' : '/' + fileNoExt
}' {
${content.substring(exportsIndex)}
}`
)
typeReferences += `/// <reference types="./${fileNoExt}" />\n`
}

await fs.writeFile(join(outputDir, 'global.d.ts'), typeReferences)
}

const externals = {
// don't bundle caniuse-lite data so users can
// update it manually
Expand Down Expand Up @@ -1803,6 +1835,7 @@ export async function compile(task, opts) {
// we compile this each time so that fresh runtime data is pulled
// before each publish
'ncc_amp_optimizer',
'copy_styled_jsx_types',
],
opts
)
Expand Down
1 change: 0 additions & 1 deletion packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="node" />
/// <reference types="react" />
/// <reference types="react-dom" />
/// <reference types="styled-jsx" />

import React from 'react'
import { ParsedUrlQuery } from 'querystring'
Expand Down
1 change: 0 additions & 1 deletion test/production/typescript-basic/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('TypeScript basic', () => {
'@types/node': 'latest',
'@types/react': 'latest',
'@types/react-dom': 'latest',
'styled-jsx': 'latest',
},
})
})
Expand Down
6 changes: 6 additions & 0 deletions test/unit/write-app-declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('find config', () => {
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/dist/styled-jsx-types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
Expand All @@ -37,6 +39,8 @@ describe('find config', () => {
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/dist/styled-jsx-types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
Expand All @@ -58,6 +62,8 @@ describe('find config', () => {
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/dist/styled-jsx-types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
Expand Down