Skip to content

Commit

Permalink
Validate @types/react by fs API as it has exports fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Apr 12, 2022
1 parent 91586b7 commit 6ccfdc6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
8 changes: 6 additions & 2 deletions packages/next/lib/eslint/runLintCheck.ts
Expand Up @@ -34,8 +34,12 @@ function isValidSeverity(severity: string): severity is Severity {
}

const requiredPackages = [
{ file: 'eslint', pkg: 'eslint' },
{ file: 'eslint-config-next', pkg: 'eslint-config-next' },
{ file: 'eslint', pkg: 'eslint', exportsRestrict: false },
{
file: 'eslint-config-next',
pkg: 'eslint-config-next',
exportsRestrict: false,
},
]

async function cliPrompt() {
Expand Down
28 changes: 26 additions & 2 deletions packages/next/lib/has-necessary-dependencies.ts
@@ -1,6 +1,10 @@
import { existsSync } from 'fs'
import { join, relative } from 'path'

export interface MissingDependency {
file: string
pkg: string
exportsRestrict: boolean
}

export type NecessaryDependencies = {
Expand All @@ -15,13 +19,33 @@ export async function hasNecessaryDependencies(
let resolutions = new Map<string, string>()
const missingPackages = requiredPackages.filter((p) => {
try {
resolutions.set(p.pkg, require.resolve(p.file, { paths: [baseDir] }))
if (p.exportsRestrict) {
const pkgPath = require.resolve(`${p.pkg}/package.json`, {
paths: [baseDir],
})
const fileNameToVerify = relative(p.pkg, p.file)
if (fileNameToVerify) {
const fileToVerify = join(pkgPath, '..', fileNameToVerify)
if (existsSync(fileToVerify)) {
resolutions.set(p.pkg, join(pkgPath, '..'))
} else {
return true
}
} else {
resolutions.set(p.pkg, pkgPath)
}
} else {
resolutions.set(p.pkg, require.resolve(p.file, { paths: [baseDir] }))
}
return false
} catch (_) {
} catch (e) {
console.error(e)
return true
}
})

console.log(resolutions)

return {
resolved: resolutions,
missing: missingPackages,
Expand Down
8 changes: 7 additions & 1 deletion packages/next/lib/verify-partytown-setup.ts
Expand Up @@ -61,7 +61,13 @@ export async function verifyPartytownSetup(
try {
const partytownDeps: NecessaryDependencies = await hasNecessaryDependencies(
dir,
[{ file: '@builder.io/partytown', pkg: '@builder.io/partytown' }]
[
{
file: '@builder.io/partytown',
pkg: '@builder.io/partytown',
exportsRestrict: false,
},
]
)

if (partytownDeps.missing?.length > 0) {
Expand Down
14 changes: 11 additions & 3 deletions packages/next/lib/verifyTypeScriptSetup.ts
Expand Up @@ -17,9 +17,17 @@ import { missingDepsError } from './typescript/missingDependencyError'
import { NextConfigComplete } from '../server/config-shared'

const requiredPackages = [
{ file: 'typescript', pkg: 'typescript' },
{ file: '@types/react/index.d.ts', pkg: '@types/react' },
{ file: '@types/node/index.d.ts', pkg: '@types/node' },
{ file: 'typescript', pkg: 'typescript', exportsRestrict: false },
{
file: '@types/react/index.d.ts',
pkg: '@types/react',
exportsRestrict: true,
},
{
file: '@types/node/index.d.ts',
pkg: '@types/node',
exportsRestrict: false,
},
]

export async function verifyTypeScriptSetup(
Expand Down

0 comments on commit 6ccfdc6

Please sign in to comment.