Skip to content

Commit

Permalink
fix: improve instanceof checks to support custom transformers
Browse files Browse the repository at this point in the history
This commit updates two instanceof checks to instead check
the constructor name. This is done to better support custom
transformers. Because custom transformers import/require
potentially different copies of the transformer modules,
the instanceof checks do not work. This required v2 custom
transformers to rely on NODE_PATH hacks and other
workarounds.

Refs: #9362
  • Loading branch information
cjihrig-aws committed Mar 11, 2022
1 parent 3df3af0 commit 8a76661
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export class FileAsset extends cdk.Construct implements cdk.IAsset {
const rootStack = findRootStack(scope);
const sythesizer = rootStack.synthesizer;

if (sythesizer instanceof TransformerStackSythesizer) {
sythesizer.setMappingTemplates(props.fileName, props.fileContent);
// Check the constructor name instead of using 'instanceof' because the latter does not work
// with copies of the class, which happens with custom transformers.
// See: https://github.com/aws-amplify/amplify-cli/issues/9362
if (sythesizer.constructor.name === 'TransformerStackSythesizer') {
(sythesizer as TransformerStackSythesizer).setMappingTemplates(props.fileName, props.fileContent);
this.assetHash = crypto
.createHash('sha256')
.update(props.fileContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,11 @@ export class TransformerResolver implements TransformerResolverProvider {
};

private substitueSlotInfo(template: MappingTemplateProvider, slotName: string, index: number) {
if (template instanceof S3MappingTemplate) {
template.substitueValues({ slotName, slotIndex: index, typeName: this.typeName, fieldName: this.fieldName });
// Check the constructor name instead of using 'instanceof' because the latter does not work
// with copies of the class, which happens with custom transformers.
// See: https://github.com/aws-amplify/amplify-cli/issues/9362
if (template.constructor.name === 'S3MappingTemplate') {
(template as S3MappingTemplate).substitueValues({ slotName, slotIndex: index, typeName: this.typeName, fieldName: this.fieldName });
}
}

Expand Down

0 comments on commit 8a76661

Please sign in to comment.