Skip to content

Commit fe0da50

Browse files
committed
fix(dts-generator): add @types discovery to generate check-compile path
TS6 no longer auto-includes @types packages when the compiler is invoked programmatically. The previous fix (631f1de) only addressed runCheck.ts but the generation path in generate.ts has the same problem — causing "Cannot find namespace 'THREE'" errors for sap.ui.vk in the legacy-free build. Extracted the @types discovery logic into a shared utility (utils/discover-types.ts) and applied it to both generate.ts and runCheck.ts. Also aligned generate.ts module settings to ESNext + Bundler to prevent bare-specifier rejection of ambient declare module declarations.
1 parent 5b81bbb commit fe0da50

5 files changed

Lines changed: 75 additions & 80 deletions

File tree

‎packages/dts-generator/src/generate-from-paths.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,9 @@ export interface GenerateFromPathsConfig {
5656
dependenciesDTSPathForCheck: string;
5757

5858
/**
59-
* Comma-separated list of package names of the libraries on which the currently to-be-built types depends.
60-
*
61-
* This is meant for entire npm packages developed separately (often by others), not sibling libraries built in the same batch.
62-
* E.g. when a custom UI5 control library is built by an application team, then it usually depends on the OpenUI5 types because
63-
* those define the base classes like Control.
64-
* Setting this has the effect that for the TS compilation check, the `types` field of the package.json file will be set to the
65-
* respective package names and any other type packages are no longer considered.
66-
*
67-
* Only needed for the check.
59+
* @deprecated Since 4.0.3. Declared @types/* dependencies are now auto-discovered
60+
* from the nearest package.json. This option is still accepted for backward
61+
* compatibility and merges additively with the discovered types.
6862
*/
6963
dependenciesTypePackagesForCheck?: string;
7064

‎packages/dts-generator/src/generate.ts‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ts from "typescript";
55
import { generateFromObjects, Directives } from "./generate-from-objects.js";
66
import { default as checkCompile } from "./checkCompile/check-compile.js";
77
import { writeFileSafe, loadJSON } from "./utils/file-utils.js";
8+
import { discoverTypes } from "./utils/discover-types.js";
89

910
const loadedCache = new Map();
1011

@@ -98,15 +99,9 @@ export type GenerateConfig = {
9899
dependencyDTSFilesForCheck: string[];
99100

100101
/**
101-
* Array of package names of the libraries on which the currently to-be-built types depends.
102-
*
103-
* This is meant for entire npm packages developed separately (often by others), not sibling libraries built in the same batch.
104-
* E.g. when a custom UI5 control library is built by an application team, then it usually depends on the OpenUI5 types because
105-
* those define the base classes like Control.
106-
* Setting this has the effect that for the TS compilation check, the `types` field of the package.json file will be set to the
107-
* respective package names and any other type packages are no longer considered.
108-
*
109-
* Only needed for the check.
102+
* @deprecated Since 4.0.3. Declared @types/* dependencies are now auto-discovered
103+
* from the nearest package.json. This option is still accepted for backward
104+
* compatibility and merges additively with the discovered types.
110105
*/
111106
dependenciesTypePackagesForCheck?: string[];
112107

@@ -170,23 +165,24 @@ export async function generate({
170165
);
171166

172167
// set up the tsconfig for the test compile
168+
const discovered = discoverTypes();
173169
const tsOptions: ts.BuildOptions = {
174170
noEmit: true,
175171
noImplicitAny: true,
176172
strict: true,
177173
target: ts.ScriptTarget.ES2015,
178-
module: ts.ModuleKind.ES2015,
174+
module: ts.ModuleKind.ESNext,
175+
moduleResolution: ts.ModuleResolutionKind.Bundler,
179176
lib: ["lib.es2015.d.ts", "lib.dom.d.ts"],
177+
...(discovered.typeRoots.length > 0 && {
178+
typeRoots: discovered.typeRoots,
179+
types: [
180+
...discovered.types,
181+
...(dependenciesTypePackagesForCheck || []),
182+
],
183+
}),
180184
};
181185

182-
// if type dependencies are set, use them
183-
if (
184-
dependenciesTypePackagesForCheck &&
185-
dependenciesTypePackagesForCheck.length > 0
186-
) {
187-
tsOptions.types = dependenciesTypePackagesForCheck;
188-
}
189-
190186
const success = checkCompile({
191187
mainFile: targetFile,
192188
dependencyFiles: dependencyDTSFilesForCheck,

‎packages/dts-generator/src/runCheck.ts‎

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const log = getLogger("@ui5/dts-generator/runCheck");
33
import esMain from "es-main";
44

55
import * as path from "path";
6-
import { promises as fsp, readdirSync, readFileSync } from "fs";
6+
import { promises as fsp } from "fs";
77
const readdir = fsp.readdir;
88

99
import {
@@ -13,6 +13,7 @@ import {
1313
ModuleKind,
1414
ModuleResolutionKind,
1515
} from "./index.js";
16+
import { discoverTypes } from "./utils/discover-types.js";
1617

1718
async function findFiles(dir: string, extension: string) {
1819
if (dir == null) {
@@ -48,52 +49,7 @@ async function main() {
4849

4950
const dtsFiles = await findFiles(dtsDir, "d.ts");
5051

51-
// TS6 no longer auto-includes @types packages; discover which ones are declared
52-
// as dependencies and walk up from CWD to find where they're installed.
53-
const declaredTypes = new Set<string>();
54-
let dir = process.cwd();
55-
while (true) {
56-
try {
57-
const pkg = JSON.parse(
58-
readFileSync(path.join(dir, "package.json"), "utf8"),
59-
);
60-
for (const deps of [pkg.dependencies, pkg.devDependencies]) {
61-
if (deps) {
62-
for (const name of Object.keys(deps)) {
63-
if (name.startsWith("@types/")) {
64-
declaredTypes.add(name.slice("@types/".length));
65-
}
66-
}
67-
}
68-
}
69-
break;
70-
} catch {
71-
// no package.json at this level
72-
}
73-
const parent = path.dirname(dir);
74-
if (parent === dir) break;
75-
dir = parent;
76-
}
77-
78-
const typeRoots: string[] = [];
79-
const types = new Set<string>();
80-
dir = process.cwd();
81-
while (true) {
82-
const candidate = path.join(dir, "node_modules", "@types");
83-
try {
84-
for (const entry of readdirSync(candidate, { withFileTypes: true })) {
85-
if (entry.isDirectory() && declaredTypes.has(entry.name)) {
86-
types.add(entry.name);
87-
}
88-
}
89-
typeRoots.push(candidate);
90-
} catch {
91-
// doesn't exist at this level
92-
}
93-
const parent = path.dirname(dir);
94-
if (parent === dir) break;
95-
dir = parent;
96-
}
52+
const { typeRoots, types } = discoverTypes();
9753

9854
log.verbose(`Running a compile check for ${dtsFiles}`);
9955
const success = checkCompile({
@@ -105,7 +61,7 @@ async function main() {
10561
target: ScriptTarget.ES2015,
10662
module: ModuleKind.ESNext,
10763
moduleResolution: ModuleResolutionKind.Bundler,
108-
...(typeRoots.length > 0 && { typeRoots, types: [...types] }),
64+
...(typeRoots.length > 0 && { typeRoots, types }),
10965
},
11066
});
11167

‎packages/dts-generator/src/utils/arguments-index.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ export const args = (() => {
1919
});
2020
parser.add_argument("--dependenciesTypePackagesForCheck", {
2121
help:
22-
"Comma-separated list of package names of the libraries on which the currently to-be-built types depends. This is meant for entire npm packages" +
23-
" developed separately (often by others), not sibling libraries built in the same generation run. E.g. when a custom UI5 control library is built by an" +
24-
" application team, then it usually depends on the OpenUI5 types because those define the base classes like Control. Setting this has the effect" +
25-
" that for the TS compilation check, the `types` field of the package.json file will be set to the respective package names and any other type packages" +
26-
" are no longer considered. Only needed for the check.",
22+
"[DEPRECATED: @types packages are now auto-discovered from the nearest package.json] " +
23+
"Comma-separated list of additional @types package names to include in the TS compilation check." +
24+
" Typically no longer needed — declared @types/* dependencies are discovered automatically.",
2725
});
2826
parser.add_argument("--directivesPath", {
2927
help: "Directory where the .dtsgenrc files for the libraries (current and dependencies) are located.",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as path from "path";
2+
import { readdirSync, readFileSync } from "fs";
3+
4+
export function discoverTypes(): { typeRoots: string[]; types: string[] } {
5+
const declaredTypes = new Set<string>();
6+
let dir = process.cwd();
7+
while (true) {
8+
try {
9+
const pkg = JSON.parse(
10+
readFileSync(path.join(dir, "package.json"), "utf8"),
11+
);
12+
for (const deps of [pkg.dependencies, pkg.devDependencies]) {
13+
if (deps) {
14+
for (const name of Object.keys(deps)) {
15+
if (name.startsWith("@types/")) {
16+
declaredTypes.add(name.slice("@types/".length));
17+
}
18+
}
19+
}
20+
}
21+
break;
22+
} catch {
23+
// no package.json at this level
24+
}
25+
const parent = path.dirname(dir);
26+
if (parent === dir) break;
27+
dir = parent;
28+
}
29+
30+
const typeRoots: string[] = [];
31+
const types = new Set<string>();
32+
dir = process.cwd();
33+
while (true) {
34+
const candidate = path.join(dir, "node_modules", "@types");
35+
try {
36+
for (const entry of readdirSync(candidate, { withFileTypes: true })) {
37+
if (entry.isDirectory() && declaredTypes.has(entry.name)) {
38+
types.add(entry.name);
39+
}
40+
}
41+
typeRoots.push(candidate);
42+
} catch {
43+
// doesn't exist at this level
44+
}
45+
const parent = path.dirname(dir);
46+
if (parent === dir) break;
47+
dir = parent;
48+
}
49+
50+
return { typeRoots, types: [...types] };
51+
}

0 commit comments

Comments
 (0)