@sanity/pkg-utils 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/README.md +8 -0
  2. package/dist/__printPackageTree-793a302b.cjs +75 -0
  3. package/dist/__printPackageTree-793a302b.cjs.map +1 -0
  4. package/dist/__resolveBuildContext-1b29cff2.cjs +336 -0
  5. package/dist/__resolveBuildContext-1b29cff2.cjs.map +1 -0
  6. package/dist/_build-08236974.cjs +169 -0
  7. package/dist/_build-08236974.cjs.map +1 -0
  8. package/dist/_buildAction-586dfb5f.cjs +43 -0
  9. package/dist/_buildAction-586dfb5f.cjs.map +1 -0
  10. package/dist/_check-9d180462.cjs +16 -0
  11. package/dist/_check-9d180462.cjs.map +1 -0
  12. package/dist/_index-c5bd1bc8.cjs +565 -0
  13. package/dist/_index-c5bd1bc8.cjs.map +1 -0
  14. package/dist/_statusAction-385b72dd.cjs +27 -0
  15. package/dist/_statusAction-385b72dd.cjs.map +1 -0
  16. package/dist/_watch-0623c005.cjs +217 -0
  17. package/dist/_watch-0623c005.cjs.map +1 -0
  18. package/dist/_watchAction-4b8d7301.cjs +42 -0
  19. package/dist/_watchAction-4b8d7301.cjs.map +1 -0
  20. package/dist/cli.cjs +14 -31
  21. package/dist/cli.cjs.map +1 -1
  22. package/dist/index.cjs +41 -16
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +278 -152
  25. package/dist/index.js +947 -5201
  26. package/dist/index.js.map +1 -1
  27. package/package.json +24 -9
  28. package/src/cli/buildAction.ts +8 -0
  29. package/src/cli/index.ts +41 -0
  30. package/src/cli/statusAction.ts +8 -0
  31. package/src/cli/watchAction.ts +8 -0
  32. package/src/{core → node/_core}/_defaults.ts +1 -3
  33. package/src/{core → node/_core}/_isRecord.ts +1 -3
  34. package/src/{core → node/_core}/config/_loadConfig.ts +1 -3
  35. package/src/node/_core/config/_resolveConfigProperty.ts +15 -0
  36. package/src/{core → node/_core}/config/defineConfig.ts +1 -3
  37. package/src/{core → node/_core}/config/index.ts +0 -0
  38. package/src/node/_core/config/types.ts +69 -0
  39. package/src/node/_core/contexts/buildContext.ts +34 -0
  40. package/src/node/_core/contexts/index.ts +1 -0
  41. package/src/{core → node/_core}/index.ts +2 -0
  42. package/src/node/_core/pkg/_loadPkg.ts +54 -0
  43. package/src/node/_core/pkg/_loadPkgWithReporting.ts +29 -0
  44. package/src/node/_core/pkg/_parseExports.ts +45 -0
  45. package/src/{core → node/_core}/pkg/_types.ts +16 -5
  46. package/src/{core → node/_core}/pkg/index.ts +1 -0
  47. package/src/{build/tasks/dts → node/_core/ts}/_loadTSConfig.ts +1 -0
  48. package/src/node/_core/ts/index.ts +1 -0
  49. package/src/node/_getFilesize.ts +8 -0
  50. package/src/node/_globFiles.ts +5 -0
  51. package/src/node/_printExtractMessages.ts +56 -0
  52. package/src/node/_printPackageTree.ts +72 -0
  53. package/src/{build/_parseWebTarget.ts → node/_resolveBrowserTarget.ts} +2 -2
  54. package/src/{build/_parseBrowserslistVersions.ts → node/_resolveBrowserslistVersions.ts} +1 -1
  55. package/src/node/_resolveBuildContext.ts +104 -0
  56. package/src/node/_resolveBuildTasks.ts +129 -0
  57. package/src/{build/_parseNodeTarget.ts → node/_resolveNodeTarget.ts} +2 -2
  58. package/src/node/_resolveWatchTasks.ts +129 -0
  59. package/src/node/_spinner.ts +26 -0
  60. package/src/node/_tasks/_types.ts +57 -0
  61. package/src/node/_tasks/dts/_DtsError.ts +11 -0
  62. package/src/{build/tasks → node/_tasks}/dts/_buildTypes.ts +20 -16
  63. package/src/node/_tasks/dts/_createApiExtractorConfig.ts +41 -0
  64. package/src/{build/tasks → node/_tasks}/dts/_createTSDocConfig.ts +2 -6
  65. package/src/node/_tasks/dts/_doExtract.ts +46 -0
  66. package/src/node/_tasks/dts/_dtsTask.ts +38 -0
  67. package/src/node/_tasks/dts/_dtsWatchTask.ts +43 -0
  68. package/src/node/_tasks/dts/_extractTypes.ts +161 -0
  69. package/src/node/_tasks/dts/_types.ts +25 -0
  70. package/src/node/_tasks/dts/index.ts +3 -0
  71. package/src/node/_tasks/index.ts +19 -0
  72. package/src/node/_tasks/rollup/_resolveRollupConfig.ts +132 -0
  73. package/src/node/_tasks/rollup/_rollupTask.ts +71 -0
  74. package/src/node/_tasks/rollup/_rollupWatchTask.ts +78 -0
  75. package/src/node/_tasks/rollup/index.ts +2 -0
  76. package/src/node/_watchConfigFiles.ts +52 -0
  77. package/src/node/_watchFiles.ts +29 -0
  78. package/src/node/build.ts +47 -0
  79. package/src/node/check.ts +14 -0
  80. package/src/node/index.ts +6 -0
  81. package/src/node/watch.ts +56 -0
  82. package/dist/_build-5bf997de.cjs +0 -5549
  83. package/dist/_build-5bf997de.cjs.map +0 -1
  84. package/src/build/_parseTasks.ts +0 -87
  85. package/src/build/_types.ts +0 -26
  86. package/src/build/build.ts +0 -101
  87. package/src/build/index.ts +0 -2
  88. package/src/build/tasks/_types.ts +0 -8
  89. package/src/build/tasks/dts/_createApiExtractorConfig.ts +0 -88
  90. package/src/build/tasks/dts/_extractTypes.ts +0 -100
  91. package/src/build/tasks/dts/dts.ts +0 -59
  92. package/src/build/tasks/dts/index.ts +0 -1
  93. package/src/build/tasks/extract/_extractTask.ts +0 -49
  94. package/src/build/tasks/extract/index.ts +0 -1
  95. package/src/build/tasks/index.ts +0 -4
  96. package/src/build/tasks/rollup/_resolveRollupConfig.ts +0 -95
  97. package/src/build/tasks/rollup/index.ts +0 -1
  98. package/src/build/tasks/rollup/rollup.ts +0 -96
  99. package/src/cli.ts +0 -25
  100. package/src/core/config/_resolveConfigProperty.ts +0 -14
  101. package/src/core/config/types.ts +0 -52
  102. package/src/core/pkg/_loadPkg.ts +0 -13
  103. package/src/core/pkg/_parseExports.ts +0 -40
  104. package/src/index.ts +0 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/pkg-utils",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "author": "Sanity.io <hello@sanity.io>",
