Skip to content

Commit

Permalink
fix: require index.js or index.ts as entrypoint for the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Nov 2, 2022
1 parent 1ce9a28 commit 7c25507
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/actions/verify-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
validateScripts,
validateTsConfig,
validateSanityDependencies,
validateSrcIndexFile,
} from './verify/validations'
import {PackageJson, TsConfig} from './verify/types'
import chalk from 'chalk'
Expand All @@ -37,6 +38,7 @@ export async function verifyPackage({basePath, flags}: {basePath: string; flags:

await validation('packageName', async () => validatePackageName(packageJson))
await validation('pkg-utils', async () => validatePkgUtilsDependency(packageJson))
await validation('srcIndex', async () => validateSrcIndexFile(basePath))
await validation('scripts', async () => validateScripts(packageJson))
await validation('module', async () => validateModule(packageJson))
await validation('nodeEngine', async () => validateNodeEngine(packageJson))
Expand Down
22 changes: 22 additions & 0 deletions src/actions/verify/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,25 @@ export function validatePackageName(packageJson: PackageJson) {
]
}
}

export async function validateSrcIndexFile(basePath: string) {
const paths = ['index.js', 'index.ts'].map((p) => path.join('src', p))
const allowedIndexFiles = paths.map((file) => path.join(basePath, file))

let hasIndex = false
for (const indexFile of allowedIndexFiles) {
hasIndex = hasIndex || (await fileExists(indexFile))
}
if (!hasIndex) {
return [
outdent`
Expected one of [${paths.join(', ')}] to exist.
@sanity/pkg-utils expects a non-jsx file to be the source entry-point for the plugin.
If you currently have JSX in your index file, extract it into a separate file and import it.
`,
]
}

return []
}
1 change: 1 addition & 0 deletions src/actions/verify/verify-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const verifyPackageConfigDefaults = {
'pkg-utils': true,
nodeEngine: true,
studioConfig: true,
srcIndex: true,
} as const

export type VerifyPackageConfig = Partial<Record<keyof typeof verifyPackageConfigDefaults, boolean>>
Expand Down
7 changes: 1 addition & 6 deletions src/npm/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,7 @@ export async function writePackageJson(data: PackageData, options: InjectOptions
license: license ? license.id : 'UNLICENSE',
}

let source = flags.typescript ? './src/index.ts' : './src/index.js'
// account for existing (j|t)sx
if (prev?.source?.endsWith('x')) {
source += 'x'
}

const source = flags.typescript ? './src/index.ts' : './src/index.js'
const manifest: PackageJson = {
...alwaysOnTop,
// Use already configured values by default
Expand Down

0 comments on commit 7c25507

Please sign in to comment.