Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot have multiple assets in the same scope (backport #1016) #1018

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@
ISource,
SourceConfig,
} from 'aws-cdk-lib/aws-s3-deployment';
<<<<<<< HEAD

Check failure on line 7 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { Construct } from 'constructs';
import { AssetBaseProps, AssetProps, JavaScriptAsset, TypeScriptAsset } from './asset';
=======

Check failure on line 10 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { Construct, IConstruct } from 'constructs';
import { TypeScriptAsset, TypeScriptAssetProps } from './asset';
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 13 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';

<<<<<<< HEAD

Check failure on line 17 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
export interface JavaScriptSourceProps extends AssetBaseProps{};
export interface TypeScriptSourceProps extends AssetBaseProps{};
=======

Check failure on line 20 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
const assetIds = new WeakMap<IConstruct, number>();
const assetId = (scope: IConstruct, name: string) => {
const nextId = (assetIds.get(scope) ?? 0) + 1;
assetIds.set(scope, nextId);

// Only one asset per scope, skip the id
if (nextId === 1) {
return name;
}

return `${name}${nextId}`;
};

export interface TypeScriptSourceProps extends TypeScriptCodeProps {};
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 35 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.

abstract class Source<
Props extends JavaScriptSourceProps | TypeScriptSourceProps,
Expand Down Expand Up @@ -181,6 +203,55 @@
*/
props: TypeScriptSourceProps = {},
) {
<<<<<<< HEAD

Check failure on line 206 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
super(entryPoints, props);
=======

Check failure on line 208 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
const defaultOptions: Partial<BuildOptions> = {
platform: 'browser',
};

this.props = {
entryPoints,
...props,
buildOptions: {
...defaultOptions,
...props.buildOptions,
},
};
}


bind(scope: Construct, context?: DeploymentSourceContext): SourceConfig {
// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new TypeScriptAsset(
scope,
assetId(scope, this.constructor.name),
this.props,
);
} else if (Stack.of(this.asset) !== Stack.of(scope)) {
throw new Error(
`Asset is already associated with another stack '${
Stack.of(this.asset).stackName
}'. ` + 'Create a new Asset instance for every stack.',
);
}

if (!context) {
throw new Error(
`To use a ${this.constructor.name}, context must be provided`,
);
}

// we give permissions on all files in the bucket since we don't want to
// accidentally revoke permission on old versions when deploying a new
// version (for example, when using Lambda traffic shifting).
this.asset.bucket.grantRead(context.handlerRole);

return {
bucket: this.asset.bucket,
zipObjectKey: this.asset.s3ObjectKey,
};
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 255 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
}
}
32 changes: 32 additions & 0 deletions test/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,36 @@ describe('source', () => {
expect(source.props.buildOptions.platform).toBe('browser');
});
});

describe('with multiple sources in the same scope', () => {
it('does not throw', () => {
const stack = new Stack();

const sourceOne = new TypeScriptSource('fixtures/handlers/ts-handler.ts', {
buildOptions: {
absWorkingDir: resolve(__dirname),
},
});
const sourceTwo = new TypeScriptSource('fixtures/handlers/ts-handler.ts', {
buildOptions: {
absWorkingDir: resolve(__dirname),
},
});

const destinationBucket = new Bucket(stack, 'WebsiteBucket', {
autoDeleteObjects: true,
publicReadAccess: true,
removalPolicy: RemovalPolicy.DESTROY,
websiteIndexDocument: 'index.html',
});

new BucketDeployment(stack, 'MultipleAssets', {
destinationBucket,
sources: [
sourceOne,
sourceTwo,
],
});
});
});
});
Loading