From 3d09e1ca0be359e5608fd33cacda4773f37821b2 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 5 May 2024 00:04:22 +0900 Subject: [PATCH] add `serveStaticDir` option and fixed the order --- .../cloudflare-pages/src/cloudflare-pages.ts | 38 ++++++++++++------- .../test/cloudflare-pages.test.ts | 5 ++- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/cloudflare-pages/src/cloudflare-pages.ts b/packages/cloudflare-pages/src/cloudflare-pages.ts index 7af2cf6..b88b09e 100644 --- a/packages/cloudflare-pages/src/cloudflare-pages.ts +++ b/packages/cloudflare-pages/src/cloudflare-pages.ts @@ -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 @@ -26,7 +32,7 @@ type CloudflarePagesOptions = { routesJson?: boolean } -export const defaultOptions: Required = { +export const defaultOptions: Required> = { entry: ['./src/index.tsx', './app/server.ts'], outputDir: './dist', external: [], @@ -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) { @@ -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) @@ -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, diff --git a/packages/cloudflare-pages/test/cloudflare-pages.test.ts b/packages/cloudflare-pages/test/cloudflare-pages.test.ts index e667c5d..99165ef 100644 --- a/packages/cloudflare-pages/test/cloudflare-pages.test.ts +++ b/packages/cloudflare-pages/test/cloudflare-pages.test.ts @@ -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', () => { @@ -23,6 +22,7 @@ describe('cloudflarePagesPlugin', () => { plugins: [ cloudflarePagesPlugin({ minify: false, + serveStaticDir: './public', }), ], }) @@ -55,6 +55,7 @@ worker.get("/static/*", serveStatic());`) root: testDir, plugins: [ cloudflarePagesPlugin({ + serveStaticDir: './public', outputDir: 'customDir', minify: false, }),