Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active December 1, 2023 01:26
Show Gist options
  • Save yusukebe/184b9080fb04c912a8d33c73c5317378 to your computer and use it in GitHub Desktop.
Save yusukebe/184b9080fb04c912a8d33c73c5317378 to your computer and use it in GitHub Desktop.
import { Plugin, ViteDevServer } from 'vite'
import { Miniflare } from 'miniflare'
import fs from 'fs/promises'
import { resolve, relative } from 'path'
type Options = {
r2Buckets: string[] | Record<string, string>
}
const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };'
export function local2r2(options: Options): Plugin {
let server: ViteDevServer
const targetDir = resolve('./src/files')
const mf = new Miniflare({
modules: true,
script: nullScript,
r2Buckets: options.r2Buckets,
r2Persist: true
})
const plugin: Plugin = {
name: 'local2r2',
configureServer(s) {
server = s
},
async watchChange(id) {
if (id.startsWith(targetDir)) {
const fileName = relative(targetDir, id)
const bucket = await mf.getR2Bucket('BUCKET')
try {
await fs.access(id)
console.info(`${fileName} is updated`)
const content = await fs.readFile(id, 'utf8')
await bucket.put(fileName, content)
} catch (e: any) {
if (e.code === 'ENOENT') {
console.info(`${fileName} is deleted`)
await bucket.delete(fileName)
} else {
console.error(`Error accessing file ${id}:`, e)
}
}
server.ws.send({
type: 'full-reload',
path: '*'
})
}
}
}
return plugin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment