Skip to content

Commit 04cdb98

Browse files
authored
fix(toolkit-lib): ToolkitError is not exported (#187)
Fixes `'@aws-cdk/toolkit-lib' has no exported member named 'ToolkitError'` Because we are now moving parts of the public API into a shared package, we also need to export the respective types. During the regular build, type declarations are not bundled. So we need to take care of it afterwards. This is not ideal, but required. Thankfully the temporary package is a, well, temporary construct and we are actively working to get rid of it (first by adding new stuff, then by moving everything to `toolkit-lib`). --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 3710b78 commit 04cdb98

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

.projenrc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ const toolkitLib = configureProject(
11741174
tmpToolkitHelpers,
11751175
'aws-cdk-lib',
11761176
'aws-sdk-client-mock',
1177+
'dts-bundle-generator',
11771178
'esbuild',
11781179
'typedoc',
11791180
],

packages/@aws-cdk/toolkit-lib/.projen/deps.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/build-tools/bundle.mjs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,53 @@ import { createRequire } from 'node:module';
22
import * as path from 'node:path';
33
import * as esbuild from 'esbuild';
44
import * as fs from 'fs-extra';
5+
import { generateDtsBundle } from 'dts-bundle-generator';
56

7+
// copy files
68
const require = createRequire(import.meta.url);
7-
89
const cliPackage = path.dirname(require.resolve('aws-cdk/package.json'));
9-
let copyFromCli = (from, to = undefined) => {
10+
const copyFromCli = (from, to = undefined) => {
1011
return fs.copy(path.join(cliPackage, ...from), path.join(process.cwd(), ...(to ?? from)));
1112
};
1213

14+
// declaration bundling
15+
const bundleDeclarations = async (entryPoints) => {
16+
const results = generateDtsBundle(entryPoints.map(filePath => ({
17+
filePath,
18+
output: {
19+
noBanner: true,
20+
exportReferencedTypes: false,
21+
},
22+
})), { preferredConfigPath: 'tsconfig.dev.json' });
23+
24+
const files = [];
25+
for (const [idx, declaration] of results.entries()) {
26+
const outputPath = path.format({ ...path.parse(entryPoints[idx]), base: '', ext: '.d.ts' });
27+
files.push(fs.promises.writeFile(outputPath, declaration));
28+
}
29+
30+
return Promise.all(files);
31+
}
32+
33+
1334
// This is a build script, we are fine
1435
// eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism
1536
await Promise.all([
1637
copyFromCli(['build-info.json']),
1738
copyFromCli(['/db.json.gz']),
1839
copyFromCli(['lib', 'index_bg.wasm']),
1940
copyFromCli(['lib', 'api', 'bootstrap', 'bootstrap-template.yaml']),
20-
]);
2141

22-
// # Copy all resources that aws_cdk/generate.sh produced, and some othersCall the generator for the
23-
// cp -R $aws_cdk/lib/init-templates ./lib/
42+
// cdk init is not yet available in the toolkit-lib
43+
// copyFromCli(['lib', 'init-templates']),
44+
]);
2445

46+
// bundle entrypoints from the library packages
2547
await esbuild.build({
2648
outdir: 'lib',
2749
entryPoints: [
2850
'lib/api/aws-cdk.ts',
29-
'lib/api/shared-public.ts',
51+
'lib/api/shared-public.ts',
3052
'lib/private/util.ts',
3153
],
3254
target: 'node18',
@@ -35,3 +57,6 @@ await esbuild.build({
3557
sourcemap: true,
3658
bundle: true,
3759
});
60+
61+
// for the shared public API we also need to bundle the types
62+
await bundleDeclarations(['lib/api/shared-public.ts']);

packages/@aws-cdk/toolkit-lib/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yarn.lock

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)