5
5
  "license": "MIT",
6
6
  "description": "Simple utilities for modern npm packages.",
@@ -13,53 +13,67 @@
13
13
  "build",
14
14
  "bundling"
15
15
  ],
16
- "source": "./src/index.ts",
16
+ "source": "./src/node/index.ts",
17
17
  "main": "./dist/index.cjs",
18
18
  "module": "./dist/index.js",
19
19
  "types": "./dist/index.d.ts",
20
20
  "exports": {
21
21
  ".": {
22
- "default": "./dist/index.js",
22
+ "source": "./src/node/index.ts",
23
+ "import": "./dist/index.js",
23
24
  "require": "./dist/index.cjs",
24
25
  "types": "./dist/index.d.ts"
25
26
  }
26
27
  },
27
28
  "bin": {
29
+ "pkg": "./bin/pkg-utils",
28
30
  "pkg-utils": "./bin/pkg-utils"
29
31
  },
30
32
  "files": [
33
+ "bin",
31
34
  "src",
32
35
  "dist"
33
36
  ],
34
37
  "scripts": {
35
- "build": "node -r esbuild-register src/cli build --extract --tsconfig tsconfig.dist.json",
38
+ "build": "node -r esbuild-register scripts/build",
39
+ "extract": "sanity-tsdoc etl --outDir etc/api/@sanity/pkg-utils --tsconfig tsconfig.dist.json",
36
40
  "format": "prettier --write --cache --ignore-unknown .",
37
41
  "lint": "eslint . --ext .cjs,.js,.ts",
38
42
  "prebuild": "rimraf dist",
39
43
  "prepublishOnly": "pnpm run build",
40
44
  "release": "semantic-release",
41
- "test": "vitest run",
42
- "type-check": "tsc --noEmit"
45
+ "test": "node -r esbuild-register scripts/status && vitest run",
46
+ "type-check": "tsc --build",
47
+ "watch": "node -r esbuild-register scripts/watch"
43
48
  },
