fix(exportable-manifest, types): add typesVersions field to PUBLISH_CONFIG_WHITELIST (#4637)

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
장지훈
2022-04-29 23:01:01 +09:00
committed by GitHub
parent af6ac00e4d
commit 18ba5e2c09
5 changed files with 90 additions and 36 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/exportable-manifest": patch
"@pnpm/types": patch
---
Add typesVersions to PUBLISH_CONFIG_WHITELIST

View File

@@ -3,30 +3,8 @@ import PnpmError from '@pnpm/error'
import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
import { Dependencies, ProjectManifest } from '@pnpm/types'
import fromPairs from 'ramda/src/fromPairs'
import isEmpty from 'ramda/src/isEmpty'
import omit from 'ramda/src/omit'
// property keys that are copied from publishConfig into the manifest
const PUBLISH_CONFIG_WHITELIST = new Set([
// manifest fields that may make sense to overwrite
'bin',
'type',
'imports',
// https://github.com/stereobooster/package.json#package-bundlers
'main',
'module',
'typings',
'types',
'exports',
'browser',
'esnext',
'es2015',
'unpkg',
'umd:main',
// These are useful to hide in order to avoid warnings during local development
'os',
'cpu',
])
import { overridePublishConfig } from './overridePublishConfig'
const PREPUBLISH_SCRIPTS = [
'prepublishOnly',
@@ -49,19 +27,7 @@ export default async function makePublishManifest (dir: string, originalManifest
}
}
const { publishConfig } = publishManifest
if (publishConfig != null) {
Object.keys(publishConfig)
.filter(key => PUBLISH_CONFIG_WHITELIST.has(key))
.forEach(key => {
publishManifest[key] = publishConfig[key]
delete publishConfig[key]
})
if (isEmpty(publishConfig)) {
delete publishManifest.publishConfig
}
}
overridePublishConfig(publishManifest)
if (opts?.readmeFile) {
publishManifest.readme ??= opts.readmeFile

View File

@@ -0,0 +1,42 @@
import { ProjectManifest } from '@pnpm/types'
import isEmpty from 'ramda/src/isEmpty'
// property keys that are copied from publishConfig into the manifest
const PUBLISH_CONFIG_WHITELIST = new Set([
// manifest fields that may make sense to overwrite
'bin',
'type',
'imports',
// https://github.com/stereobooster/package.json#package-bundlers
'main',
'module',
'typings',
'types',
'exports',
'browser',
'esnext',
'es2015',
'unpkg',
'umd:main',
// These are useful to hide in order to avoid warnings during local development
'os',
'cpu',
// https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions
'typesVersions',
])
export function overridePublishConfig (publishManifest: ProjectManifest): void {
const { publishConfig } = publishManifest
if (!publishConfig) return
Object.entries(publishConfig)
.filter(([key]) => PUBLISH_CONFIG_WHITELIST.has(key))
.forEach(([key, value]) => {
publishManifest[key] = value
delete publishConfig[key]
})
if (isEmpty(publishConfig)) {
delete publishManifest.publishConfig
}
}

View File

@@ -0,0 +1,31 @@
import { PackageManifest, PublishConfig } from '@pnpm/types'
import { overridePublishConfig } from '../lib/overridePublishConfig'
test('publish config to be overridden', async () => {
const publishConfig: PublishConfig = {
main: 'overridden',
types: 'overridden',
typesVersions: {
'*': {
'*': ['overridden'],
},
},
}
const publishManifest: PackageManifest = {
name: 'foo',
version: '1.0.0',
main: 'origin',
types: 'origin',
typesVersions: {
'*': {
'*': ['origin'],
},
},
publishConfig,
}
overridePublishConfig(publishManifest)
Object.keys(publishConfig).forEach((publishConfigKey) => {
expect(publishManifest[publishConfigKey]).toEqual(publishConfig[publishConfigKey])
})
})

View File

@@ -58,6 +58,14 @@ export interface PublishConfig extends Record<string, unknown> {
executableFiles?: string[]
}
type Version = string
type Pattern = string
export interface TypesVersions {
[version: Version]: {
[pattern: Pattern]: string[]
}
}
export interface BaseManifest {
name?: string
version?: string
@@ -90,6 +98,7 @@ export interface BaseManifest {
typings?: string
types?: string
publishConfig?: PublishConfig
typesVersions?: TypesVersions
readme?: string
keywords?: string[]
author?: string