@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.
- package/README.md +8 -0
- package/dist/__printPackageTree-793a302b.cjs +75 -0
- package/dist/__printPackageTree-793a302b.cjs.map +1 -0
- package/dist/__resolveBuildContext-1b29cff2.cjs +336 -0
- package/dist/__resolveBuildContext-1b29cff2.cjs.map +1 -0
- package/dist/_build-08236974.cjs +169 -0
- package/dist/_build-08236974.cjs.map +1 -0
- package/dist/_buildAction-586dfb5f.cjs +43 -0
- package/dist/_buildAction-586dfb5f.cjs.map +1 -0
- package/dist/_check-9d180462.cjs +16 -0
- package/dist/_check-9d180462.cjs.map +1 -0
- package/dist/_index-c5bd1bc8.cjs +565 -0
- package/dist/_index-c5bd1bc8.cjs.map +1 -0
- package/dist/_statusAction-385b72dd.cjs +27 -0
- package/dist/_statusAction-385b72dd.cjs.map +1 -0
- package/dist/_watch-0623c005.cjs +217 -0
- package/dist/_watch-0623c005.cjs.map +1 -0
- package/dist/_watchAction-4b8d7301.cjs +42 -0
- package/dist/_watchAction-4b8d7301.cjs.map +1 -0
- package/dist/cli.cjs +14 -30
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs +42 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +278 -154
- package/dist/index.js +965 -5129
- package/dist/index.js.map +1 -1
- package/package.json +25 -9
- package/src/cli/buildAction.ts +8 -0
- package/src/cli/index.ts +41 -0
- package/src/cli/statusAction.ts +8 -0
- package/src/cli/watchAction.ts +8 -0
- package/src/node/_core/_defaults.ts +9 -0
- 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 +54 -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 +17 -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/node/_resolveBrowserTarget.ts +35 -0
- package/src/node/_resolveBrowserslistVersions.ts +60 -0
- package/src/node/_resolveBuildContext.ts +104 -0
- package/src/node/_resolveBuildTasks.ts +129 -0
- package/src/node/_resolveNodeTarget.ts +15 -0
- 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 +20 -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 +46 -0
- package/src/node/_tasks/dts/_dtsTask.ts +38 -0
- package/src/node/_tasks/dts/_dtsWatchTask.ts +43 -0
- package/src/node/_tasks/dts/_extractTypes.ts +161 -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 +71 -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 +14 -0
- package/src/node/index.ts +6 -0
- package/src/node/watch.ts +56 -0
- package/dist/_build-e608bb7c.cjs +0 -5458
- package/dist/_build-e608bb7c.cjs.map +0 -1
- package/src/build/_parseTasks.ts +0 -87
- package/src/build/_types.ts +0 -25
- package/src/build/build.ts +0 -82
- 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/_defaults.ts +0 -9
- package/src/core/config/_resolveConfigProperty.ts +0 -12
- package/src/core/config/types.ts +0 -56
- package/src/core/pkg/_loadPkg.ts +0 -13
- package/src/core/pkg/_parseExports.ts +0 -40
- package/src/index.ts +0 -2
|
@@ -2,18 +2,14 @@ import {readFile} from 'fs/promises'
|
|
|
2
2
|
import {TSDocConfigFile} from '@microsoft/tsdoc-config'
|
|
3
3
|
import {parse} from 'jsonc-parser'
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
5
|
+
/** @public */
|
|
8
6
|
export interface TSDocCustomTag {
|
|
9
7
|
name: string
|
|
10
8
|
syntaxKind: 'block' | 'modifier'
|
|
11
9
|
allowMultiple?: boolean
|
|
12
10
|
}
|
|
13
11
|
|
|
14
|
-
/**
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
12
|
+
/** @internal */
|
|
17
13
|
export async function _createTSDocConfig(opts: {
|
|
18
14
|
customTags: TSDocCustomTag[]
|
|
19
15
|
}): Promise<TSDocConfigFile | undefined> {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import {promisify} from 'util'
|
|
3
|
+
import rimrafCallback from 'rimraf'
|
|
4
|
+
import {_BuildContext} from '../../_core'
|
|
5
|
+
import {_buildTypes} from './_buildTypes'
|
|
6
|
+
import {_DtsError} from './_DtsError'
|
|
7
|
+
import {_extractTypes} from './_extractTypes'
|
|
8
|
+
import {_DtsResult, _DtsTask, _DtsWatchTask} from './_types'
|
|
9
|
+
|
|
10
|
+
const rimraf = promisify(rimrafCallback)
|
|
11
|
+
|
|
12
|
+
export async function _doExtract(
|
|
13
|
+
ctx: _BuildContext,
|
|
14
|
+
task: _DtsTask | _DtsWatchTask
|
|
15
|
+
): Promise<_DtsResult> {
|
|
16
|
+
const {config, cwd, dist, files, ts} = ctx
|
|
17
|
+
|
|
18
|
+
if (!ts.config || !ts.configPath) return {type: 'dts', messages: []}
|
|
19
|
+
|
|
20
|
+
const sourcePath = task.source
|
|
21
|
+
const exportPath = task.exportPath === '.' ? './index' : task.exportPath
|
|
22
|
+
const distPath = path.resolve(cwd, dist)
|
|
23
|
+
const outDir = ts.config?.options.outDir || path.resolve(cwd, dist)
|
|
24
|
+
|
|
25
|
+
const types = await _buildTypes({cwd, outDir, sourcePath, tsconfig: ts.config})
|
|
26
|
+
|
|
27
|
+
const {messages} = await _extractTypes({
|
|
28
|
+
cwd,
|
|
29
|
+
exportPath,
|
|
30
|
+
files,
|
|
31
|
+
filePath: path.relative(distPath, path.resolve(cwd, task.output)),
|
|
32
|
+
projectPath: cwd,
|
|
33
|
+
rules: config?.extract?.rules,
|
|
34
|
+
sourcePath: types.path,
|
|
35
|
+
tsconfigPath: path.resolve(cwd, ts.configPath),
|
|
36
|
+
distPath,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
await rimraf(outDir)
|
|
40
|
+
|
|
41
|
+
if (messages.some((msg) => msg.logLevel === 'error')) {
|
|
42
|
+
throw new _DtsError('encountered errors when extracting types', messages)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {type: 'dts', messages}
|
|
46
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import {Observable} from 'rxjs'
|
|
3
|
+
import {_printExtractMessages} from '../../_printExtractMessages'
|
|
4
|
+
import {_TaskHandler} from '../_types'
|
|
5
|
+
import {_doExtract} from './_doExtract'
|
|
6
|
+
import {_DtsError} from './_DtsError'
|
|
7
|
+
import {_DtsResult, _DtsTask} from './_types'
|
|
8
|
+
|
|
9
|
+
/** @internal */
|
|
10
|
+
export const _dtsTask: _TaskHandler<_DtsTask, _DtsResult> = {
|
|
11
|
+
name: (_ctx, task) =>
|
|
12
|
+
[
|
|
13
|
+
`build type definitions`,
|
|
14
|
+
` ${chalk.blue(task.importId)}: ${task.source} -> ${task.output}`,
|
|
15
|
+
].join('\n'),
|
|
16
|
+
exec: (ctx, task) => {
|
|
17
|
+
return new Observable((observer) => {
|
|
18
|
+
_doExtract(ctx, task)
|
|
19
|
+
.then((result) => {
|
|
20
|
+
observer.next(result)
|
|
21
|
+
observer.complete()
|
|
22
|
+
})
|
|
23
|
+
.catch(observer.error)
|
|
24
|
+
})
|
|
25
|
+
},
|
|
26
|
+
complete: (ctx, _task, result) => {
|
|
27
|
+
_printExtractMessages(ctx, result.messages)
|
|
28
|
+
},
|
|
29
|
+
error: (ctx, _task, err) => {
|
|
30
|
+
const {logger} = ctx
|
|
31
|
+
|
|
32
|
+
if (err instanceof _DtsError) {
|
|
33
|
+
_printExtractMessages(ctx, err.messages)
|
|
34
|
+
} else if (err instanceof Error) {
|
|
35
|
+
logger.error(err)
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import {Observable} from 'rxjs'
|
|
3
|
+
import {_printExtractMessages} from '../../_printExtractMessages'
|
|
4
|
+
import {_TaskHandler} from '../_types'
|
|
5
|
+
import {_doExtract} from './_doExtract'
|
|
6
|
+
import {_DtsError} from './_DtsError'
|
|
7
|
+
import {_DtsResult, _DtsWatchTask} from './_types'
|
|
8
|
+
|
|
9
|
+
/** @internal */
|
|
10
|
+
export const _dtsWatchTask: _TaskHandler<_DtsWatchTask, _DtsResult> = {
|
|
11
|
+
name: (_ctx, task) =>
|
|
12
|
+
[
|
|
13
|
+
`build type definitions`,
|
|
14
|
+
` ${chalk.blue(task.importId)}: ${task.source} -> ${task.output}`,
|
|
15
|
+
].join('\n'),
|
|
16
|
+
exec: (ctx, task) => {
|
|
17
|
+
return new Observable((observer) => {
|
|
18
|
+
_doExtract(ctx, task)
|
|
19
|
+
.then((result) => {
|
|
20
|
+
observer.next(result)
|
|
21
|
+
observer.complete()
|
|
22
|
+
})
|
|
23
|
+
.catch(observer.error)
|
|
24
|
+
})
|
|
25
|
+
},
|
|
26
|
+
complete: (ctx, _task, result) => {
|
|
27
|
+
const {logger} = ctx
|
|
28
|
+
|
|
29
|
+
_printExtractMessages(ctx, result.messages)
|
|
30
|
+
|
|
31
|
+
logger.warn('watching typescript definitions is currently not supported')
|
|
32
|
+
logger.log()
|
|
33
|
+
},
|
|
34
|
+
error: (ctx, _task, err) => {
|
|
35
|
+
const {logger} = ctx
|
|
36
|
+
|
|
37
|
+
if (err instanceof _DtsError) {
|
|
38
|
+
_printExtractMessages(ctx, err.messages)
|
|
39
|
+
} else if (err instanceof Error) {
|
|
40
|
+
logger.error(err)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import fs from 'fs/promises'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import {
|
|
4
|
+
Extractor,
|
|
5
|
+
ExtractorConfig,
|
|
6
|
+
ExtractorLogLevel,
|
|
7
|
+
ExtractorMessage,
|
|
8
|
+
ExtractorResult,
|
|
9
|
+
IExtractorMessagesConfig,
|
|
10
|
+
} from '@microsoft/api-extractor'
|
|
11
|
+
import prettier from 'prettier'
|
|
12
|
+
import {PkgRuleLevel, PkgConfigOptions, _BuildFile} from '../../_core'
|
|
13
|
+
import {_createApiExtractorConfig} from './_createApiExtractorConfig'
|
|
14
|
+
import {_createTSDocConfig} from './_createTSDocConfig'
|
|
15
|
+
|
|
16
|
+
const _LOG_LEVELS: Record<PkgRuleLevel, ExtractorLogLevel> = {
|
|
17
|
+
error: 'error' as ExtractorLogLevel.Error,
|
|
18
|
+
info: 'info' as ExtractorLogLevel.Info,
|
|
19
|
+
off: 'none' as ExtractorLogLevel.None,
|
|
20
|
+
warn: 'warning' as ExtractorLogLevel.Warning,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function _extractTypes(options: {
|
|
24
|
+
cwd: string
|
|
25
|
+
distPath: string
|
|
26
|
+
exportPath: string
|
|
27
|
+
files: _BuildFile[]
|
|
28
|
+
filePath: string
|
|
29
|
+
projectPath: string
|
|
30
|
+
rules?: NonNullable<PkgConfigOptions['extract']>['rules']
|
|
31
|
+
sourcePath: string
|
|
32
|
+
tsconfigPath: string
|
|
33
|
+
}): Promise<{extractorResult: ExtractorResult; messages: ExtractorMessage[]}> {
|
|
34
|
+
const {distPath, exportPath, files, filePath, projectPath, rules, sourcePath, tsconfigPath} =
|
|
35
|
+
options
|
|
36
|
+
|
|
37
|
+
const tsdocConfigFile = await _createTSDocConfig({
|
|
38
|
+
customTags: [],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
function _ruleToLogLevel(
|
|
42
|
+
key: keyof NonNullable<NonNullable<PkgConfigOptions['extract']>['rules']>,
|
|
43
|
+
defaultLevel?: ExtractorLogLevel
|
|
44
|
+
) {
|
|
45
|
+
const r = rules?.[key]
|
|
46
|
+
|
|
47
|
+
return (r ? _LOG_LEVELS[r] : defaultLevel || 'warning') as ExtractorLogLevel
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const messagesConfig: IExtractorMessagesConfig = {
|
|
51
|
+
compilerMessageReporting: {
|
|
52
|
+
default: {
|
|
53
|
+
logLevel: 'warning' as ExtractorLogLevel,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
extractorMessageReporting: {
|
|
58
|
+
default: {
|
|
59
|
+
logLevel: 'warning' as ExtractorLogLevel,
|
|
60
|
+
addToApiReportFile: false,
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
'ae-forgotten-export': {
|
|
64
|
+
logLevel: _ruleToLogLevel('ae-forgotten-export', 'error' as ExtractorLogLevel),
|
|
65
|
+
addToApiReportFile: false,
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
'ae-incompatible-release-tags': {
|
|
69
|
+
logLevel: _ruleToLogLevel('ae-incompatible-release-tags', 'error' as ExtractorLogLevel),
|
|
70
|
+
addToApiReportFile: false,
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
'ae-internal-missing-underscore': {
|
|
74
|
+
logLevel: _ruleToLogLevel('ae-internal-missing-underscore'),
|
|
75
|
+
addToApiReportFile: false,
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
'ae-missing-release-tag': {
|
|
79
|
+
logLevel: _ruleToLogLevel('ae-missing-release-tag', 'error' as ExtractorLogLevel),
|
|
80
|
+
addToApiReportFile: false,
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
'ae-wrong-input-file-type': {
|
|
84
|
+
logLevel: 'none' as ExtractorLogLevel,
|
|
85
|
+
addToApiReportFile: false,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
tsdocMessageReporting: {
|
|
90
|
+
default: {
|
|
91
|
+
logLevel: 'warning' as ExtractorLogLevel,
|
|
92
|
+
addToApiReportFile: false,
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
// 'tsdoc-link-tag-unescaped-text': {
|
|
96
|
+
// logLevel: 'warning' as ExtractorLogLevel,
|
|
97
|
+
// addToApiReportFile: false
|
|
98
|
+
// },
|
|
99
|
+
|
|
100
|
+
// 'tsdoc-unsupported-tag': {
|
|
101
|
+
// logLevel: 'none' as ExtractorLogLevel,
|
|
102
|
+
// addToApiReportFile: false
|
|
103
|
+
// },
|
|
104
|
+
|
|
105
|
+
// 'tsdoc-undefined-tag': {
|
|
106
|
+
// logLevel: 'none' as ExtractorLogLevel,
|
|
107
|
+
// addToApiReportFile: false
|
|
108
|
+
// }
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const extractorConfig: ExtractorConfig = ExtractorConfig.prepare({
|
|
113
|
+
configObject: _createApiExtractorConfig({
|
|
114
|
+
distPath,
|
|
115
|
+
exportPath,
|
|
116
|
+
filePath,
|
|
117
|
+
messages: messagesConfig,
|
|
118
|
+
projectPath,
|
|
119
|
+
sourcePath,
|
|
120
|
+
tsconfigPath,
|
|
121
|
+
}),
|
|
122
|
+
configObjectFullPath: undefined,
|
|
123
|
+
tsdocConfigFile,
|
|
124
|
+
packageJsonFullPath: path.resolve(projectPath, 'package.json'),
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
const messages: ExtractorMessage[] = []
|
|
128
|
+
|
|
129
|
+
// Invoke API Extractor
|
|
130
|
+
const extractorResult = Extractor.invoke(extractorConfig, {
|
|
131
|
+
// Equivalent to the "--local" command-line parameter
|
|
132
|
+
localBuild: true,
|
|
133
|
+
// Equivalent to the "--verbose" command-line parameter
|
|
134
|
+
showVerboseMessages: true,
|
|
135
|
+
// handle messages
|
|
136
|
+
messageCallback(message: ExtractorMessage) {
|
|
137
|
+
messages.push(message)
|
|
138
|
+
message.handled = true
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const typesPath = path.resolve(distPath, `${exportPath}.d.ts`)
|
|
143
|
+
const typesBuf = await fs.readFile(typesPath)
|
|
144
|
+
const prettierConfig = await prettier.resolveConfig(typesPath)
|
|
145
|
+
|
|
146
|
+
await fs.writeFile(
|
|
147
|
+
typesPath,
|
|
148
|
+
prettier.format(typesBuf.toString(), {
|
|
149
|
+
...prettierConfig,
|
|
150
|
+
filepath: typesPath,
|
|
151
|
+
})
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
// Add to `files` in context
|
|
155
|
+
files.push({
|
|
156
|
+
type: 'types',
|
|
157
|
+
path: typesPath,
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
return {extractorResult, messages}
|
|
161
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {ExtractorMessage} from '@microsoft/api-extractor'
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export interface _DtsWatchTask {
|
|
5
|
+
type: 'watch:dts'
|
|
6
|
+
exportPath: string
|
|
7
|
+
importId: string
|
|
8
|
+
source: string
|
|
9
|
+
output: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @internal */
|
|
13
|
+
export interface _DtsTask {
|
|
14
|
+
type: 'build:dts'
|
|
15
|
+
exportPath: string
|
|
16
|
+
importId: string
|
|
17
|
+
source: string
|
|
18
|
+
output: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
export interface _DtsResult {
|
|
23
|
+
type: 'dts'
|
|
24
|
+
messages: ExtractorMessage[]
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {_BuildTaskHandlers, _WatchTaskHandlers} from './_types'
|
|
2
|
+
import {_dtsTask, _dtsWatchTask} from './dts'
|
|
3
|
+
import {_rollupTask, _rollupWatchTask} from './rollup'
|
|
4
|
+
|
|
5
|
+
export * from './_types'
|
|
6
|
+
export * from './dts'
|
|
7
|
+
export * from './rollup'
|
|
8
|
+
|
|
9
|
+
/** @internal */
|
|
10
|
+
export const _buildTaskHandlers: _BuildTaskHandlers = {
|
|
11
|
+
'build:dts': _dtsTask,
|
|
12
|
+
'build:js': _rollupTask,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @internal */
|
|
16
|
+
export const _watchTaskHandlers: _WatchTaskHandlers = {
|
|
17
|
+
'watch:dts': _dtsWatchTask,
|
|
18
|
+
'watch:js': _rollupWatchTask,
|
|
19
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import alias from '@rollup/plugin-alias'
|
|
3
|
+
import commonjs from '@rollup/plugin-commonjs'
|
|
4
|
+
import json from '@rollup/plugin-json'
|
|
5
|
+
import {nodeResolve} from '@rollup/plugin-node-resolve'
|
|
6
|
+
import replace from '@rollup/plugin-replace'
|
|
7
|
+
import {InputOptions, OutputOptions} from 'rollup'
|
|
8
|
+
import esbuild from 'rollup-plugin-esbuild'
|
|
9
|
+
import {_BuildContext} from '../../_core'
|
|
10
|
+
import {_RollupTask, _RollupWatchTask} from '../_types'
|
|
11
|
+
|
|
12
|
+
export interface _RollupConfig {
|
|
13
|
+
inputOptions: InputOptions
|
|
14
|
+
outputOptions: OutputOptions
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export function _resolveRollupConfig(
|
|
19
|
+
ctx: _BuildContext,
|
|
20
|
+
buildTask: _RollupTask | _RollupWatchTask
|
|
21
|
+
): _RollupConfig {
|
|
22
|
+
const {format, runtime, target} = buildTask
|
|
23
|
+
const {config, cwd, external, dist: outDir, pkg, ts} = ctx
|
|
24
|
+
const outputExt = format === 'commonjs' ? '.cjs' : pkg.type === 'module' ? '.mjs' : '.js'
|
|
25
|
+
|
|
26
|
+
const pathAliases = Object.fromEntries(
|
|
27
|
+
Object.entries(ts.config?.options.paths || {}).map(([key, val]) => {
|
|
28
|
+
return [key, path.resolve(cwd, ts.config?.options.baseUrl || '.', val[0])]
|
|
29
|
+
})
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
const entries = buildTask.entries.map((entry) => {
|
|
33
|
+
return {
|
|
34
|
+
...entry,
|
|
35
|
+
name: path.relative(outDir, entry.output).replace(/\.[^/.]+$/, ''),
|
|
36
|
+
}
|
|
37
|
+
}, {})
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
inputOptions: {
|
|
41
|
+
context: cwd,
|
|
42
|
+
|
|
43
|
+
external: (id) => {
|
|
44
|
+
const idParts = id.split('/')
|
|
45
|
+
|
|
46
|
+
const name = idParts[0].startsWith('@') ? `${idParts[0]}/${idParts[1]}` : idParts[0]
|
|
47
|
+
|
|
48
|
+
if (name && external.includes(name)) {
|
|
49
|
+
return true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return false
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
input: entries.reduce<{[entryAlias: string]: string}>((acc, entry) => {
|
|
56
|
+
return {...acc, [entry.name]: entry.source}
|
|
57
|
+
}, {}),
|
|
58
|
+
|
|
59
|
+
onwarn(warning, rollupWarn) {
|
|
60
|
+
if (!warning.code || !['CIRCULAR_DEPENDENCY'].includes(warning.code)) {
|
|
61
|
+
rollupWarn(warning)
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
plugins: [
|
|
66
|
+
replace({
|
|
67
|
+
preventAssignment: true,
|
|
68
|
+
values: {
|
|
69
|
+
...(pkg.name === '@sanity/pkg-utils'
|
|
70
|
+
? {}
|
|
71
|
+
: {
|
|
72
|
+
'process.env.PKG_FILE_PATH': (arg) => {
|
|
73
|
+
const sourcePath = './' + path.relative(cwd, arg)
|
|
74
|
+
const entry = entries.find((e) => e.source === sourcePath)
|
|
75
|
+
|
|
76
|
+
if (!entry) {
|
|
77
|
+
throw new Error(`could not find source entry: ${sourcePath}`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return JSON.stringify(
|
|
81
|
+
path.relative(cwd, path.resolve(outDir, entry.name + outputExt))
|
|
82
|
+
)
|
|
83
|
+
},
|
|
84
|
+
'process.env.PKG_FORMAT': JSON.stringify(format),
|
|
85
|
+
'process.env.PKG_RUNTIME': JSON.stringify(runtime),
|
|
86
|
+
'process.env.PKG_VERSION': JSON.stringify(process.env.PKG_VERSION || pkg.version),
|
|
87
|
+
}),
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
alias({
|
|
91
|
+
entries: {...pathAliases},
|
|
92
|
+
}),
|
|
93
|
+
nodeResolve({
|
|
94
|
+
// mainFields: ['module', 'jsnext', 'main'],
|
|
95
|
+
browser: runtime === 'browser',
|
|
96
|
+
// exportConditions: [runtime === 'node' ? 'node' : 'browser'],
|
|
97
|
+
extensions: ['.cjs', '.mjs', '.js', '.jsx', '.json', '.node'],
|
|
98
|
+
preferBuiltins: true, // runtime === 'node',
|
|
99
|
+
}),
|
|
100
|
+
commonjs({
|
|
101
|
+
esmExternals: false,
|
|
102
|
+
extensions: ['.js'],
|
|
103
|
+
// include: /\/node_modules\//,
|
|
104
|
+
// requireReturnsDefault: 'namespace',
|
|
105
|
+
}),
|
|
106
|
+
json(),
|
|
107
|
+
esbuild({
|
|
108
|
+
minify: config?.minify ?? true,
|
|
109
|
+
jsx: 'transform', // default, or 'preserve'
|
|
110
|
+
jsxFactory: 'React.createElement',
|
|
111
|
+
jsxFragment: 'React.Fragment',
|
|
112
|
+
target,
|
|
113
|
+
tsconfig: ctx.ts.configPath || 'tsconfig.json', // 'tsconfig.json', // default
|
|
114
|
+
}),
|
|
115
|
+
],
|
|
116
|
+
|
|
117
|
+
treeshake: {
|
|
118
|
+
propertyReadSideEffects: false,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
outputOptions: {
|
|
122
|
+
chunkFileNames: () => `_[name]-[hash]${outputExt}`,
|
|
123
|
+
dir: outDir,
|
|
124
|
+
entryFileNames: () => `[name]${outputExt}`,
|
|
125
|
+
format,
|
|
126
|
+
// esModule: true,
|
|
127
|
+
exports: 'auto',
|
|
128
|
+
// freeze: true,
|
|
129
|
+
sourcemap: config?.sourcemap ?? true,
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import chalk from 'chalk'
|
|
5
|
+
import {rollup} from 'rollup'
|
|
6
|
+
import {Observable} from 'rxjs'
|
|
7
|
+
import {_BuildContext} from '../../_core'
|
|
8
|
+
import {_RollupTask, _TaskHandler} from '../_types'
|
|
9
|
+
import {_resolveRollupConfig} from './_resolveRollupConfig'
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const _rollupTask: _TaskHandler<_RollupTask> = {
|
|
13
|
+
name: (ctx, task) =>
|
|
14
|
+
`build javascript files (target ${task.target.join(' + ')}, format ${
|
|
15
|
+
task.format
|
|
16
|
+
})\n ${task.entries
|
|
17
|
+
.map((e) => `${chalk.blue(path.join(ctx.pkg.name, e.path))}: ${e.source} -> ${e.output}`)
|
|
18
|
+
.join('\n ')}`,
|
|
19
|
+
exec: (ctx, task) => {
|
|
20
|
+
return new Observable((observer) => {
|
|
21
|
+
_execPromise(ctx, task).then((result) => {
|
|
22
|
+
observer.next(result)
|
|
23
|
+
observer.complete()
|
|
24
|
+
}, observer.error)
|
|
25
|
+
})
|
|
26
|
+
},
|
|
27
|
+
complete: () => {
|
|
28
|
+
//
|
|
29
|
+
},
|
|
30
|
+
error: (_ctx, _task, err) => {
|
|
31
|
+
if (err instanceof Error) {
|
|
32
|
+
console.log(err)
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function _execPromise(ctx: _BuildContext, task: _RollupTask) {
|
|
38
|
+
const {files, dist: outDir} = ctx
|
|
39
|
+
|
|
40
|
+
const {inputOptions, outputOptions} = _resolveRollupConfig(ctx, task)
|
|
41
|
+
|
|
42
|
+
// Create bundle
|
|
43
|
+
const bundle = await rollup(inputOptions)
|
|
44
|
+
|
|
45
|
+
// an array of file names this bundle depends on
|
|
46
|
+
// console.log(bundle.watchFiles)
|
|
47
|
+
|
|
48
|
+
// generate output specific code in-memory
|
|
49
|
+
// you can call this function multiple times on the same bundle object
|
|
50
|
+
const {output} = await bundle.generate(outputOptions)
|
|
51
|
+
|
|
52
|
+
for (const chunkOrAsset of output) {
|
|
53
|
+
if (chunkOrAsset.type === 'asset') {
|
|
54
|
+
files.push({
|
|
55
|
+
type: 'asset',
|
|
56
|
+
path: path.resolve(outDir, chunkOrAsset.fileName),
|
|
57
|
+
})
|
|
58
|
+
} else {
|
|
59
|
+
files.push({
|
|
60
|
+
type: 'chunk',
|
|
61
|
+
path: path.resolve(outDir, chunkOrAsset.fileName),
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// or write the bundle to disk
|
|
67
|
+
await bundle.write(outputOptions)
|
|
68
|
+
|
|
69
|
+
// closes the bundle
|
|
70
|
+
await bundle.close()
|
|
71
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import {RollupWatcherEvent, RollupWatchOptions, watch as rollupWatch} from 'rollup'
|
|
4
|
+
import {Observable} from 'rxjs'
|
|
5
|
+
import {_RollupWatchTask, _TaskHandler} from '../_types'
|
|
6
|
+
import {_resolveRollupConfig} from './_resolveRollupConfig'
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
export const _rollupWatchTask: _TaskHandler<_RollupWatchTask, RollupWatcherEvent> = {
|
|
10
|
+
name: (ctx, task) =>
|
|
11
|
+
`build javascript files (target ${task.target.join(' + ')}, format ${
|
|
12
|
+
task.format
|
|
13
|
+
})\n ${task.entries
|
|
14
|
+
.map((e) => `${chalk.blue(path.join(ctx.pkg.name, e.path))}: ${e.source} -> ${e.output}`)
|
|
15
|
+
.join('\n ')}`,
|
|
16
|
+
exec: (ctx, task) => {
|
|
17
|
+
const {inputOptions, outputOptions} = _resolveRollupConfig(ctx, task)
|
|
18
|
+
|
|
19
|
+
return new Observable((observer) => {
|
|
20
|
+
const watchOptions: RollupWatchOptions = {
|
|
21
|
+
...inputOptions,
|
|
22
|
+
output: outputOptions,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const watcher = rollupWatch(watchOptions)
|
|
26
|
+
|
|
27
|
+
watcher.on('event', (event) => {
|
|
28
|
+
observer.next(event)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return () => {
|
|
32
|
+
watcher.close()
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
},
|
|
36
|
+
complete: (ctx, task, event) => {
|
|
37
|
+
const {logger} = ctx
|
|
38
|
+
|
|
39
|
+
if (event.code === 'BUNDLE_END') {
|
|
40
|
+
logger.success(
|
|
41
|
+
`build javascript files (target ${task.target.join(' + ')}, format ${
|
|
42
|
+
task.format
|
|
43
|
+
})\n ${task.entries
|
|
44
|
+
.map((e) => `${chalk.blue(path.join(ctx.pkg.name, e.path))}: ${e.source} -> ${e.output}`)
|
|
45
|
+
.join('\n ')}`
|
|
46
|
+
)
|
|
47
|
+
logger.log('')
|
|
48
|
+
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (event.code === 'BUNDLE_START') {
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (event.code === 'END') {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (event.code === 'ERROR') {
|
|
61
|
+
// logger.log('')
|
|
62
|
+
logger.error(event.code, event)
|
|
63
|
+
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (event.code === 'START') {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
error: (ctx, _task, err) => {
|
|
72
|
+
const {logger} = ctx
|
|
73
|
+
|
|
74
|
+
if (err instanceof Error) {
|
|
75
|
+
logger.log(err)
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
}
|