@@ -46,6 +46,7 @@ export interface StaticSiteProps {
4646 readonly s3Bucket ?: s3 . BucketProps ;
4747 readonly cfDistribution ?: StaticSiteCdkDistributionProps ;
4848 readonly environment ?: { [ key : string ] : string } ;
49+ readonly purgeFiles ?: boolean ;
4950 readonly disablePlaceholder ?: boolean ;
5051 readonly waitForInvalidation ?: boolean ;
5152}
@@ -68,6 +69,7 @@ export class StaticSite extends Construct implements SSTConstruct {
6869 private readonly props : StaticSiteProps ;
6970 private readonly isPlaceholder : boolean ;
7071 private readonly assets : s3Assets . Asset [ ] ;
72+ private readonly filenamesAsset ?: s3Assets . Asset ;
7173 private readonly awsCliLayer : AwsCliLayer ;
7274
7375 constructor ( scope : Construct , id : string , props : StaticSiteProps ) {
@@ -92,7 +94,9 @@ export class StaticSite extends Construct implements SSTConstruct {
9294 this . validateCustomDomainSettings ( ) ;
9395
9496 // Build app
95- this . assets = this . buildApp ( fileSizeLimit , buildDir ) ;
97+ this . buildApp ( ) ;
98+ this . assets = this . bundleAssets ( fileSizeLimit , buildDir ) ;
99+ this . filenamesAsset = this . bundleFilenamesAsset ( buildDir ) ;
96100
97101 // Create Bucket
98102 this . s3Bucket = this . createS3Bucket ( ) ;
@@ -159,17 +163,12 @@ export class StaticSite extends Construct implements SSTConstruct {
159163 } ;
160164 }
161165
162- private buildApp ( fileSizeLimit : number , buildDir : string ) : s3Assets . Asset [ ] {
166+ private buildApp ( ) {
163167 if ( this . isPlaceholder ) {
164- return [
165- new s3Assets . Asset ( this , "Asset" , {
166- path : path . resolve ( __dirname , "../assets/StaticSite/stub" ) ,
167- } ) ,
168- ] ;
168+ return ;
169169 }
170170
171171 const { path : sitePath , buildCommand } = this . props ;
172- const buildOutput = this . props . buildOutput || "." ;
173172
174173 // validate site path exists
175174 if ( ! fs . existsSync ( sitePath ) ) {
@@ -180,8 +179,6 @@ export class StaticSite extends Construct implements SSTConstruct {
180179 ) ;
181180 }
182181
183- // Build and package user's website
184-
185182 // build
186183 if ( buildCommand ) {
187184 try {
@@ -200,6 +197,22 @@ export class StaticSite extends Construct implements SSTConstruct {
200197 ) ;
201198 }
202199 }
200+ }
201+
202+ private bundleAssets (
203+ fileSizeLimit : number ,
204+ buildDir : string
205+ ) : s3Assets . Asset [ ] {
206+ if ( this . isPlaceholder ) {
207+ return [
208+ new s3Assets . Asset ( this , "Asset" , {
209+ path : path . resolve ( __dirname , "../assets/StaticSite/stub" ) ,
210+ } ) ,
211+ ] ;
212+ }
213+
214+ const { path : sitePath , buildCommand } = this . props ;
215+ const buildOutput = this . props . buildOutput || "." ;
203216
204217 // validate buildOutput exists
205218 const siteOutputPath = path . resolve ( path . join ( sitePath , buildOutput ) ) ;
@@ -248,6 +261,31 @@ export class StaticSite extends Construct implements SSTConstruct {
248261 return assets ;
249262 }
250263
264+ private bundleFilenamesAsset ( buildDir : string ) : s3Assets . Asset | undefined {
265+ if ( this . isPlaceholder ) {
266+ return ;
267+ }
268+ if ( this . props . purgeFiles === false ) {
269+ return ;
270+ }
271+
272+ const zipPath = path . resolve (
273+ path . join ( buildDir , `StaticSite-${ this . node . id } -${ this . node . addr } ` )
274+ ) ;
275+
276+ // create assets
277+ const filenamesPath = path . join ( zipPath , `filenames` ) ;
278+ if ( ! fs . existsSync ( filenamesPath ) ) {
279+ throw new Error (
280+ `There was a problem generating the "${ this . node . id } " StaticSite package.`
281+ ) ;
282+ }
283+
284+ return new s3Assets . Asset ( this , `AssetFilenames` , {
285+ path : filenamesPath ,
286+ } ) ;
287+ }
288+
251289 private createS3Bucket ( ) : s3 . Bucket {
252290 let { s3Bucket } = this . props ;
253291 s3Bucket = s3Bucket || { } ;
@@ -304,6 +342,7 @@ export class StaticSite extends Construct implements SSTConstruct {
304342 } ,
305343 } ) ;
306344 this . s3Bucket . grantReadWrite ( handler ) ;
345+ this . filenamesAsset ?. grantRead ( handler ) ;
307346 uploader . grantInvoke ( handler ) ;
308347
309348 // Create custom resource
@@ -316,6 +355,10 @@ export class StaticSite extends Construct implements SSTConstruct {
316355 ObjectKey : asset . s3ObjectKey ,
317356 } ) ) ,
318357 DestinationBucketName : this . s3Bucket . bucketName ,
358+ Filenames : this . filenamesAsset && {
359+ BucketName : this . filenamesAsset . s3BucketName ,
360+ ObjectKey : this . filenamesAsset . s3ObjectKey ,
361+ } ,
319362 FileOptions : ( fileOptions || [ ] ) . map (
320363 ( { exclude, include, cacheControl } ) => {
321364 if ( typeof exclude === "string" ) {
0 commit comments