cdk-booster 1.1.0-alpha.1 → 1.1.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,9 @@ async function run() {
53
53
  compileCodeFile,
54
54
  });
55
55
  Logger.verbose(`Found ${lambdas.length} Lambda functions in the CDK code:`, JSON.stringify(lambdas, null, 2));
56
- const lambdasEsBuildCommands = lambdas;
56
+ let lambdasEsBuildCommands = lambdas;
57
+ // skip all lambdas that have SKIP_CDK_BOOSTER env var set to true in their bundling environment
58
+ lambdasEsBuildCommands = lambdasEsBuildCommands.filter((lambda) => lambda.environment?.SKIP_CDK_BOOSTER !== 'true');
57
59
  // Prepare bundling temp folders for each Lambda function
58
60
  await recreateBundlingTempFolders(lambdasEsBuildCommands);
59
61
  // Execute pre-bundling commands
@@ -97,9 +99,10 @@ async function bundle(lambdasEsBuildCommands) {
97
99
  for (const buildBatch of buildBatches) {
98
100
  const build = async () => {
99
101
  parallelCount++;
102
+ let context;
103
+ const buildOptions = buildBatch.buildOptions;
104
+ const entryPoints = buildBatch.entryPoints;
100
105
  try {
101
- const buildOptions = buildBatch[0].buildOptions;
102
- const entryPoints = buildBatch.map((b) => b.entryPoint);
103
106
  const normalizedEsbuildArgs = normalizeEsbuildArgs(buildOptions.esbuildArgs);
104
107
  const esBuildOpt = {
105
108
  entryPoints,
@@ -136,13 +139,52 @@ async function bundle(lambdasEsBuildCommands) {
136
139
  else {
137
140
  Logger.log(`Bundling:\n - ${entryPoints.join('\n - ')}`);
138
141
  }
139
- const buildingResults = await esbuild.build(esBuildOpt);
142
+ // Create esbuild context for this build batch
143
+ context = await esbuild.context(esBuildOpt);
144
+ let buildingResults;
145
+ try {
146
+ // Perform the build using the context
147
+ buildingResults = await context.rebuild();
148
+ }
149
+ catch (err) {
150
+ Logger.log(`Error creating esbuild context.`, err);
151
+ // Esbuild’s child process was stopped; recreate the context once.
152
+ if (String(err?.message || err).includes('The service was stopped') &&
153
+ context) {
154
+ Logger.log(`Retrying bundling.`);
155
+ try {
156
+ await context.dispose();
157
+ }
158
+ catch {
159
+ // ignore
160
+ }
161
+ try {
162
+ context = await esbuild.context(esBuildOpt);
163
+ buildingResults = await context.rebuild();
164
+ Logger.log(`Retry successful.`);
165
+ }
166
+ catch (err) {
167
+ Logger.log(`Retry failed.`, err);
168
+ throw err;
169
+ }
170
+ }
171
+ else {
172
+ throw err;
173
+ }
174
+ }
140
175
  outputs = {
141
176
  ...outputs,
142
177
  ...buildingResults.metafile?.outputs,
143
178
  };
144
179
  }
180
+ catch (error) {
181
+ Logger.error(`The following functions failed to bundle:\n - ${entryPoints.join('\n - ')}. Set batch parameter (-b) to a smaller number, like 5, to lower the chance of this error, and in case of error, a smaller batch would be affected.`, error);
182
+ }
145
183
  finally {
184
+ // Always dispose of the context to free resources
185
+ if (context) {
186
+ await context.dispose();
187
+ }
146
188
  parallelCount--;
147
189
  }
148
190
  };
@@ -246,15 +288,24 @@ function createBuildCombinations(lambdasEsBuildCommands) {
246
288
  const uniqueBuildHashes = new Set(buildCombinations.map((b) => b.buildOptionsHash));
247
289
  for (const buildHash of uniqueBuildHashes) {
248
290
  const buildBatch = buildCombinations.filter((b) => b.buildOptionsHash === buildHash);
291
+ let entryPoints = buildBatch.map((b) => b.entryPoint);
292
+ // unique entry points
293
+ entryPoints = Array.from(new Set(entryPoints));
249
294
  // if batch size is set and if each buildOptionsHash has more than batchSize entries, split them
250
- if (batchSize && buildBatch.length > batchSize) {
251
- for (let i = 0; i < buildBatch.length; i += batchSize) {
252
- const chunk = buildBatch.slice(i, i + batchSize);
253
- buildBatches.push(chunk);
295
+ if (batchSize && entryPoints.length > batchSize) {
296
+ for (let i = 0; i < entryPoints.length; i += batchSize) {
297
+ const chunk = entryPoints.slice(i, i + batchSize);
298
+ buildBatches.push({
299
+ entryPoints: chunk,
300
+ buildOptions: buildBatch[0].buildOptions,
301
+ });
254
302
  }
255
303
  }
256
304
  else {
257
- buildBatches.push(buildBatch);
305
+ buildBatches.push({
306
+ entryPoints,
307
+ buildOptions: buildBatch[0].buildOptions,
308
+ });
258
309
  }
259
310
  }
260
311
  return buildBatches;
@@ -1,4 +1,4 @@
1
- import { Command } from 'commander';
1
+ import { Command, InvalidArgumentError } from 'commander';
2
2
  import { getVersion } from './version.mjs';
3
3
  /**
4
4
  * Get configuration from CLI arguments
@@ -10,8 +10,8 @@ export async function getConfigFromCliArgs() {
10
10
  const program = new Command();
11
11
  program.name('cdk-booster').description('CDK Booster').version(version);
12
12
  program.option('-v, --verbose', 'Verbose logging');
13
- program.option('-b, --batch <number>', 'Number of Lambdas bundled in a batch with ESBuild');
14
- program.option('-p, --parallel <number>', 'Number of parallel ESBuild processes. You usually do not need to change this.');
13
+ program.option('-b, --batch <number>', 'Number of Lambdas bundled in a batch with ESBuild', parseInteger);
14
+ program.option('-p, --parallel <number>', 'Number of parallel ESBuild processes. You usually do not need to change this.', parseInteger);
15
15
  program.arguments('<string>');
16
16
  program.parse(process.argv);
17
17
  const args = program.opts();
@@ -23,3 +23,10 @@ export async function getConfigFromCliArgs() {
23
23
  args.entryFile = entryFile;
24
24
  return args;
25
25
  }
26
+ function parseInteger(value) {
27
+ const parsedValue = parseInt(value, 10);
28
+ if (isNaN(parsedValue)) {
29
+ throw new InvalidArgumentError('Not a number.');
30
+ }
31
+ return parsedValue;
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdk-booster",
3
- "version": "1.1.0-alpha.1",
3
+ "version": "1.1.0-alpha.3",
4
4
  "type": "module",
5
5
  "description": "Speed up AWS CDK's bundling of TypeScript/JavaScript Lambda handlers",
6
6
  "homepage": "https://www.cdkbooster.com",
@@ -1,5 +0,0 @@
1
- import { BundleSettings } from './bundleSettings.js';
2
- export type BuildTask = {
3
- buildOptions: BundleSettings;
4
- entryPoint: string;
5
- };
@@ -1 +0,0 @@
1
- export {};