@sanity/pkg-utils 1.2.0 → 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 (105) 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 -30
  21. package/dist/cli.cjs.map +1 -1
  22. package/dist/index.cjs +42 -16
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +278 -154
  25. package/dist/index.js +965 -5129
  26. package/dist/index.js.map +1 -1
  27. package/package.json +25 -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/node/_core/_defaults.ts +9 -0
  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 +17 -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/node/_resolveBrowserTarget.ts +35 -0
  54. package/src/node/_resolveBrowserslistVersions.ts +60 -0
  55. package/src/node/_resolveBuildContext.ts +104 -0
  56. package/src/node/_resolveBuildTasks.ts +129 -0
  57. package/src/node/_resolveNodeTarget.ts +15 -0
  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-e608bb7c.cjs +0 -5458
  83. package/dist/_build-e608bb7c.cjs.map +0 -1
  84. package/src/build/_parseTasks.ts +0 -87
  85. package/src/build/_types.ts +0 -25
  86. package/src/build/build.ts +0 -82
  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/_defaults.ts +0 -9
  101. package/src/core/config/_resolveConfigProperty.ts +0 -12
  102. package/src/core/config/types.ts +0 -56
  103. package/src/core/pkg/_loadPkg.ts +0 -13
  104. package/src/core/pkg/_parseExports.ts +0 -40
  105. package/src/index.ts +0 -2
