@sanity/pkg-utils 1.3.1 → 1.5.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.
- package/README.md +8 -0
- package/dist/__printPackageTree-3ff3ecd3.cjs +76 -0
- package/dist/__printPackageTree-3ff3ecd3.cjs.map +1 -0
- package/dist/__resolveBuildContext-85cfca8d.cjs +337 -0
- package/dist/__resolveBuildContext-85cfca8d.cjs.map +1 -0
- package/dist/_build-683d9922.cjs +168 -0
- package/dist/_build-683d9922.cjs.map +1 -0
- package/dist/_buildAction-f593323a.cjs +44 -0
- package/dist/_buildAction-f593323a.cjs.map +1 -0
- package/dist/_check-6843c26d.cjs +118 -0
- package/dist/_check-6843c26d.cjs.map +1 -0
- package/dist/_checkAction-0bca6a14.cjs +29 -0
- package/dist/_checkAction-0bca6a14.cjs.map +1 -0
- package/dist/_index-5765fada.cjs +584 -0
- package/dist/_index-5765fada.cjs.map +1 -0
- package/dist/_watch-ec1b35d6.cjs +217 -0
- package/dist/_watch-ec1b35d6.cjs.map +1 -0
- package/dist/_watchAction-a388fc00.cjs +43 -0
- package/dist/_watchAction-a388fc00.cjs.map +1 -0
- package/dist/cli.cjs +14 -31
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs +43 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +282 -152
- package/dist/index.js +1055 -5198
- package/dist/index.js.map +1 -1
- package/package.json +37 -10
- package/src/cli/buildAction.ts +8 -0
- package/src/cli/checkAction.ts +9 -0
- package/src/cli/index.ts +42 -0
- package/src/cli/watchAction.ts +8 -0
- package/src/{core → node/_core}/_defaults.ts +1 -3
- package/src/{core → node/_core}/_isRecord.ts +1 -3
- package/src/{core → node/_core}/config/_loadConfig.ts +1 -3
- package/src/node/_core/config/_resolveConfigProperty.ts +15 -0
- package/src/{core → node/_core}/config/defineConfig.ts +1 -3
- package/src/{core → node/_core}/config/index.ts +0 -0
- package/src/node/_core/config/types.ts +69 -0
- package/src/node/_core/contexts/buildContext.ts +34 -0
- package/src/node/_core/contexts/index.ts +1 -0
- package/src/{core → node/_core}/index.ts +2 -0
- package/src/node/_core/pkg/_loadPkg.ts +55 -0
- package/src/node/_core/pkg/_loadPkgWithReporting.ts +29 -0
- package/src/node/_core/pkg/_parseExports.ts +45 -0
- package/src/{core → node/_core}/pkg/_types.ts +16 -5
- package/src/{core → node/_core}/pkg/index.ts +1 -0
- package/src/{build/tasks/dts → node/_core/ts}/_loadTSConfig.ts +1 -0
- package/src/node/_core/ts/index.ts +1 -0
- package/src/node/_getFilesize.ts +8 -0
- package/src/node/_globFiles.ts +5 -0
- package/src/node/_printExtractMessages.ts +56 -0
- package/src/node/_printPackageTree.ts +72 -0
- package/src/{build/_parseWebTarget.ts → node/_resolveBrowserTarget.ts} +2 -2
- package/src/{build/_parseBrowserslistVersions.ts → node/_resolveBrowserslistVersions.ts} +1 -1
- package/src/node/_resolveBuildContext.ts +104 -0
- package/src/node/_resolveBuildTasks.ts +129 -0
- package/src/{build/_parseNodeTarget.ts → node/_resolveNodeTarget.ts} +2 -2
- package/src/node/_resolveWatchTasks.ts +129 -0
- package/src/node/_spinner.ts +26 -0
- package/src/node/_tasks/_types.ts +57 -0
- package/src/node/_tasks/dts/_DtsError.ts +11 -0
- package/src/{build/tasks → node/_tasks}/dts/_buildTypes.ts +30 -16
- package/src/node/_tasks/dts/_createApiExtractorConfig.ts +41 -0
- package/src/{build/tasks → node/_tasks}/dts/_createTSDocConfig.ts +2 -6
- package/src/node/_tasks/dts/_doExtract.ts +57 -0
- package/src/node/_tasks/dts/_dtsTask.ts +40 -0
- package/src/node/_tasks/dts/_dtsWatchTask.ts +43 -0
- package/src/node/_tasks/dts/_extractTypes.ts +164 -0
- package/src/node/_tasks/dts/_types.ts +25 -0
- package/src/node/_tasks/dts/index.ts +3 -0
- package/src/node/_tasks/index.ts +19 -0
- package/src/node/_tasks/rollup/_resolveRollupConfig.ts +132 -0
- package/src/node/_tasks/rollup/_rollupTask.ts +73 -0
- package/src/node/_tasks/rollup/_rollupWatchTask.ts +78 -0
- package/src/node/_tasks/rollup/index.ts +2 -0
- package/src/node/_watchConfigFiles.ts +52 -0
- package/src/node/_watchFiles.ts +29 -0
- package/src/node/build.ts +47 -0
- package/src/node/check.ts +136 -0
- package/src/node/index.ts +6 -0
- package/src/node/watch.ts +56 -0
- package/dist/_build-5bf997de.cjs +0 -5549
- package/dist/_build-5bf997de.cjs.map +0 -1
- package/src/build/_parseTasks.ts +0 -87
- package/src/build/_types.ts +0 -26
- package/src/build/build.ts +0 -101
- package/src/build/index.ts +0 -2
- package/src/build/tasks/_types.ts +0 -8
- package/src/build/tasks/dts/_createApiExtractorConfig.ts +0 -88
- package/src/build/tasks/dts/_extractTypes.ts +0 -100
- package/src/build/tasks/dts/dts.ts +0 -59
- package/src/build/tasks/dts/index.ts +0 -1
- package/src/build/tasks/extract/_extractTask.ts +0 -49
- package/src/build/tasks/extract/index.ts +0 -1
- package/src/build/tasks/index.ts +0 -4
- package/src/build/tasks/rollup/_resolveRollupConfig.ts +0 -95
- package/src/build/tasks/rollup/index.ts +0 -1
- package/src/build/tasks/rollup/rollup.ts +0 -96
- package/src/cli.ts +0 -25
- package/src/core/config/_resolveConfigProperty.ts +0 -14
- package/src/core/config/types.ts +0 -52
- package/src/core/pkg/_loadPkg.ts +0 -13
- package/src/core/pkg/_parseExports.ts +0 -40
- package/src/index.ts +0 -2
|
@@ -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
|
+
export function _fileExists(file: string): boolean {
|
|
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 = _fileExists(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)} ${chalk.red('does not exist')}`
|
|
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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {_PackageTargetVersions} from './
|
|
1
|
+
import {_PackageTargetVersions} from './_resolveBrowserslistVersions'
|
|
2
2
|
|
|
3
|
-
export function
|
|
3
|
+
export function _resolveBrowserTarget(versions: _PackageTargetVersions): string[] | undefined {
|
|
4
4
|
const target: string[] = []
|
|
5
5
|
|
|
6
6
|
if (versions.chrome.length) {
|
|
@@ -17,7 +17,7 @@ function _sortAsNumber(a: string, b: string) {
|
|
|
17
17
|
return aVersion - bVersion
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export function
|
|
20
|
+
export function _resolveBrowserslistVersions(query: string[]): _PackageTargetVersions {
|
|
21
21
|
const result = browserslist(query)
|
|
22
22
|
|
|
23
23
|
const entries = result.map((key) => {
|
|
@@ -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
|
+
sourcePath: exp.source,
|
|
47
|
+
targetPath: 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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {_PackageTargetVersions} from './
|
|
1
|
+
import {_PackageTargetVersions} from './_resolveBrowserslistVersions'
|
|
2
2
|
|
|
3
|
-
export function
|
|
3
|
+
export function _resolveNodeTarget(versions: _PackageTargetVersions): string[] | undefined {
|
|
4
4
|
const target: string[] = []
|
|
5
5
|
|
|
6
6
|
if (versions.node.length) {
|
|
@@ -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
|
+
sourcePath: exp.source,
|
|
47
|
+
targetPath: 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,51 @@ 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
|
-
|
|
17
|
+
tsconfig: ts.ParsedCommandLine
|
|
21
18
|
}): Promise<{path: string}> {
|
|
22
|
-
const {cwd, outDir, sourcePath,
|
|
23
|
-
|
|
24
|
-
const config = await _loadTSConfig({cwd, tsconfigPath})
|
|
19
|
+
const {cwd, outDir, sourcePath, tsconfig} = options
|
|
25
20
|
|
|
26
21
|
const compilerOptions: ts.CompilerOptions = {
|
|
27
|
-
...
|
|
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
|
|
30
|
+
const rootDir =
|
|
31
|
+
path.relative(
|
|
32
|
+
cwd,
|
|
33
|
+
compilerOptions.rootDir
|
|
34
|
+
? path.resolve(cwd, compilerOptions.rootDir)
|
|
35
|
+
: path.dirname(path.resolve(cwd, sourcePath))
|
|
36
|
+
) || '.'
|
|
37
|
+
|
|
38
|
+
const program = ts.createProgram(tsconfig.fileNames, compilerOptions)
|
|
39
|
+
|
|
40
|
+
const sourceFiles = program.getSourceFiles()
|
|
41
|
+
const sourceFile = sourceFiles.find((f) => f.fileName === path.resolve(cwd, sourcePath))
|
|
42
|
+
|
|
43
|
+
if (!sourceFile) {
|
|
44
|
+
throw new Error(`source file not found: ${sourcePath}`)
|
|
45
|
+
}
|
|
34
46
|
|
|
35
47
|
const emitResult = program.emit()
|
|
36
48
|
|
|
37
49
|
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
|
|
38
50
|
|
|
39
51
|
allDiagnostics.forEach((diagnostic) => {
|
|
40
|
-
if (diagnostic.file) {
|
|
41
|
-
const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start
|
|
52
|
+
if (diagnostic.file && diagnostic.start) {
|
|
53
|
+
const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start)
|
|
42
54
|
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
|
|
43
55
|
|
|
44
|
-
console.log(`${diagnostic.file.fileName}
|
|
56
|
+
console.log(`${diagnostic.file.fileName} ${line + 1}:${character + 1}\n${message}`)
|
|
45
57
|
} else {
|
|
46
58
|
console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
|
|
47
59
|
}
|
|
@@ -57,7 +69,9 @@ export async function _buildTypes(options: {
|
|
|
57
69
|
}
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
const typesPath = path
|
|
73
|
+
.resolve(outDir, path.relative(rootDir, path.resolve(cwd, sourcePath)))
|
|
74
|
+
.replace(/\.[jt]sx?$/, '.d.ts')
|
|
75
|
+
|
|
76
|
+
return {path: typesPath}
|
|
63
77
|
}
|
|
@@ -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
|
+
}
|