Skip to content

Commit 6c1e26f

Browse files
committed
build: enhance dts gen
1 parent dd59a23 commit 6c1e26f

File tree

7 files changed

+118
-83
lines changed

7 files changed

+118
-83
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"build": "npm run build:js && npm run build:dts",
6666
"build:check": "tsc",
6767
"build:js": "node scripts/build-js.mjs --format=cjs --hybrid --entry=src/*.ts && npm run build:vendor",
68-
"build:vendor": "node scripts/build-js.mjs --format=cjs --entry='src/core-vendor.ts:src/vendor.ts' --bundle=all",
68+
"build:vendor": "node scripts/build-js.mjs --format=cjs --entry=src/vendor-*.ts --bundle=all",
6969
"build:dts": "tsc --project tsconfig.prod.json && node scripts/build-dts.mjs",
7070
"pretest": "npm run build",
7171
"test": "npm run test:unit && npm run test:types && npm run test:license",
@@ -81,7 +81,7 @@
8181
"test:smoke:cjs": "node ./test/smoke/node.test.cjs",
8282
"test:smoke:mjs": "node ./test/smoke/node.test.mjs",
8383
"test:smoke:deno": "deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run",
84-
"coverage": "c8 -x build/deno.js -x build/vendor.cjs -x build/core-vendor.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
84+
"coverage": "c8 -x build/deno.js -x build/vendor-extra.cjs -x build/vendor-core.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
8585
"version": "cat package.json | fx .version"
8686
},
8787
"optionalDependencies": {

scripts/build-dts.mjs

+44-23
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ import fs from 'fs/promises'
1818
import { generateDtsBundle } from 'dts-bundle-generator'
1919
import glob from 'fast-glob'
2020

21-
const entry = {
22-
filePath: './src/vendor.ts',
23-
outFile: './build/vendor.d.ts',
21+
const output = {
22+
inlineDeclareExternals: true,
23+
inlineDeclareGlobals: true,
24+
sortNodes: false,
25+
exportReferencedTypes: false, //args['export-referenced-types'],
26+
}
27+
const entries = [{
28+
filePath: './src/vendor-extra.ts',
29+
outFile: './build/vendor-extra.d.ts',
2430
libraries: {
2531
allowedTypesLibraries: ['node'], // args['external-types'],
2632
inlinedLibraries: [
@@ -30,30 +36,39 @@ const entry = {
3036
'fast-glob',
3137
'@types/jsonfile',
3238
'node-fetch-native',
33-
'chalk',
39+
// 'chalk',
3440
'globby',
35-
'@types/minimist',
36-
'@types/which',
37-
'zurk',
38-
'@webpod/ps',
41+
// '@types/minimist',
42+
// '@types/which',
43+
// 'zurk',
44+
// '@webpod/ps',
3945
'@webpod/ingrid',
4046
'depseek',
4147
], // args['external-inlines'],
4248
},
43-
output: {
44-
inlineDeclareExternals: true,
45-
inlineDeclareGlobals: true,
46-
sortNodes: false,
47-
exportReferencedTypes: false, //args['export-referenced-types'],
49+
output,
50+
}, {
51+
filePath: './src/vendor-core.ts',
52+
outFile: './build/vendor-core.d.ts',
53+
libraries: {
54+
allowedTypesLibraries: ['node'], // args['external-types'],
55+
inlinedLibraries: [
56+
'@types/which',
57+
'@webpod/ps',
58+
'@webpod/ingrid',
59+
'chalk',
60+
'zurk',
61+
], // args['external-inlines'],
4862
},
49-
}
63+
output,
64+
}]
5065

5166
const compilationOptions = {
5267
preferredConfigPath: './tsconfig.prod.json', // args.project,
5368
followSymlinks: true,
5469
}
5570

56-
let [result] = generateDtsBundle([entry], compilationOptions)
71+
const results = generateDtsBundle(entries, compilationOptions)
5772

5873
// generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually:
5974
/*
@@ -65,17 +80,23 @@ build/vendor.d.ts(167,7): error TS2456: Type alias 'FileSystemAdapter' circularl
6580
build/vendor.d.ts(197,48): error TS2694: Namespace 'FastGlob' has no exported member 'FastGlobOptions
6681
*/
6782

68-
result = result
69-
.replace('type Options = Options;', 'export {Options};')
70-
.replace('type Task = Task;', 'export {Task};')
71-
.replace('type Pattern = Pattern;', 'export {Pattern};')
72-
.replace('FastGlob.FastGlobOptions', 'FastGlob.Options')
73-
.replace('type Entry =', 'export type Entry =')
83+
.map(r => r
84+
.replace('type Options = Options;', 'export {Options};')
85+
.replace('type Task = Task;', 'export {Task};')
86+
.replace('type Pattern = Pattern;', 'export {Pattern};')
87+
.replace('FastGlob.FastGlobOptions', 'FastGlob.Options')
88+
.replace('type Entry =', 'export type Entry =')
89+
)
7490

75-
await fs.writeFile(entry.outFile, result, 'utf8')
91+
for (const i in results) {
92+
const entry = entries[i]
93+
const result = results[i]
94+
95+
await fs.writeFile(entry.outFile, result, 'utf8')
96+
}
7697

7798
// Replaces redundant triple-slash directives
78-
for (const dts of await glob(['build/**/*.d.ts', '!build/vendor.d.ts'])) {
99+
for (const dts of await glob(['build/**/*.d.ts', '!build/vendor.d.ts', '!build/core-vendor.d.ts'])) {
79100
const contents = (await fs.readFile(dts, 'utf8'))
80101
.split('\n')
81102
.filter((line) => !line.startsWith('/// <reference types'))

src/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
type RequestInfo,
3535
type RequestInit,
3636
type TSpawnStore,
37-
} from './core-vendor.js'
37+
} from './vendor-core.js'
3838
import {
3939
type Duration,
4040
errnoMessage,

src/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import os from 'node:os'
1616
import path from 'node:path'
1717
import fs from 'node:fs'
18-
import { chalk } from './core-vendor.js'
18+
import { chalk } from './vendor-core.js'
1919

2020
export function tempdir(prefix = `zx-${randomId()}`) {
2121
const dirpath = path.join(os.tmpdir(), prefix)
File renamed without changes.

src/vendor-extra.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { URL } from 'node:url'
16+
import {
17+
convertPathToPattern,
18+
globby,
19+
globbySync,
20+
globbyStream,
21+
generateGlobTasksSync,
22+
generateGlobTasks,
23+
isGitIgnoredSync,
24+
isGitIgnored,
25+
isDynamicPattern,
26+
type Options as GlobbyOptions,
27+
} from 'globby'
28+
import * as yaml from 'yaml'
29+
import * as _fs from 'fs-extra'
30+
import _createRequire from 'create-require'
31+
import { type fetch, AbortController } from 'node-fetch-native'
32+
33+
export { fetch as nodeFetch } from 'node-fetch-native'
34+
35+
global.AbortController = global.AbortController || AbortController
36+
37+
export const createRequire = _createRequire as unknown as (
38+
filename: string | URL
39+
) => NodeRequire
40+
41+
export const globbyModule = {
42+
convertPathToPattern,
43+
globby,
44+
globbySync,
45+
globbyStream,
46+
generateGlobTasksSync,
47+
generateGlobTasks,
48+
isGitIgnoredSync,
49+
isGitIgnored,
50+
isDynamicPattern,
51+
}
52+
53+
export const glob = Object.assign(function globby(
54+
patterns: string | readonly string[],
55+
options?: GlobbyOptions
56+
) {
57+
return globbyModule.globby(patterns, options)
58+
}, globbyModule) as (typeof globbyModule)['globby'] & typeof globbyModule
59+
60+
export const YAML: {
61+
parse(text: string): any
62+
stringify(object: any): string
63+
} = yaml
64+
65+
export const fs: typeof import('fs-extra') = _fs
66+
67+
export { depseekSync as depseek } from 'depseek'
68+
export { default as minimist } from 'minimist'

src/vendor.ts

+2-56
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,5 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { URL } from 'node:url'
16-
import {
17-
convertPathToPattern,
18-
globby,
19-
globbySync,
20-
globbyStream,
21-
generateGlobTasksSync,
22-
generateGlobTasks,
23-
isGitIgnoredSync,
24-
isGitIgnored,
25-
isDynamicPattern,
26-
type Options as GlobbyOptions,
27-
} from 'globby'
28-
import * as yaml from 'yaml'
29-
import * as _fs from 'fs-extra'
30-
import _createRequire from 'create-require'
31-
import { type fetch, AbortController } from 'node-fetch-native'
32-
33-
export * from './core-vendor.js'
34-
35-
export { fetch as nodeFetch } from 'node-fetch-native'
36-
37-
global.AbortController = global.AbortController || AbortController
38-
39-
export const createRequire = _createRequire as unknown as (
40-
filename: string | URL
41-
) => NodeRequire
42-
43-
export const globbyModule = {
44-
convertPathToPattern,
45-
globby,
46-
globbySync,
47-
globbyStream,
48-
generateGlobTasksSync,
49-
generateGlobTasks,
50-
isGitIgnoredSync,
51-
isGitIgnored,
52-
isDynamicPattern,
53-
}
54-
55-
export const glob = Object.assign(function globby(
56-
patterns: string | readonly string[],
57-
options?: GlobbyOptions
58-
) {
59-
return globbyModule.globby(patterns, options)
60-
}, globbyModule) as (typeof globbyModule)['globby'] & typeof globbyModule
61-
62-
export const YAML: {
63-
parse(text: string): any
64-
stringify(object: any): string
65-
} = yaml
66-
67-
export const fs: typeof import('fs-extra') = _fs
68-
69-
export { depseekSync as depseek } from 'depseek'
70-
export { default as minimist } from 'minimist'
15+
export * from './vendor-core.js'
16+
export * from './vendor-extra.js'

0 commit comments

Comments
 (0)