@@ -0,0 +1,35 @@
1
+ import {_PackageTargetVersions} from './_resolveBrowserslistVersions'
2
+
3
+ export function _resolveBrowserTarget(versions: _PackageTargetVersions): string[] | undefined {
4
+ const target: string[] = []
5
+
6
+ if (versions.chrome.length) {
7
+ target.push(`chrome${versions.chrome[0]}`)
8
+ }
9
+
10
+ if (versions.ff.length) {
11
+ target.push(`firefox${versions.ff[0]}`)
12
+ }
13
+
14
+ if (versions.edge.length) {
15
+ target.push(`edge${versions.edge[0]}`)
16
+ }
17
+
18
+ if (versions.iosSaf.length) {
19
+ target.push(`ios${versions.iosSaf[0]}`)
20
+ }
21
+
22
+ if (versions.saf.length) {
23
+ target.push(`safari${versions.saf[0]}`)
24
+ }
25
+
26
+ if (versions.opera.length) {
27
+ target.push(`opera${versions.opera[0]}`)
28
+ }
29
+
30
+ if (target.length === 0) {
31
+ return undefined
32
+ }
33
+
34
+ return target
35
+ }
@@ -0,0 +1,60 @@
1
+ import browserslist from 'browserslist'
2
+
3
+ export interface _PackageTargetVersions {
4
+ chrome: string[]
5
+ ff: string[]
6
+ edge: string[]
7
+ iosSaf: string[]
8
+ saf: string[]
9
+ opera: string[]
10
+ node: string[]
11
+ }
12
+
13
+ function _sortAsNumber(a: string, b: string) {
14
+ const aVersion = Number(a)
15
+ const bVersion = Number(b)
16
+
17
+ return aVersion - bVersion
18
+ }
19
+
20
+ export function _resolveBrowserslistVersions(query: string[]): _PackageTargetVersions {
21
+ const result = browserslist(query)
22
+
23
+ const entries = result.map((key) => {
24
+ const [name, version] = key.split(' ')
25
+
26
+ return {name, version: version.split('-')[0].split('.')[0]}
27
+ })
28
+
29
+ const andChr = entries.filter((b) => b.name === 'and_chr')
30
+ const andFf = entries.filter((b) => b.name === 'and_ff')
31
+ const chrome = entries.filter((b) => b.name === 'chrome')
32
+ const edge = entries.filter((b) => b.name === 'edge')
33
+ const ff = entries.filter((b) => b.name === 'firefox')
34
+ const iosSaf = entries.filter((b) => b.name === 'ios_saf')
35
+ const saf = entries.filter((b) => b.name === 'safari')
36
+ const opera = entries.filter((b) => b.name === 'opera')
37
+ const node = entries.filter((b) => b.name === 'node')
38
+
39
+ // versions
40
+ const versions: _PackageTargetVersions = {
41
+ chrome: andChr.concat(chrome).map((b) => b.version),
42
+ ff: andFf.concat(ff).map((b) => b.version),
43
+ edge: edge.map((b) => b.version),
44
+ iosSaf: iosSaf.map((b) => b.version),
45
+ saf: saf.map((b) => b.version),
46
+ opera: opera.map((b) => b.version),
47
+ node: node.map((b) => b.version),
48
+ }
49
+
50
+ // sort
51
+ versions.chrome.sort(_sortAsNumber)
52
+ versions.ff.sort(_sortAsNumber)
53
+ versions.edge.sort(_sortAsNumber)
54
+ versions.iosSaf.sort(_sortAsNumber)
55
+ versions.saf.sort(_sortAsNumber)
56
+ versions.opera.sort(_sortAsNumber)
57
+ versions.node.sort(_sortAsNumber)
58
+
59
+ return versions
60
+ }
@@ -0,0 +1,104 @@
1
+ import path from 'path'
2
+ import chalk from 'chalk'
3
+ import {
4
+ PkgConfigOptions,
5
+ PkgExports,
6
+ PkgRuntime,
7
+ _BuildContext,
8
+ _DEFAULTS,
9
+ // _loadConfig,
10
+ // _loadPkgWithReporting,
11
+ _loadTSConfig,
12
+ _PackageJSON,
13
+ _parseExports,
14
+ _resolveConfigProperty,
15
+ } from './_core'
16
+ import {_resolveBrowserslistVersions} from './_resolveBrowserslistVersions'
17
+ import {_resolveBrowserTarget} from './_resolveBrowserTarget'
18
+ import {_resolveNodeTarget} from './_resolveNodeTarget'
19
+
20
+ export async function _resolveBuildContext(options: {
21
+ config?: PkgConfigOptions
22
+ cwd: string
23
+ pkg: _PackageJSON
24
+ tsconfig: string
25
+ }): Promise<_BuildContext> {
26
+ const {config, cwd, pkg, tsconfig: tsconfigPath} = options
27
+
28
+ const tsconfig = await _loadTSConfig({cwd, tsconfigPath})
29
+
30
+ const targetVersions = _resolveBrowserslistVersions(pkg.browserslist || _DEFAULTS.browserslist)
31
+
32
+ const srcPath = path.resolve(cwd, config?.src || 'src')
33
+ const distPath = path.resolve(cwd, config?.dist || 'dist')
34
+
35
+ const nodeTarget = _resolveNodeTarget(targetVersions)
36
+ const webTarget = _resolveBrowserTarget(targetVersions)
37
+
38
+ if (!nodeTarget) {
39
+ throw new Error('no matching `node` target')
40
+ }
41
+
42
+ if (!webTarget) {
43
+ throw new Error('no matching `web` target')
44
+ }
45
+
46
+ const target: Record<PkgRuntime, string[]> = {
47
+ '*': webTarget.concat(nodeTarget),
48
+ browser: webTarget,
49
+ node: nodeTarget,
50
+ }
51
+
52
+ const parsedExports = _parseExports({pkg}).reduce<PkgExports>((acc, x) => {
53
+ const {_path: exportPath, ...exportEntry} = x
54
+
55
+ return {...acc, [exportPath]: exportEntry}
56
+ }, {})
57
+
58
+ const exports = _resolveConfigProperty(config?.exports, parsedExports)
59
+
60
+ const parsedExternal = [
61
+ ...(pkg.dependencies ? Object.keys(pkg.dependencies) : []),
62
+ ...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []),
63
+ ]
64
+
65
+ const external = _resolveConfigProperty(config?.external, parsedExternal)
66
+
67
+ const ctx: _BuildContext = {
68
+ config,
69
+ cwd,
70
+ dist: path.relative(cwd, distPath),
71
+ exports,
72
+ external,
73
+ files: [],
74
+ logger: {
75
+ /* eslint-disable no-console */
76
+ log: (...args) => {
77
+ console.log(...args)
78
+ },
79
+ info: (...args) => {
80
+ console.log(chalk.blue(' info'), ...args)
81
+ },
82
+ warn: (...args) => {
83
+ console.log(chalk.yellow(' warn'), ...args)
84
+ },
85
+ error: (...args) => {
86
+ console.log(chalk.red(' fail'), ...args)
87
+ },
88
+ success: (...args) => {
89
+ console.log(chalk.green(' ok '), ...args)
90
+ },
91
+ /* eslint-enable no-console */
92
+ },
93
+ pkg,
94
+ src: path.relative(cwd, srcPath),
95
+ runtime: config?.runtime ?? '*',
96
+ target,
97
+ ts: {
98
+ config: tsconfig,
99
+ configPath: tsconfigPath,
100
+ },
101
+ }
102
+
103
+ return ctx
104
+ }
@@ -0,0 +1,129 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import {PkgExport, PkgFormat, PkgRuntime, _BuildContext} from './_core'
4
+ import {_BuildTask, _DtsTask, _RollupTask, _RollupTaskEntry} from './_tasks'
5
+
6
+ /** @internal */
7
+ export function _resolveBuildTasks(ctx: _BuildContext): _BuildTask[] {
8
+ const {cwd, pkg, target} = ctx
9
+
10
+ const tasks: _BuildTask[] = []
11
+
12
+ const exports = Object.entries(ctx.exports || {}).map(
13
+ ([_path, exp]) => ({_path, ...exp} as PkgExport & {_path: string})
14
+ )
15
+
16
+ const dtsTasks: Record<string, _DtsTask> = {}
17
+ const rollupTasks: Record<string, _RollupTask> = {}
18
+
19
+ function _addRollupTaskEntry(format: PkgFormat, runtime: PkgRuntime, entry: _RollupTaskEntry) {
20
+ const buildId = `${format}:${runtime}`
21
+
22
+ if (!rollupTasks[buildId]) {
23
+ rollupTasks[buildId] = {
24
+ type: 'build:js',
25
+ buildId,
26
+ entries: [entry],
27
+ runtime,
28
+ format,
29
+ target: target[runtime],
30
+ }
31
+ } else {
32
+ rollupTasks[buildId].entries.push(entry)
33
+ }
34
+ }
35
+
36
+ // Parse `dts` tasks
37
+ for (const exp of exports) {
38
+ const importId = path.join(pkg.name, exp._path)
39
+
40
+ if (exp.types) {
41
+ if (!dtsTasks[importId]) {
42
+ dtsTasks[importId] = {
43
+ type: 'build:dts',
44
+ importId,
45
+ exportPath: exp._path,
46
+ source: exp.source,
47
+ output: exp.types,
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ // Parse rollup:commonjs:* tasks
54
+ for (const exp of exports) {
55
+ const output = exp.require
56
+
57
+ if (!output) continue
58
+
59
+ _addRollupTaskEntry('commonjs', ctx.runtime, {
60
+ path: exp._path,
61
+ source: exp.source,
62
+ output,
63
+ })
64
+ }
65
+
66
+ // Parse rollup:commonjs:browser tasks
67
+ for (const exp of exports) {
68
+ const output = exp.browser?.require
69
+
70
+ if (!output) continue
71
+
72
+ _addRollupTaskEntry('commonjs', 'browser', {
73
+ path: exp._path,
74
+ source: exp.browser?.source || exp.source,
75
+ output,
76
+ })
77
+ }
78
+
79
+ // Parse rollup:esm:* tasks
80
+ for (const exp of exports) {
81
+ const output = exp.import
82
+
83
+ if (!output) continue
84
+
85
+ _addRollupTaskEntry('esm', ctx.runtime, {
86
+ path: exp._path,
87
+ source: exp.source,
88
+ output,
89
+ })
90
+ }
91
+
92
+ // Parse rollup:esm:browser tasks
93
+ for (const exp of exports) {
94
+ const output = exp.browser?.import
95
+
96
+ if (!output) continue
97
+
98
+ _addRollupTaskEntry('esm', 'browser', {
99
+ path: exp._path,
100
+ source: exp.browser?.source || exp.source,
101
+ output,
102
+ })
103
+ }
104
+
105
+ tasks.push(...Object.values(dtsTasks))
106
+ tasks.push(...Object.values(rollupTasks))
107
+
108
+ // Parse rollup:esm:browser tasks
109
+ for (const exp of exports) {
110
+ if (exp._exported && exp._path !== '.') {
111
+ const relativeTargetPath = (exp.browser?.import || exp.import || '').replace(/\.[^/.]+$/, '')
112
+
113
+ if (relativeTargetPath) {
114
+ fs.writeFileSync(
115
+ path.resolve(cwd, `${exp._path}.js`),
116
+ [
117
+ `// AUTO-GENERATED – DO NOT EDIT`,
118
+ ``,
119
+ `export {default} from '${relativeTargetPath}'`,
120
+ `export * from '${relativeTargetPath}'`,
121
+ ``,
122
+ ].join('\n')
123
+ )
124
+ }
125
+ }
126
+ }
127
+
128
+ return tasks
129
+ }
@@ -0,0 +1,15 @@
1
+ import {_PackageTargetVersions} from './_resolveBrowserslistVersions'
2
+
3
+ export function _resolveNodeTarget(versions: _PackageTargetVersions): string[] | undefined {
4
+ const target: string[] = []
5
+
6
+ if (versions.node.length) {
7
+ target.push(`node${versions.node[0]}`)
8
+ }
9
+
10
+ if (target.length === 0) {
11
+ return undefined
12
+ }
13
+
14
+ return target
15
+ }
@@ -0,0 +1,129 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import {PkgExport, PkgFormat, PkgRuntime, _BuildContext} from './_core'
4
+ import {_WatchTask, _DtsWatchTask, _RollupWatchTask, _RollupTaskEntry} from './_tasks'
5
+
6
+ /** @internal */
7
+ export function _resolveWatchTasks(ctx: _BuildContext): _WatchTask[] {
8
+ const {cwd, pkg, target} = ctx
9
+
10
+ const tasks: _WatchTask[] = []
11
+
12
+ const exports = Object.entries(ctx.exports || {}).map(
13
+ ([_path, exp]) => ({_path, ...exp} as PkgExport & {_path: string})
14
+ )
15
+
16
+ const dtsTasks: Record<string, _DtsWatchTask> = {}
17
+ const rollupTasks: Record<string, _RollupWatchTask> = {}
18
+
19
+ function _addRollupTaskEntry(format: PkgFormat, runtime: PkgRuntime, entry: _RollupTaskEntry) {
20
+ const buildId = `${format}:${runtime}`
21
+
22
+ if (!rollupTasks[buildId]) {
23
+ rollupTasks[buildId] = {
24
+ type: 'watch:js',
25
+ buildId,
26
+ entries: [entry],
27
+ runtime,
28
+ format,
29
+ target: target[runtime],
30
+ }
31
+ } else {
32
+ rollupTasks[buildId].entries.push(entry)
33
+ }
34
+ }
35
+
36
+ // Parse `dts` tasks
37
+ for (const exp of exports) {
38
+ const importId = path.join(pkg.name, exp._path)
39
+
40
+ if (exp.types) {
41
+ if (!dtsTasks[importId]) {
42
+ dtsTasks[importId] = {
43
+ type: 'watch:dts',
44
+ importId,
45
+ exportPath: exp._path,
46
+ source: exp.source,
47
+ output: exp.types,
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ // Parse rollup:commonjs:* tasks
54
+ for (const exp of exports) {
55
+ const output = exp.require
56
+
57
+ if (!output) continue
58
+
59
+ _addRollupTaskEntry('commonjs', ctx.runtime, {
60
+ path: exp._path,
61
+ source: exp.source,
62
+ output,
63
+ })
64
+ }
65
+
66
+ // Parse rollup:commonjs:browser tasks
67
+ for (const exp of exports) {
68
+ const output = exp.browser?.require
69
+
70
+ if (!output) continue
71
+
72
+ _addRollupTaskEntry('commonjs', 'browser', {
73
+ path: exp._path,
74
+ source: exp.browser?.source || exp.source,
75
+ output,
76
+ })
77
+ }
78
+
79
+ // Parse rollup:esm:* tasks
80
+ for (const exp of exports) {
81
+ const output = exp.import
82
+
83
+ if (!output) continue
84
+
85
+ _addRollupTaskEntry('esm', ctx.runtime, {
86
+ path: exp._path,
87
+ source: exp.source,
88
+ output,
89
+ })
90
+ }
91
+
92
+ // Parse rollup:esm:browser tasks
93
+ for (const exp of exports) {
94
+ const output = exp.browser?.import
95
+
96
+ if (!output) continue
97
+
98
+ _addRollupTaskEntry('esm', 'browser', {
99
+ path: exp._path,
100
+ source: exp.browser?.source || exp.source,
101
+ output,
102
+ })
103
+ }
104
+
105
+ tasks.push(...Object.values(dtsTasks))
106
+ tasks.push(...Object.values(rollupTasks))
107
+
108
+ // Parse rollup:esm:browser tasks
109
+ for (const exp of exports) {
110
+ if (exp._exported && exp._path !== '.') {
111
+ const relativeTargetPath = (exp.browser?.import || exp.import || '').replace(/\.[^/.]+$/, '')
112
+
113
+ if (relativeTargetPath) {
114
+ fs.writeFileSync(
115
+ path.resolve(cwd, `${exp._path}.js`),
116
+ [
117
+ `// AUTO-GENERATED – DO NOT EDIT`,
118
+ ``,
119
+ `export {default} from '${relativeTargetPath}'`,
120
+ `export * from '${relativeTargetPath}'`,
121
+ ``,
122
+ ].join('\n')
123
+ )
124
+ }
125
+ }
126
+ }
127
+
128
+ return tasks
129
+ }
@@ -0,0 +1,26 @@
1
+ import chalk from 'chalk'
2
+ import ora from 'ora'
3
+
4
+ export function _spinner(msg: string): {complete: () => void; error: () => void} {
5
+ const startTime = Date.now()
6
+
7
+ const spinner = ora({
8
+ spinner: 'dots',
9
+ text: `${chalk.yellow('run ')} ${msg}`,
10
+ })
11
+
12
+ spinner.color = 'yellow'
13
+
14
+ spinner.start()
15
+
16
+ return {
17
+ complete: () => {
18
+ spinner.color = 'green'
19
+ spinner.succeed(`${chalk.green('ok ')} ${msg} ${chalk.gray(`${Date.now() - startTime}ms`)}`)
20
+ },
21
+ error: () => {
22
+ spinner.color = 'red'
23
+ spinner.fail(chalk.red(`${msg} failed ${chalk.gray(`${Date.now() - startTime}ms`)}`))
24
+ },
25
+ }
26
+ }
@@ -0,0 +1,57 @@
1
+ import {RollupWatcherEvent} from 'rollup'
2
+ import {Observable} from 'rxjs'
3
+ import {PkgRuntime, _BuildContext} from '../_core'
4
+ import {_DtsResult, _DtsTask, _DtsWatchTask} from './dts'
5
+
6
+ /** @internal */
7
+ export interface _RollupTaskEntry {
8
+ path: string
9
+ source: string
10
+ output: string
11
+ }
12
+
13
+ /** @internal */
14
+ export interface _RollupTask {
15
+ type: 'build:js'
16
+ buildId: string
17
+ entries: _RollupTaskEntry[]
18
+ runtime: PkgRuntime
19
+ format: 'commonjs' | 'esm'
20
+ target: string[]
21
+ }
22
+
23
+ /** @internal */
24
+ export interface _RollupWatchTask {
25
+ type: 'watch:js'
26
+ buildId: string
27
+ entries: _RollupTaskEntry[]
28
+ runtime: PkgRuntime
29
+ format: 'commonjs' | 'esm'
30
+ target: string[]
31
+ }
32
+
33
+ /** @internal */
34
+ export type _BuildTask = _DtsTask | _RollupTask
35
+
36
+ /** @internal */
37
+ export type _WatchTask = _DtsWatchTask | _RollupWatchTask
38
+
39
+ /** @internal */
40
+ export type _TaskHandler<Task, Result = void> = {
41
+ name: (ctx: _BuildContext, task: Task) => string
42
+ exec: (ctx: _BuildContext, task: Task) => Observable<Result>
43
+ complete: (ctx: _BuildContext, task: Task, result: Result) => void
44
+ error: (ctx: _BuildContext, task: Task, error: unknown) => void
45
+ }
46
+
47
+ /** @internal */
48
+ export interface _BuildTaskHandlers {
49
+ 'build:dts': _TaskHandler<_DtsTask, _DtsResult>
50
+ 'build:js': _TaskHandler<_RollupTask>
51
+ }
52
+
53
+ /** @internal */
54
+ export interface _WatchTaskHandlers {
55
+ 'watch:dts': _TaskHandler<_DtsWatchTask, _DtsResult>
56
+ 'watch:js': _TaskHandler<_RollupWatchTask, RollupWatcherEvent>
57
+ }
@@ -0,0 +1,11 @@
1
+ import {ExtractorMessage} from '@microsoft/api-extractor'
2
+
3
+ /** @internal */
4
+ export class _DtsError extends Error {
5
+ messages: ExtractorMessage[]
6
+
7
+ constructor(message: string, messages: ExtractorMessage[]) {
8
+ super(message)
9
+ this.messages = messages
10
+ }
11
+ }
@@ -2,7 +2,6 @@
2
2
 
3
3
  import path from 'path'
4
4
  import ts from 'typescript'
5
- import {_loadTSConfig} from './_loadTSConfig'
6
5
 
7
6
  export const formatHost: ts.FormatDiagnosticsHost = {
8
7
  getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
@@ -10,38 +9,43 @@ export const formatHost: ts.FormatDiagnosticsHost = {
10
9
  getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase(),
11
10
  }
12
11
 
13
- /**
14
- * @internal
15
- */
12
+ /** @internal */
16
13
  export async function _buildTypes(options: {
17
14
  cwd: string
18
15
  outDir: string
19
16
  sourcePath: string
20
- tsconfigPath: string
17
+ tsconfig: ts.ParsedCommandLine
21
18
  }): Promise<{path: string}> {
22
- const {cwd, outDir, sourcePath, tsconfigPath} = options
23
-
24
- const config = await _loadTSConfig({cwd, tsconfigPath})
19
+ const {cwd, outDir, sourcePath, tsconfig} = options
25
20
 
26
21
  const compilerOptions: ts.CompilerOptions = {
27
- ...config.options,
22
+ ...tsconfig.options,
28
23
  declaration: true,
24
+ declarationDir: outDir,
29
25
  emitDeclarationOnly: true,
26
+ noEmit: false,
30
27
  outDir,
31
28
  }
32
29
 
33
- const program = ts.createProgram([path.resolve(cwd, sourcePath)], compilerOptions)
30
+ const program = ts.createProgram(tsconfig.fileNames, compilerOptions)
31
+
32
+ const sourceFiles = program.getSourceFiles()
33
+ const sourceFile = sourceFiles.find((f) => f.fileName === path.resolve(cwd, sourcePath))
34
+
35
+ if (!sourceFile) {
36
+ throw new Error(`source file not found: ${sourcePath}`)
37
+ }
34
38
 
35
39
  const emitResult = program.emit()
36
40
 
37
41
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
38
42
 
39
43
  allDiagnostics.forEach((diagnostic) => {
40
- if (diagnostic.file) {
41
- const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!)
44
+ if (diagnostic.file && diagnostic.start) {
45
+ const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start)
42
46
  const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
43
47
 
44
- console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
48
+ console.log(`${diagnostic.file.fileName} ${line + 1}:${character + 1}\n${message}`)
45
49
  } else {
46
50
  console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
47
51
  }
@@ -57,7 +61,7 @@ export async function _buildTypes(options: {
57
61
  }
58
62
  }
59
63
 
60
- return {
61
- path: path.resolve(outDir, sourcePath.replace(/\.ts$/, '.d.ts')),
62
- }
64
+ const typesPath = path.resolve(outDir, sourcePath.replace(/\.ts$/, '.d.ts'))
65
+
66
+ return {path: typesPath}
63
67
  }
@@ -0,0 +1,41 @@
1
+ import path from 'path'
2
+ import {IConfigFile, IExtractorMessagesConfig} from '@microsoft/api-extractor'
3
+
4
+ export function _createApiExtractorConfig(options: {
5
+ distPath: string
6
+ exportPath: string
7
+ filePath: string
8
+ messages: IExtractorMessagesConfig
9
+ projectPath: string
10
+ sourcePath: string
11
+ tsconfigPath: string
12
+ }): IConfigFile {
13
+ const {distPath, exportPath, filePath, messages, projectPath, sourcePath, tsconfigPath} = options
14
+
15
+ return {
16
+ apiReport: {
17
+ enabled: false,
18
+ reportFileName: '<unscopedPackageName>.api.md',
19
+ },
20
+ // bundledPackages: [],
21
+ compiler: {
22
+ tsconfigFilePath: tsconfigPath,
23
+ },
24
+ docModel: {
25
+ enabled: false,
26
+ apiJsonFilePath: path.resolve(distPath, `${exportPath}.api.json`),
27
+ },
28
+ dtsRollup: {
29
+ enabled: true,
30
+ untrimmedFilePath: path.resolve(distPath, filePath),
31
+ // betaTrimmedFilePath: path.resolve(distPath, filePath.replace('.d.ts', '-beta.d.ts')),
32
+ // publicTrimmedFilePath: path.resolve(distPath, filePath.replace('.d.ts', '-public.d.ts')),
33
+ },
34
+ tsdocMetadata: {
35
+ enabled: false,
36
+ },
37
+ messages,
38
+ mainEntryPointFilePath: sourcePath,
39
+ projectFolder: projectPath,
40
+ }
41
+ }