Skip to content

Commit 02738a8

Browse files
committedJun 20, 2024
build: enhance dts gen
1 parent dd59a23 commit 02738a8

File tree

7 files changed

+141
-99
lines changed

7 files changed

+141
-99
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

+67-39
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,62 @@ 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',
24-
libraries: {
25-
allowedTypesLibraries: ['node'], // args['external-types'],
26-
inlinedLibraries: [
27-
'@nodelib/fs.stat',
28-
'@nodelib/fs.scandir',
29-
'@nodelib/fs.walk',
30-
'fast-glob',
31-
'@types/jsonfile',
32-
'node-fetch-native',
33-
'chalk',
34-
'globby',
35-
'@types/minimist',
36-
'@types/which',
37-
'zurk',
38-
'@webpod/ps',
39-
'@webpod/ingrid',
40-
'depseek',
41-
], // args['external-inlines'],
21+
const output = {
22+
inlineDeclareExternals: true,
23+
inlineDeclareGlobals: true,
24+
sortNodes: false,
25+
exportReferencedTypes: false, //args['export-referenced-types'],
26+
}
27+
const entries = [
28+
{
29+
filePath: './src/vendor-extra.ts',
30+
outFile: './build/vendor-extra.d.ts',
31+
libraries: {
32+
allowedTypesLibraries: ['node'], // args['external-types'],
33+
inlinedLibraries: [
34+
'@nodelib/fs.stat',
35+
'@nodelib/fs.scandir',
36+
'@nodelib/fs.walk',
37+
'fast-glob',
38+
'@types/jsonfile',
39+
'node-fetch-native',
40+
// 'chalk',
41+
'globby',
42+
// '@types/minimist',
43+
// '@types/which',
44+
// 'zurk',
45+
// '@webpod/ps',
46+
'@webpod/ingrid',
47+
'depseek',
48+
], // args['external-inlines'],
49+
},
50+
output,
4251
},
43-
output: {
44-
inlineDeclareExternals: true,
45-
inlineDeclareGlobals: true,
46-
sortNodes: false,
47-
exportReferencedTypes: false, //args['export-referenced-types'],
52+
{
53+
filePath: './src/vendor-core.ts',
54+
outFile: './build/vendor-core.d.ts',
55+
libraries: {
56+
allowedTypesLibraries: ['node'], // args['external-types'],
57+
inlinedLibraries: [
58+
'@types/which',
59+
'@webpod/ps',
60+
'@webpod/ingrid',
61+
'chalk',
62+
'zurk',
63+
], // args['external-inlines'],
64+
},
65+
output,
4866
},
49-
}
67+
]
5068

5169
const compilationOptions = {
5270
preferredConfigPath: './tsconfig.prod.json', // args.project,
5371
followSymlinks: true,
5472
}
5573

56-
let [result] = generateDtsBundle([entry], compilationOptions)
57-
58-
// generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually:
59-
/*
74+
const results = generateDtsBundle(entries, compilationOptions)
75+
// generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually:
76+
/*
6077
build/vendor.d.ts(163,7): error TS2456: Type alias 'Options' circularly references itself.
6178
build/vendor.d.ts(164,7): error TS2456: Type alias 'Entry' circularly references itself.
6279
build/vendor.d.ts(165,7): error TS2456: Type alias 'Task' circularly references itself.
@@ -65,17 +82,28 @@ build/vendor.d.ts(167,7): error TS2456: Type alias 'FileSystemAdapter' circularl
6582
build/vendor.d.ts(197,48): error TS2694: Namespace 'FastGlob' has no exported member 'FastGlobOptions
6683
*/
6784

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 =')
85+
.map((r) =>
86+
r
87+
.replace('type Options = Options;', 'export {Options};')
88+
.replace('type Task = Task;', 'export {Task};')
89+
.replace('type Pattern = Pattern;', 'export {Pattern};')
90+
.replace('FastGlob.FastGlobOptions', 'FastGlob.Options')
91+
.replace('type Entry =', 'export type Entry =')
92+
)
7493

75-
await fs.writeFile(entry.outFile, result, 'utf8')
94+
for (const i in results) {
95+
const entry = entries[i]
96+
const result = results[i]
97+
98+
await fs.writeFile(entry.outFile, result, 'utf8')
99+
}
76100

77101
// Replaces redundant triple-slash directives
78-
for (const dts of await glob(['build/**/*.d.ts', '!build/vendor.d.ts'])) {
102+
for (const dts of await glob([
103+
'build/**/*.d.ts',
104+
'!build/vendor.d.ts',
105+
'!build/core-vendor.d.ts',
106+
])) {
79107
const contents = (await fs.readFile(dts, 'utf8'))
80108
.split('\n')
81109
.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)