-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cx-api): break circular dependencies (#18767)
Use the "Declaration Merging" feature from TypeScript, combined with the "Monkey Patching" feature from JavaScript, to break the circular dependencies between some of the classes in this library. These dependency cycles interfere with proper bundling by `esbuild`. Use `madge` to validate that we don't add those cycles back. Madge validation only happens on the `.js` files, because those are the only ones we care about when bundling. Cyclic imports that are only used to import type information won't actually cause `require()` cycles at runtime, since types don't exist in JS. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
8 changed files
with
565 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/@aws-cdk/cx-api/lib/artifacts/nested-cloud-assembly-artifact-aug.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { CloudAssembly } from '../cloud-assembly'; | ||
import { NestedCloudAssemblyArtifact } from './nested-cloud-assembly-artifact'; | ||
|
||
const cacheSym = Symbol(); | ||
|
||
/** | ||
* The nested Assembly | ||
* | ||
* Declared in a different file to break circular dep between CloudAssembly and NestedCloudAssemblyArtifact | ||
*/ | ||
Object.defineProperty(NestedCloudAssemblyArtifact.prototype, 'nestedAssembly', { | ||
get() { | ||
if (!this[cacheSym]) { | ||
this[cacheSym] = new CloudAssembly(this.fullPath); | ||
} | ||
return this[cacheSym]; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as cxschema from '@aws-cdk/cloud-assembly-schema'; | ||
import { AssetManifestArtifact } from './artifacts/asset-manifest-artifact'; | ||
import { CloudFormationStackArtifact } from './artifacts/cloudformation-artifact'; | ||
import { NestedCloudAssemblyArtifact } from './artifacts/nested-cloud-assembly-artifact'; | ||
import { TreeCloudArtifact } from './artifacts/tree-cloud-artifact'; | ||
import { CloudArtifact } from './cloud-artifact'; | ||
import { CloudAssembly } from './cloud-assembly'; | ||
|
||
/** | ||
* Add the 'fromManifest' factory function | ||
* | ||
* It is defined in a separate file to avoid circular dependencies between 'cloud-artifact.ts' | ||
* and all of its subclass files. | ||
*/ | ||
CloudArtifact.fromManifest = function fromManifest( | ||
assembly: CloudAssembly, | ||
id: string, | ||
artifact: cxschema.ArtifactManifest, | ||
): CloudArtifact | undefined { | ||
switch (artifact.type) { | ||
case cxschema.ArtifactType.AWS_CLOUDFORMATION_STACK: | ||
return new CloudFormationStackArtifact(assembly, id, artifact); | ||
case cxschema.ArtifactType.CDK_TREE: | ||
return new TreeCloudArtifact(assembly, id, artifact); | ||
case cxschema.ArtifactType.ASSET_MANIFEST: | ||
return new AssetManifestArtifact(assembly, id, artifact); | ||
case cxschema.ArtifactType.NESTED_CLOUD_ASSEMBLY: | ||
return new NestedCloudAssemblyArtifact(assembly, id, artifact); | ||
default: | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.