Skip to content

Commit

Permalink
don't set serveStatc() and fixed the order for _routes.json
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed May 6, 2024
1 parent 3d09e1c commit 84b90c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 51 deletions.
50 changes: 17 additions & 33 deletions packages/cloudflare-pages/src/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +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
*/
minify?: boolean
emptyOutDir?: boolean
/**
* @description Create `_routes.json` or not.
* @default true
*/
routesJson?: boolean
}

export const defaultOptions: Required<Omit<CloudflarePagesOptions, 'serveStaticDir'>> = {
Expand All @@ -38,24 +27,22 @@ export const defaultOptions: Required<Omit<CloudflarePagesOptions, 'serveStaticD
external: [],
minify: true,
emptyOutDir: false,
routesJson: true,
}

const WORKER_JS_NAME = '_worker.js'

type StaticRoutes = { version: number; include: string[]; exclude: string[] }

export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin => {
const virtualEntryId = 'virtual:cloudflare-pages-entry-module'
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 @@ -64,33 +51,30 @@ 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)
? options.entry
: [options.entry]
: [...defaultOptions.entry],
staticPaths,
})
}
},
writeBundle: async () => {
if (!options?.routesJson === false) {
return
}
staticRoutes = {
const paths = await readdir(resolve(config.root, config.build.outDir), {
withFileTypes: true,
})
paths.forEach((p) => {
if (p.isDirectory()) {
staticPaths.push(`/${p.name}/*`)
} else {
if (p.name === WORKER_JS_NAME) {
return
}
staticPaths.push(`/${p.name}`)
}
})
const staticRoutes: StaticRoutes = {
version: 1,
include: ['/*'],
exclude: staticPaths,
Expand All @@ -117,7 +101,7 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin
external: [...builtinModules, /^node:/],
input: virtualEntryId,
output: {
entryFileNames: '_worker.js',
entryFileNames: WORKER_JS_NAME,
},
},
},
Expand Down
6 changes: 0 additions & 6 deletions packages/cloudflare-pages/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { normalize } from 'node:path'

export type Options = {
entry: string[]
staticPaths: string[]
}

const normalizePaths = (paths: string[]) => {
Expand All @@ -16,9 +15,6 @@ const normalizePaths = (paths: string[]) => {
}

export const getEntryContent = async (options: Options) => {
const staticStr = options.staticPaths
.map((path) => `worker.get('${path}', serveStatic())`)
.join(';')
const globStr = normalizePaths(options.entry)
.map((e) => `'${e}'`)
.join(',')
Expand All @@ -37,10 +33,8 @@ export const getEntryContent = async (options: Options) => {
`

return `import { Hono } from 'hono'
import { serveStatic } from 'hono/cloudflare-pages'
const worker = new Hono()
${staticStr}
${appStr}
Expand Down
13 changes: 1 addition & 12 deletions packages/cloudflare-pages/test/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,14 @@ describe('cloudflarePagesPlugin', () => {

await build({
root: testDir,
plugins: [
cloudflarePagesPlugin({
minify: false,
serveStaticDir: './public',
}),
],
plugins: [cloudflarePagesPlugin()],
})

expect(fs.existsSync(outputFile)).toBe(true)
expect(fs.existsSync(routesFile)).toBe(true)

const output = fs.readFileSync(outputFile, 'utf-8')
expect(output).toContain('Hello World')
expect(output).toContain(`worker.get("/favicon.ico", serveStatic());
worker.get("/static/*", serveStatic());`)

const routes = fs.readFileSync(routesFile, 'utf-8')
expect(routes).toContain(
Expand All @@ -55,9 +48,7 @@ worker.get("/static/*", serveStatic());`)
root: testDir,
plugins: [
cloudflarePagesPlugin({
serveStaticDir: './public',
outputDir: 'customDir',
minify: false,
}),
],
build: {
Expand All @@ -70,8 +61,6 @@ worker.get("/static/*", serveStatic());`)

const output = fs.readFileSync(outputFile, 'utf-8')
expect(output).toContain('Hello World')
expect(output).toContain(`worker.get("/favicon.ico", serveStatic());
worker.get("/static/*", serveStatic());`)

const routes = fs.readFileSync(routesFile, 'utf-8')
expect(routes).toContain(
Expand Down

0 comments on commit 84b90c4

Please sign in to comment.