Skip to content

Commit 4c0dae6

Browse files
authored
chore(custom-resource-handlers): refactor minify and bundling step (#26287)
Make `minify-and-bundle-sources.ts` more clear by putting the bundled files into its own `dist` folder, and copying that `dist` folder into `aws-cdk-lib`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6da9a4c commit 4c0dae6

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

packages/@aws-cdk/custom-resource-handlers/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"version": "0.0.0",
66
"scripts": {
7-
"build": "tsc -b && node scripts/minify-sources.js",
7+
"build": "tsc -b && node scripts/minify-and-bundle-sources.js",
88
"integ": "integ-runner",
99
"lint": "cdk-lint",
1010
"package": "cdk-package",

packages/@aws-cdk/custom-resource-handlers/scripts/minify-sources.ts renamed to packages/@aws-cdk/custom-resource-handlers/scripts/minify-and-bundle-sources.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ function recFolderStructure(fileOrDir: string) {
1010
recFolderStructure(path.join(fileOrDir, i));
1111
}
1212
} else {
13-
if (path.extname(fileOrDir) === '.ts' && !fileOrDir.includes('.d.ts') && !fileOrDir.includes('nodejs-entrypoint')) {
13+
// Only minify + bundle 'index.ts' files.
14+
// The reason why they are called 'index.ts' is that aws-cdk-lib expects that
15+
// as the file name and it is more intuitive to keep the same name rather than
16+
// rename as we copy it out.
17+
if (fileOrDir.includes('index.ts')) {
1418
entryPoints.push(fileOrDir);
1519
}
1620
}
@@ -23,7 +27,7 @@ recFolderStructure(bindingsDir);
2327
for (const ep of entryPoints) {
2428
void esbuild.build({
2529
entryPoints: [ep],
26-
outfile: `${ep.slice(0, ep.lastIndexOf('.'))}.js`,
30+
outfile: calculateOutfile(ep),
2731
external: ['@aws-sdk/*'],
2832
format: 'cjs',
2933
platform: 'node',
@@ -36,3 +40,14 @@ for (const ep of entryPoints) {
3640
tsconfig: 'tsconfig.json',
3741
});
3842
}
43+
44+
function calculateOutfile(file: string) {
45+
// turn ts extension into js extension
46+
file = path.join(path.dirname(file), path.basename(file, path.extname(file)) + '.js');
47+
48+
// replace /lib with /dist
49+
const fileContents = file.split(path.sep);
50+
fileContents[fileContents.lastIndexOf('lib')] = 'dist';
51+
52+
return fileContents.join(path.sep);
53+
}

packages/aws-cdk-lib/aws-ecr/lib/repository.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ export class Repository extends RepositoryBase {
860860
private enableAutoDeleteImages() {
861861
const firstTime = Stack.of(this).node.tryFindChild(`${AUTO_DELETE_IMAGES_RESOURCE_TYPE}CustomResourceProvider`) === undefined;
862862
const provider = CustomResourceProvider.getOrCreateProvider(this, AUTO_DELETE_IMAGES_RESOURCE_TYPE, {
863-
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'lib', 'aws-ecr', 'auto-delete-images-handler'),
863+
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'dist', 'aws-ecr', 'auto-delete-images-handler'),
864864
useCfnResponseWrapper: false,
865865
runtime: CustomResourceProviderRuntime.NODEJS_18_X,
866866
description: `Lambda function for auto-deleting images in ${this.repositoryName} repository.`,

packages/aws-cdk-lib/aws-s3/lib/bucket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ export class Bucket extends BucketBase {
24272427

24282428
private enableAutoDeleteObjects() {
24292429
const provider = CustomResourceProvider.getOrCreateProvider(this, AUTO_DELETE_OBJECTS_RESOURCE_TYPE, {
2430-
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'lib', 'aws-s3', 'auto-delete-objects-handler'),
2430+
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'dist', 'aws-s3', 'auto-delete-objects-handler'),
24312431
useCfnResponseWrapper: false,
24322432
runtime: CustomResourceProviderRuntime.NODEJS_18_X,
24332433
description: `Lambda function for auto-deleting objects in ${this.bucketName} S3 bucket.`,

packages/aws-cdk-lib/scripts/airlift-custom-resource-handlers.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ customresourcedir=$(node -p "path.dirname(require.resolve('@aws-cdk/custom-resou
55
awscdklibdir=${scriptdir}/..
66

77
list_custom_resources() {
8-
for file in $customresourcedir/lib/*/*/index.js; do
8+
for file in $customresourcedir/dist/*/*/index.js; do
99
echo $file | rev | cut -d "/" -f 2-4 | rev
1010
done
1111
}

0 commit comments

Comments
 (0)