Skip to content

Commit

Permalink
add serveStaticDir option and fixed the order
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed May 4, 2024
1 parent 0919785 commit 3d09e1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
38 changes: 24 additions & 14 deletions packages/cloudflare-pages/src/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type CloudflarePagesOptions = {
* @default './dist'
*/
outputDir?: string
/**
* @description The directory includes static files.
* The default is `config.build.outDir`, but if you don't include built files, you can specify a different directory, e.g., `public` or set `undefined` to disable it.
* @default config.build.outDir
*/
serveStaticDir?: string | undefined
external?: string[]
/**
* @default true
Expand All @@ -26,7 +32,7 @@ type CloudflarePagesOptions = {
routesJson?: boolean
}

export const defaultOptions: Required<CloudflarePagesOptions> = {
export const defaultOptions: Required<Omit<CloudflarePagesOptions, 'serveStaticDir'>> = {
entry: ['./src/index.tsx', './app/server.ts'],
outputDir: './dist',
external: [],
Expand All @@ -42,12 +48,14 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin
const resolvedVirtualEntryId = '\0' + virtualEntryId
let config: ResolvedConfig
let staticRoutes: StaticRoutes
let serveStaticDir = options?.serveStaticDir
const staticPaths: string[] = []

return {
name: '@hono/vite-cloudflare-pages',
configResolved: async (resolvedConfig) => {
config = resolvedConfig
serveStaticDir ??= config.build.outDir
},
resolveId(id) {
if (id === virtualEntryId) {
Expand All @@ -56,6 +64,18 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin
},
async load(id) {
if (id === resolvedVirtualEntryId) {
if (typeof serveStaticDir === 'string') {
const paths = await readdir(resolve(config.root, serveStaticDir!), {
withFileTypes: true,
})
paths.forEach((p) => {
if (p.isDirectory()) {
staticPaths.push(`/${p.name}/*`)
} else {
staticPaths.push(`/${p.name}`)
}
})
}
return await getEntryContent({
entry: options?.entry
? Array.isArray(options.entry)
Expand All @@ -67,24 +87,14 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin
}
},
writeBundle: async () => {
const paths = await readdir(config.build.outDir, {
withFileTypes: true,
})
paths.forEach((p) => {
if (p.isDirectory()) {
staticPaths.push(`/${p.name}/*`)
} else {
staticPaths.push(`/${p.name}`)
}
})
if (!options?.routesJson === false) {
return
}
staticRoutes = {
version: 1,
include: ['/*'],
exclude: staticPaths,
}
if (!options?.routesJson === false) {
return
}
const path = resolve(
config.root,
options?.outputDir ?? defaultOptions.outputDir,
Expand Down
5 changes: 3 additions & 2 deletions packages/cloudflare-pages/test/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'node:fs'
import path from 'path'
import { build } from 'vite'
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { describe, it, expect, afterAll } from 'vitest'
import cloudflarePagesPlugin from '../src/index'

describe('cloudflarePagesPlugin', () => {
Expand All @@ -23,6 +22,7 @@ describe('cloudflarePagesPlugin', () => {
plugins: [
cloudflarePagesPlugin({
minify: false,
serveStaticDir: './public',
}),
],
})
Expand Down Expand Up @@ -55,6 +55,7 @@ worker.get("/static/*", serveStatic());`)
root: testDir,
plugins: [
cloudflarePagesPlugin({
serveStaticDir: './public',
outputDir: 'customDir',
minify: false,
}),
Expand Down

0 comments on commit 3d09e1c

Please sign in to comment.