Unverified Commit 3e19ad5c authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

typing: upgrade styled-jsx to remove workaround in build script (#39408)

Improve styled-jsx types in next.js, previously there's no `declare module` for styled-jsx and there's type name conflicts in `styled-jsx/index.d.ts` and we use a rename hack to avoid the conflicts. Now styled-jsx 5.0.3 fixed those issues.

x-ref: https://github.com/vercel/styled-jsx/pull/805
parent d3bbfea1
/// <reference types="./types/global" /> /// <reference types="./types/global" />
/// <reference path="./dist/styled-jsx/global.d.ts" /> /// <reference path="./dist/styled-jsx/types/global.d.ts" />
/// <reference path="./amp.d.ts" /> /// <reference path="./amp.d.ts" />
/// <reference path="./app.d.ts" /> /// <reference path="./app.d.ts" />
/// <reference path="./config.d.ts" /> /// <reference path="./config.d.ts" />
......
...@@ -257,7 +257,7 @@ ...@@ -257,7 +257,7 @@
"string-hash": "1.1.3", "string-hash": "1.1.3",
"string_decoder": "1.3.0", "string_decoder": "1.3.0",
"strip-ansi": "6.0.0", "strip-ansi": "6.0.0",
"styled-jsx": "5.0.2", "styled-jsx": "5.0.3",
"tar": "6.1.11", "tar": "6.1.11",
"taskr": "1.1.0", "taskr": "1.1.0",
"terser": "5.14.1", "terser": "5.14.1",
......
...@@ -44,7 +44,7 @@ export async function copy_regenerator_runtime(task, opts) { ...@@ -44,7 +44,7 @@ export async function copy_regenerator_runtime(task, opts) {
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
export async function copy_styled_jsx_assets(task, opts) { export async function copy_styled_jsx_assets(task, opts) {
// we copy the styled-jsx types so that we can reference them // we copy the styled-jsx assets and types so that we can reference them
// in the next-env.d.ts file so it doesn't matter if the styled-jsx // 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 // package is hoisted out of Next.js' node_modules or not
const styledJsxPath = dirname(require.resolve('styled-jsx/package.json')) const styledJsxPath = dirname(require.resolve('styled-jsx/package.json'))
...@@ -53,33 +53,22 @@ export async function copy_styled_jsx_assets(task, opts) { ...@@ -53,33 +53,22 @@ export async function copy_styled_jsx_assets(task, opts) {
cwd: styledJsxPath, cwd: styledJsxPath,
}) })
const outputDir = join(__dirname, 'dist/styled-jsx') const outputDir = join(__dirname, 'dist/styled-jsx')
let typeReferences = `` // Separate type files into different folders to avoid conflicts between
// dev dep `styled-jsx` and `next/dist/styled-jsx` for duplicated declare modules
const typesDir = join(outputDir, 'types')
let typeReferences = ''
await fs.ensureDir(outputDir) await fs.ensureDir(outputDir)
await fs.ensureDir(typesDir)
for (const file of typeFiles) { for (const file of typeFiles) {
const fileNoExt = file.replace(/\.d\.ts/, '') const fileNoExt = file.replace(/\.d\.ts/, '')
const content = await fs.readFile(join(styledJsxPath, file), 'utf8') const content = await fs.readFile(join(styledJsxPath, file), 'utf8')
const exportsIndex = content.indexOf('export') await fs.writeFile(join(typesDir, file), content)
let replacedContent =
`${content.substring(0, exportsIndex)}\n` +
`declare module 'styled-jsx${
file === 'index.d.ts' ? '' : '/' + fileNoExt
}' {
${content.substring(exportsIndex)}
}`
if (file === 'index.d.ts') {
replacedContent = replacedContent
.replace(/export function StyleRegistry/g, 'export function IRegistry')
.replace(/StyleRegistry/g, 'IStyleRegistry')
.replace(/IRegistry/g, 'Registry')
}
await fs.writeFile(join(outputDir, file), replacedContent)
typeReferences += `/// <reference types="./${fileNoExt}" />\n` typeReferences += `/// <reference types="./${fileNoExt}" />\n`
} }
await fs.writeFile(join(outputDir, 'global.d.ts'), typeReferences) await fs.writeFile(join(typesDir, 'global.d.ts'), typeReferences)
for (const file of jsFiles) { for (const file of jsFiles) {
const content = await fs.readFile(join(styledJsxPath, file), 'utf8') const content = await fs.readFile(join(styledJsxPath, file), 'utf8')
...@@ -1806,7 +1795,12 @@ export async function path_to_regexp(task, opts) { ...@@ -1806,7 +1795,12 @@ export async function path_to_regexp(task, opts) {
export async function precompile(task, opts) { export async function precompile(task, opts) {
await task.parallel( await task.parallel(
['browser_polyfills', 'path_to_regexp', 'copy_ncced'], [
'browser_polyfills',
'path_to_regexp',
'copy_ncced',
'copy_styled_jsx_assets',
],
opts opts
) )
} }
...@@ -1965,7 +1959,6 @@ export async function compile(task, opts) { ...@@ -1965,7 +1959,6 @@ export async function compile(task, opts) {
// we compile this each time so that fresh runtime data is pulled // we compile this each time so that fresh runtime data is pulled
// before each publish // before each publish
'ncc_amp_optimizer', 'ncc_amp_optimizer',
'copy_styled_jsx_assets',
], ],
opts opts
) )
......
...@@ -18,11 +18,6 @@ declare module 'next/dist/compiled/@next/react-refresh-utils/dist/ReactRefreshWe ...@@ -18,11 +18,6 @@ declare module 'next/dist/compiled/@next/react-refresh-utils/dist/ReactRefreshWe
export = m export = m
} }
declare module 'next/dist/styled-jsx' {
import m from 'styled-jsx'
export = m
}
declare module 'next/dist/compiled/node-html-parser' { declare module 'next/dist/compiled/node-html-parser' {
export * from 'node-html-parser' export * from 'node-html-parser'
} }
...@@ -289,6 +284,11 @@ declare module 'next/dist/compiled/postcss-scss' { ...@@ -289,6 +284,11 @@ declare module 'next/dist/compiled/postcss-scss' {
import m from 'postcss-scss' import m from 'postcss-scss'
export = m export = m
} }
declare module 'next/dist/styled-jsx' {
import m from 'styled-jsx'
export = m
}
declare module 'next/dist/compiled/text-table' { declare module 'next/dist/compiled/text-table' {
function textTable( function textTable(
rows: Array<Array<{}>>, rows: Array<Array<{}>>,
......
...@@ -561,7 +561,7 @@ importers: ...@@ -561,7 +561,7 @@ importers:
string_decoder: 1.3.0 string_decoder: 1.3.0
string-hash: 1.1.3 string-hash: 1.1.3
strip-ansi: 6.0.0 strip-ansi: 6.0.0
styled-jsx: 5.0.2 styled-jsx: 5.0.3
tar: 6.1.11 tar: 6.1.11
taskr: 1.1.0 taskr: 1.1.0
terser: 5.14.1 terser: 5.14.1
...@@ -750,7 +750,7 @@ importers: ...@@ -750,7 +750,7 @@ importers:
string_decoder: 1.3.0 string_decoder: 1.3.0
string-hash: 1.1.3 string-hash: 1.1.3
strip-ansi: 6.0.0 strip-ansi: 6.0.0
styled-jsx: 5.0.2_@babel+core@7.18.0 styled-jsx: 5.0.3_@babel+core@7.18.0
tar: 6.1.11 tar: 6.1.11
taskr: 1.1.0 taskr: 1.1.0
terser: 5.14.1 terser: 5.14.1
...@@ -20560,8 +20560,8 @@ packages: ...@@ -20560,8 +20560,8 @@ packages:
postcss-load-plugins: 2.3.0 postcss-load-plugins: 2.3.0
dev: true dev: true
/styled-jsx/5.0.2_@babel+core@7.18.0: /styled-jsx/5.0.3_@babel+core@7.18.0:
resolution: {integrity: sha512-LqPQrbBh3egD57NBcHET4qcgshPks+yblyhPlH2GY8oaDgKs8SK4C3dBh3oSJjgzJ3G5t1SYEZGHkP+QEpX9EQ==} resolution: {integrity: sha512-v82oihjxFj2WJtQiodZIDjJpnmVcE71HTCVylxdcQHU0ocnI0rGhJ0+5A3311NMQUx0KFJ+18RSHNlfIgcSU8g==}
engines: {node: '>= 12.0.0'} engines: {node: '>= 12.0.0'}
peerDependencies: peerDependencies:
'@babel/core': '*' '@babel/core': '*'
......
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