44
49
  "dependencies": {
45
50
  "@microsoft/api-extractor": "^7.29.5",
46
51
  "@microsoft/tsdoc-config": "^0.16.1",
52
+ "@rollup/plugin-alias": "^3.1.9",
47
53
  "@rollup/plugin-commonjs": "^22.0.2",
48
54
  "@rollup/plugin-json": "^4.1.0",
49
55
  "@rollup/plugin-node-resolve": "^13.3.0",
50
- "@sanity/tsdoc-to-portable-text": "^0.4.1",
56
+ "@rollup/plugin-replace": "^4.0.0",
51
57
  "browserslist": "^4.21.3",
52
58
  "cac": "^6.7.12",
53
59
  "chalk": "^4.1.2",
60
+ "chokidar": "^3.5.3",
54
61
  "esbuild": "^0.15.5",
55
62
  "esbuild-register": "^3.3.3",
56
63
  "find-config": "^1.0.0",
64
+ "globby": "^12.2.0",
57
65
  "jsonc-parser": "^3.1.0",
58
66
  "mkdirp": "^1.0.4",
67
+ "ora": "^5.4.1",
59
68
  "pkg-up": "^3.1.0",
69
+ "prettier": "^2.7.1",
70
+ "pretty-bytes": "^5.6.0",
60
71
  "rimraf": "^3.0.2",
61
72
  "rollup": "^2.78.1",
62
- "rollup-plugin-esbuild": "^4.9.3"
73
+ "rollup-plugin-esbuild": "^4.9.3",
74
+ "rxjs": "^7.5.6",
75
+ "treeify": "^1.1.0",
76
+ "zod": "^3.18.0"
63
77
  },
