import path from "path" ; import { Option , OptionType , Plugin , PluginContext , SetupResult , globsToMatcher , } from "@miniflare/shared" ; import { FilteredKVNamespace } from "./filtered" ; export interface SitesOptions { sitePath ? : string ; siteInclude ? : string [ ] ; siteExclude ? : string [ ] ; } export class SitesPlugin extends Plugin < SitesOptions > implements SitesOptions { @ Option ( { type : OptionType . STRING , name : "site" , alias : "s" , description : "Path to serve Workers Site files from" , logName : "Workers Site Path" , fromWrangler : ( { site } ) => site ?. bucket , } ) sitePath ? : string ; @ Option ( { type : OptionType . ARRAY , description : "Glob pattern of site files to serve" , logName : "Workers Site Include" , fromWrangler : ( { site } ) => site ?. include , } ) siteInclude ? : string [ ] ; @ Option ( { type : OptionType . ARRAY , description : "Glob pattern of site files not to serve" , logName : "Workers Site Exclude" , fromWrangler : ( { site } ) => site ?. exclude , } ) siteExclude ? : string [ ] ; readonly #setupResult : Promise < SetupResult > ; constructor ( ctx , options ? ) { super ( ctx ) ; this . assignOptions ( options ) ; this . #setupResult = this . #setup ( ) ; } async #setup ( ) { if ( ! this . sitePath ) return { } ; const { FileStorage , } = require ( "@miniflare/storage-file" ) ; const sitePath = path . resolve ( this . ctx . rootPath , this . sitePath ) ; const storage = new FileStorage ( sitePath , false ) ; const bindings = { __STATIC_CONTENT : new FilteredKVNamespace ( storage , { readOnly : true , include : this . siteInclude && globsToMatcher ( this . siteInclude ) , exclude : this . siteExclude && globsToMatcher ( this . siteExclude ) , } ) , __STATIC_CONTENT_MANIFEST : { } , } ; const additionalModules = { __STATIC_CONTENT_MANIFEST : { default : "{}" } , } ; return { bindings , watch : [ sitePath ] , additionalModules } ; } async setup ( ) { return this . #setupResult ; } }