Skip to content

Commit faffce0

Browse files
authored
feat(aws-cdk-lib): reduce load time of JavaScript library (#27217)
This change halves the load time of `require('aws-cdk-lib')`, from ~600ms to ~300ms on my machine. It also shaves ~8min off running all integ tests (again, on my machine). It does this by making all loading of JavaScript modules lazy, so they are only loaded just before any symbols from them are actually accessed (instead of loading all modules at startup). This new automatic module replaces the previous manual effort to do something similar, in `lazy-index.ts`. Note that this transformation (unfortunately) does not affect TypeScript compilation times: the TypeScript compiler is still going to read all `.d.ts` files. To improve interactive TypeScript performance, use `swc` or `ts-node` with type checking disabled. Fully type checking your application using `tsc` will take the amount of time it takes. ## What's the big idea? Turning this: ```js const x = require("x"); module.exports.banana = function() { return x.hello(); } ``` Into this: ```js var x = () => { var tmp = require("x"); x = () => tmp; return tmp; }; module.exports.banana = function () { return x().hello(); }; ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1be9894 commit faffce0

File tree

32 files changed

+798
-287
lines changed

32 files changed

+798
-287
lines changed

lerna.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"tools/@aws-cdk/prlint",
2121
"tools/@aws-cdk/spec2cdk",
2222
"tools/@aws-cdk/yarn-cling",
23+
"tools/@aws-cdk/lazify",
2324
"scripts/@aws-cdk/script-tests"
2425
],
2526
"rejectCycles": true,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"tools/@aws-cdk/prlint",
8989
"tools/@aws-cdk/spec2cdk",
9090
"tools/@aws-cdk/yarn-cling",
91+
"tools/@aws-cdk/lazify",
9192
"scripts/@aws-cdk/script-tests"
9293
],
9394
"nohoist": [

packages/@aws-cdk-testing/cli-integ/tests/init-javascript/init-javascript.integtest.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs-extra';
13
import { integTest, withTemporaryDirectory, ShellHelper, withPackages } from '../../lib';
24

35
['app', 'sample-app'].forEach(template => {
@@ -13,3 +15,43 @@ import { integTest, withTemporaryDirectory, ShellHelper, withPackages } from '..
1315
await shell.shell(['cdk', 'synth']);
1416
})));
1517
});
18+
19+
integTest('Test importing CDK from ESM', withTemporaryDirectory(withPackages(async (context) => {
20+
// Use 'cdk init -l=javascript' to get set up, but use a different file
21+
const shell = ShellHelper.fromContext(context);
22+
await context.packages.makeCliAvailable();
23+
24+
await shell.shell(['cdk', 'init', '-l', 'javascript', 'app']);
25+
26+
// Rewrite some files
27+
await fs.writeFile(path.join(context.integTestDir, 'new-entrypoint.mjs'), `
28+
// Test two styles of imports
29+
import { Stack, aws_sns as sns, aws_sns_subscriptions as subs, aws_sqs as sqs } from 'aws-cdk-lib';
30+
import * as cdk from 'aws-cdk-lib';
31+
32+
class TestjsStack extends Stack {
33+
constructor(scope, id, props) {
34+
super(scope, id, props);
35+
36+
const queue = new sqs.Queue(this, 'TestjsQueue', {
37+
visibilityTimeout: cdk.Duration.seconds(300)
38+
});
39+
40+
const topic = new sns.Topic(this, 'TestjsTopic');
41+
42+
topic.addSubscription(new subs.SqsSubscription(queue));
43+
}
44+
}
45+
46+
const app = new cdk.App();
47+
new TestjsStack(app, 'TestjsStack');
48+
`, { encoding: 'utf-8' });
49+
50+
// Rewrite 'cdk.json' to use new entrypoint
51+
const cdkJson = await fs.readJson(path.join(context.integTestDir, 'cdk.json'));
52+
cdkJson.app = 'node new-entrypoing.mjs';
53+
await fs.writeJson(path.join(context.integTestDir, 'cdk.json'), cdkJson);
54+
55+
await shell.shell(['cdk', 'synth']);
56+
57+
})));

packages/aws-cdk-lib/aws-ec2/lib/restrict-default-security-group-handler/.is_custom_resource

Whitespace-only changes.

packages/aws-cdk-lib/aws-iam/lib/oidc-provider/.is_custom_resource

Whitespace-only changes.

packages/aws-cdk-lib/aws-lambda/test/code.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ describe('code', () => {
440440
// then
441441
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
442442
Metadata: {
443-
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.7bbf7edf9881819a1b91e5b02acae3e3973f96fa93325c676a1285351ddacc62',
443+
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: Match.stringLikeRegexp('asset\\.[0-9a-f]+'),
444444
[cxapi.ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY]: false,
445445
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code',
446446
},

packages/aws-cdk-lib/aws-lambda/test/docker-build-lambda/.is_custom_resource

Whitespace-only changes.

packages/aws-cdk-lib/aws-logs/lib/log-retention-provider/.is_custom_resource

Whitespace-only changes.

packages/aws-cdk-lib/aws-route53/lib/cross-account-zone-delegation-handler/.is_custom_resource

Whitespace-only changes.

packages/aws-cdk-lib/aws-route53/lib/delete-existing-record-set-handler/.is_custom_resource

Whitespace-only changes.

0 commit comments

Comments
 (0)