64
78
  "devDependencies": {
65
79
  "@commitlint/cli": "^17.0.3",
@@ -69,8 +83,10 @@
69
83
  "@types/find-config": "^1.0.1",
70
84
  "@types/mkdirp": "^1.0.2",
71
85
  "@types/node": "^18.7.13",
86
+ "@types/prettier": "^2.7.0",
72
87
  "@types/rimraf": "^3.0.2",
73
88
  "@types/tmp": "^0.2.3",
89
+ "@types/treeify": "^1.0.0",
74
90
  "@typescript-eslint/eslint-plugin": "^5.35.1",
75
91
  "@typescript-eslint/parser": "^5.35.1",
76
92
  "commitizen": "^4.2.5",
@@ -81,7 +97,6 @@
81
97
  "eslint-plugin-prettier": "^4.2.1",
82
98
  "husky": "^8.0.1",
83
99
  "lint-staged": "^12.5.0",
84
- "prettier": "^2.7.1",
85
100
  "semantic-release": "^19.0.5",
86
101
  "tmp": "^0.2.1",
87
102
  "ts-node": "^10.9.1",
@@ -0,0 +1,8 @@
1
+ import {build} from '../node/build'
2
+
3
+ export async function buildAction(options: {tsconfig?: string}): Promise<void> {
4
+ return await build({
5
+ cwd: process.cwd(),
6
+ tsconfig: options.tsconfig,
7
+ })
8
+ }
@@ -0,0 +1,41 @@
1
+ import cac from 'cac'
2
+ import {version} from '../../package.json'
3
+
4
+ const cli = cac()
5
+
6
+ // default commands checks package status
7
+ cli
8
+ .command('', 'Status')
9
+ .option('--tsconfig [tsconfig]', '[string] tsconfig.json')
10
+ .action(async (options) => {
11
+ const {statusAction} = await import('./statusAction')
12
+
13
+ return statusAction(options)
14
+ })
15
+
16
+ cli
17
+ .command('build', 'Build package')
18
+ .option('--tsconfig [tsconfig]', '[string] tsconfig.json')
19
+ .action(async (options) => {
20
+ const {buildAction} = await import('./buildAction')
21
+
22
+ return buildAction(options)
23
+ })
24
+
25
+ cli
26
+ .command('watch', 'Watch package')
27
+ .option('--tsconfig [tsconfig]', '[string] tsconfig.json')
28
+ .action(async (options) => {
29
+ const {watchAction} = await import('./watchAction')
30
+
31
+ return watchAction(options)
32
+ })
33
+
34
+ // Display help message when `-h` or `--help` appears
35
+ cli.help()
36
+
37
+ // Display version number when `-v` or `--version` appears
38
+ // It's also used in help message
39
+ cli.version(version)
40
+
41
+ cli.parse()
@@ -0,0 +1,8 @@
1
+ import {check} from '../node/check'
2
+
3
+ export async function statusAction(options: {tsconfig?: string}): Promise<void> {
4
+ return await check({
5
+ cwd: process.cwd(),
6
+ tsconfig: options.tsconfig,
7
+ })
8
+ }
@@ -0,0 +1,8 @@
1
+ import {watch} from '../node/watch'
2
+
3
+ export async function watchAction(options: {tsconfig?: string}): Promise<void> {
4
+ return await watch({
5
+ cwd: process.cwd(),
6
+ tsconfig: options.tsconfig,
7
+ })
8
+ }
@@ -1,6 +1,4 @@
1
- /**
2
- * @internal
3
- */
1
+ /** @internal */
4
2
  export const _DEFAULTS = {
5
3
  browserslist: [
6
4
  '> 0.2% and supports es6-module and supports es6-module-dynamic-import',
@@ -1,6 +1,4 @@
1
- /**
2
- * @internal
3
- */
1
+ /** @internal */
4
2
  export function _isRecord(value: unknown): value is Record<string, unknown> {
5
3
  return Boolean(value) && !Array.isArray(value) && typeof value === 'object'
6
4
  }
@@ -4,9 +4,7 @@ import findConfig from 'find-config'
4
4
  import pkgUp from 'pkg-up'
5
5
  import {PkgConfigOptions} from './types'
6
6
 
7
- /**
8
- * @internal
9
- */
7
+ /** @internal */
10
8
  export async function _loadConfig(options: {cwd: string}): Promise<PkgConfigOptions | undefined> {
11
9
  const {cwd} = options
12
10
 
@@ -0,0 +1,15 @@
1
+ import {PkgConfigProperty, PkgConfigPropertyResolver} from './types'
2
+
3
+ /** @internal */
4
+ export function _resolveConfigProperty<T>(
5
+ prop: PkgConfigProperty<T> | undefined,
6
+ initialValue: T
7
+ ): T {
8
+ if (!prop) return initialValue
9
+
10
+ if (typeof prop === 'function') {
11
+ return (prop as PkgConfigPropertyResolver<T>)(initialValue)
12
+ }
13
+
14
+ return prop
15
+ }
@@ -1,8 +1,6 @@
1
1
  import {PkgConfigOptions} from './types'
2
2
 
3
- /**
4
- * @public
5
- */
3
+ /** @public */
6
4
  export function defineConfig(configOptions: PkgConfigOptions): PkgConfigOptions {
7
5
  return configOptions
8
6
  }
File without changes
@@ -0,0 +1,69 @@
1
+ /** @public */
2
+ export type PkgFormat = 'commonjs' | 'esm'
3
+
4
+ /** @public */
5
+ export type PkgRuntime = '*' | 'browser' | 'node'
6
+
7
+ /** @public */
8
+ export interface PkgExport {
9
+ /** @internal */
10
+ _exported?: boolean
11
+ browser?: {
12
+ source?: string
13
+ import?: string
14
+ require?: string
15
+ }
16
+ // electron?: {
17
+ // node?: string
18
+ // default?: string
19
+ // }
20
+ import?: string
21
+ require?: string
22
+ source: string
23
+ types?: string
24
+ }
25
+
26
+ /** @public */
27
+ export type PkgConfigPropertyResolver<T> = (prev: T) => T
28
+
29
+ /** @public */
30
+ export type PkgConfigProperty<T> = PkgConfigPropertyResolver<T> | T
31
+
32
+ /** @public */
33
+ export interface PkgExports {
34
+ [path: string]: PkgExport
35
+ }
36
+
37
+ /** @public */
38
+ export type PkgRuleLevel = 'error' | 'warn' | 'info' | 'off'
39
+
40
+ /** @public */
41
+ export interface PkgConfigOptions {
42
+ /**
43
+ * Directory of distributed & bundled files.
44
+ */
45
+ dist?: string
46
+ exports?: PkgConfigProperty<PkgExports>
47
+ extract?: {
48
+ rules?: {
49
+ 'ae-forgotten-export'?: PkgRuleLevel
50
+ 'ae-incompatible-release-tags'?: PkgRuleLevel
51
+ 'ae-internal-missing-underscore'?: PkgRuleLevel
52
+ 'ae-missing-release-tag'?: PkgRuleLevel
53
+ }
54
+ }
55
+ /**
56
+ * Packages to exclude from the bundles.
57
+ */
58
+ external?: PkgConfigProperty<string[]>
59
+ minify?: boolean
60
+ /**
61
+ * Default runtime of package exports
62
+ */
63
+ runtime?: PkgRuntime
64
+ sourcemap?: boolean
65
+ /**
66
+ * Directory of source files.
67
+ */
68
+ src?: string
69
+ }
@@ -0,0 +1,34 @@
1
+ import ts from 'typescript'
2
+ import {PkgExports, PkgRuntime, PkgConfigOptions} from '../config'
3
+ import {_PackageJSON} from '../pkg'
4
+
5
+ /** @internal */
6
+ export interface _BuildFile {
7
+ type: 'asset' | 'chunk' | 'types'
8
+ path: string
9
+ }
10
+
11
+ /** @internal */
12
+ export interface _BuildContext {
13
+ config?: PkgConfigOptions
14
+ cwd: string
15
+ dist: string
16
+ exports: PkgExports | undefined
17
+ external: string[]
18
+ files: _BuildFile[]
19
+ logger: {
20
+ log: (...args: unknown[]) => void
21
+ info: (...args: unknown[]) => void
22
+ warn: (...args: unknown[]) => void
23
+ error: (...args: unknown[]) => void
24
+ success: (...args: unknown[]) => void
25
+ }
26
+ pkg: _PackageJSON
27
+ runtime: PkgRuntime
28
+ src: string
29
+ target: Record<PkgRuntime, string[]>
30
+ ts: {
31
+ config?: ts.ParsedCommandLine
32
+ configPath?: string
33
+ }
34
+ }
@@ -0,0 +1 @@
1
+ export * from './buildContext'
@@ -1,4 +1,6 @@
1
1
  export * from './_defaults'
2
2
  export * from './_isRecord'
3
3
  export * from './config'
4
+ export * from './contexts'
4
5
  export * from './pkg'
6
+ export * from './ts'
@@ -0,0 +1,54 @@
1
+ import fs from 'fs/promises'
2
+ import pkgUp from 'pkg-up'
3
+ import {z} from 'zod'
4
+ import {_PackageJSON} from './_types'
5
+
6
+ const pkgSchema = z.object({
7
+ type: z.optional(z.enum(['module'])),
8
+ name: z.string(),
9
+ version: z.string(),
10
+ bin: z.optional(z.record(z.string())),
11
+ dependencies: z.optional(z.record(z.string())),
12
+ devDependencies: z.optional(z.record(z.string())),
13
+ peerDependencies: z.optional(z.record(z.string())),
14
+ source: z.string(),
15
+ main: z.optional(z.string()),
16
+ browser: z.optional(z.record(z.string())),
17
+ module: z.optional(z.string()),
18
+ exports: z.optional(
19
+ z.record(
20
+ z.object({
21
+ source: z.string(),
22
+ browser: z.optional(
23
+ z.object({
24
+ require: z.optional(z.string()),
25
+ import: z.optional(z.string()),
26
+ })
27
+ ),
28
+ node: z.optional(
29
+ z.object({
30
+ require: z.optional(z.string()),
31
+ import: z.optional(z.string()),
32
+ })
33
+ ),
34
+ require: z.optional(z.string()),
35
+ import: z.optional(z.string()),
36
+ types: z.optional(z.string()),
37
+ })
38
+ )
39
+ ),
40
+ browserslist: z.optional(z.array(z.string())),
41
+ })
42
+
43
+ /** @internal */
44
+ export async function _loadPkg(options: {cwd: string}): Promise<_PackageJSON> {
45
+ const {cwd} = options
46
+
47
+ const pkgPath = await pkgUp({cwd})
48
+
49
+ if (!pkgPath) throw new Error('no package.json found')
50
+
51
+ const buf = await fs.readFile(pkgPath)
52
+
53
+ return pkgSchema.parse(JSON.parse(buf.toString()))
54
+ }
@@ -0,0 +1,29 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ import {ZodError} from 'zod'
5
+ import {_loadPkg} from './_loadPkg'
6
+ import {_PackageJSON} from './_types'
7
+
8
+ /** @internal */
9
+ export async function _loadPkgWithReporting(options: {cwd: string}): Promise<_PackageJSON> {
10
+ try {
11
+ return await _loadPkg(options)
12
+ } catch (err) {
13
+ if (err instanceof ZodError) {
14
+ for (const issue of err.issues) {
15
+ if (issue.code === 'invalid_type') {
16
+ console.log(
17
+ `${chalk.red('invalid type')} in ./package.json at ${chalk.magenta(
18
+ `\`${issue.path.join('')}\``
19
+ )} (expected ${issue.expected}, received ${issue.received})`
20
+ )
21
+ }
22
+ }
23
+ } else {
24
+ console.error(err)
25
+ }
26
+
27
+ process.exit(1)
28
+ }
29
+ }
@@ -0,0 +1,45 @@
1
+ import {_isRecord} from '../_isRecord'
2
+ import {PkgExport} from '../config'
3
+ import {_PackageJSON} from './_types'
4
+
5
+ /** @internal */
6
+ export function _parseExports(options: {pkg: _PackageJSON}): (PkgExport & {_path: string})[] {
7
+ const {pkg} = options
8
+
9
+ const rootExport: PkgExport & {_path: string} = {
10
+ _exported: true,
11
+ _path: '.',
12
+ browser: pkg.browser && {
13
+ require: pkg.main && pkg.browser[pkg.main],
14
+ import: pkg.module && pkg.browser[pkg.module],
15
+ },
16
+ source: pkg.source || 'index.js',
17
+ require: pkg.main,
18
+ import: pkg.module,
19
+ types: pkg.types,
20
+ }
21
+
22
+ const extraExports: (PkgExport & {_path: string})[] = []
23
+
24
+ if (pkg.exports) {
25
+ for (const [exportPath, exportEntry] of Object.entries(pkg.exports)) {
26
+ if (_isRecord(exportEntry)) {
27
+ if (exportPath === '.') {
28
+ Object.assign(rootExport, exportEntry)
29
+ } else {
30
+ const extraExport: PkgExport & {_path: string} = {
31
+ _exported: true,
32
+ _path: exportPath,
33
+ ...exportEntry,
34
+ }
35
+
36
+ extraExports.push(extraExport)
37
+ }
38
+ } else {
39
+ throw new Error('string entries are not supported')
40
+ }
41
+ }
42
+ }
43
+
44
+ return [rootExport, ...extraExports]
45
+ }
@@ -1,6 +1,4 @@
1
- /**
2
- * @internal
3
- */
1
+ /** @internal */
4
2
  export interface _PackageJSON {
5
3
  type?: 'module'
6
4
  version: string
@@ -10,9 +8,22 @@ export interface _PackageJSON {
10
8
  dependencies?: Record<string, string>
11
9
  devDependencies?: Record<string, string>
12
10
  peerDependencies?: Record<string, string>
13
- exports?: Record<string, string | Record<string, string>>
11
+ exports?: Record<
12
+ string,
13
+ {
14
+ source: string
15
+ browser?: {
16
+ require?: string
17
+ import?: string
18
+ }
19
+ require?: string
20
+ import?: string
21
+ types?: string
22
+ }
23
+ >
14
24
  main?: string
15
- source?: string
25
+ browser?: Record<string, string>
26
+ source: string
16
27
  module?: string
17
28
  types?: string
18
29
  browserslist?: string[]
@@ -1,3 +1,4 @@
1
1
  export * from './_loadPkg'
2
+ export * from './_loadPkgWithReporting'
2
3
  export * from './_parseExports'
3
4
  export * from './_types'
@@ -1,5 +1,6 @@
1
1
  import ts from 'typescript'
2
2
 
3
+ /** @internal */
3
4
  export async function _loadTSConfig(options: {
4
5
  cwd: string
5
6
  tsconfigPath: string
@@ -0,0 +1 @@
1
+ export * from './_loadTSConfig'
@@ -0,0 +1,8 @@
1
+ import {statSync} from 'fs'
2
+ import prettyBytes from 'pretty-bytes'
3
+
4
+ export function _getFilesize(file: string): string {
5
+ const stats = statSync(file)
6
+
7
+ return prettyBytes(stats.size)
8
+ }
@@ -0,0 +1,5 @@
1
+ import {globby} from 'globby'
2
+
3
+ export function _globFiles(patterns: string[]): Promise<string[]> {
4
+ return globby(patterns)
5
+ }
@@ -0,0 +1,56 @@
1
+ import path from 'path'
2
+ import {ExtractorMessage} from '@microsoft/api-extractor'
3
+ import chalk from 'chalk'
4
+ import {_BuildContext} from './_core'
5
+
6
+ export function _printExtractMessages(ctx: _BuildContext, messages: ExtractorMessage[]): void {
7
+ const {cwd, logger} = ctx
8
+
9
+ const warnings = messages.filter((msg) => msg.logLevel === 'warning')
10
+
11
+ if (warnings.length) {
12
+ logger.log()
13
+ }
14
+
15
+ for (const msg of warnings) {
16
+ const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath)
17
+
18
+ if (msg.messageId === 'TS6307') {
19
+ continue
20
+ }
21
+
22
+ logger.log(
23
+ [
24
+ `${chalk.cyan(sourceFilePath || '?')}`,
25
+ `:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
26
+ ` - ${chalk.yellow('warning')} ${chalk.gray(msg.messageId)}\n`,
27
+ msg.text,
28
+ '\n',
29
+ ].join('')
30
+ )
31
+ }
32
+
33
+ const errors: ExtractorMessage[] = messages.filter((msg) => msg.logLevel === 'error')
34
+
35
+ if (!warnings.length && errors.length) {
36
+ logger.log('')
37
+ }
38
+
39
+ for (const msg of errors) {
40
+ const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath)
41
+
42
+ logger.log(
43
+ [
44
+ `${chalk.cyan(sourceFilePath || '?')}`,
45
+ `:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
46
+ ` - ${chalk.red('error')} ${chalk.gray(msg.messageId)}\n`,
47
+ msg.text,
48
+ '\n',
49
+ ].join('')
50
+ )
51
+ }
52
+
53
+ if (errors.length) {
54
+ process.exit(1)
55
+ }
56
+ }
@@ -0,0 +1,72 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import chalk from 'chalk'
4
+ import treeify from 'treeify'
5
+ import {PkgExport, _BuildContext} from './_core'
6
+ import {_getFilesize} from './_getFilesize'
7
+
8
+ function _getFileExists(file: string) {
9
+ try {
10
+ fs.accessSync(file)
11
+
12
+ return true
13
+ } catch (_) {
14
+ return false
15
+ }
16
+ }
17
+
18
+ function _getFileInfo(cwd: string, filePath: string) {
19
+ const p = path.resolve(cwd, filePath)
20
+ const exists = _getFileExists(p)
21
+ const size = exists ? _getFilesize(p) : undefined
22
+
23
+ return {exists, size}
24
+ }
25
+
26
+ export function _printPackageTree(ctx: _BuildContext): void {
27
+ const {cwd, exports, logger, pkg} = ctx
28
+
29
+ if (!exports) return
30
+
31
+ logger.log(`${chalk.blue(pkg.name)}@${chalk.green(pkg.version)}`)
32
+
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ const tree: Record<string, any> = {}
35
+
36
+ if (pkg.bin) {
37
+ tree.bin = pkg.bin
38
+ }
39
+
40
+ function _fileInfo(file: string) {
41
+ const info = _getFileInfo(cwd, file)
42
+
43
+ if (!info.size) {
44
+ return chalk.gray(file)
45
+ }
46
+
47
+ return `${file} ${chalk.gray(info.size)}`
48
+ }
49
+
50
+ tree.exports = Object.fromEntries(
51
+ Object.entries(exports)
52
+ .filter(([, entry]) => entry._exported)
53
+ .map(([exportPath, entry]) => {
54
+ const exp: Omit<PkgExport, '_exported'> = {source: entry.source}
55
+
56
+ if (entry.browser) {
57
+ exp.browser = {}
58
+
59
+ if (entry.browser.import) exp.browser.import = _fileInfo(entry.browser.import)
60
+ if (entry.browser.require) exp.browser.import = _fileInfo(entry.browser.require)
61
+ }
62
+
63
+ if (entry.import) exp.import = _fileInfo(entry.import)
64
+ if (entry.require) exp.require = _fileInfo(entry.require)
65
+ if (entry.types) exp.types = _fileInfo(entry.types)
66
+
67
+ return [chalk.green(path.join(pkg.name, exportPath)), exp]
68
+ })
69
+ )
70
+
71
+ logger.log(treeify.asTree(tree, true, true))
72
+ }