diff --git a/packages/cloudflare-pages/src/cloudflare-pages.ts b/packages/cloudflare-pages/src/cloudflare-pages.ts index b88b09e..2179213 100644 --- a/packages/cloudflare-pages/src/cloudflare-pages.ts +++ b/packages/cloudflare-pages/src/cloudflare-pages.ts @@ -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> = { @@ -38,24 +27,22 @@ export const defaultOptions: Required { 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) { @@ -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, @@ -117,7 +101,7 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin external: [...builtinModules, /^node:/], input: virtualEntryId, output: { - entryFileNames: '_worker.js', + entryFileNames: WORKER_JS_NAME, }, }, }, diff --git a/packages/cloudflare-pages/src/entry.ts b/packages/cloudflare-pages/src/entry.ts index ea375c0..1ac5315 100644 --- a/packages/cloudflare-pages/src/entry.ts +++ b/packages/cloudflare-pages/src/entry.ts @@ -2,7 +2,6 @@ import { normalize } from 'node:path' export type Options = { entry: string[] - staticPaths: string[] } const normalizePaths = (paths: string[]) => { @@ -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(',') @@ -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} diff --git a/packages/cloudflare-pages/test/cloudflare-pages.test.ts b/packages/cloudflare-pages/test/cloudflare-pages.test.ts index 99165ef..06b8a36 100644 --- a/packages/cloudflare-pages/test/cloudflare-pages.test.ts +++ b/packages/cloudflare-pages/test/cloudflare-pages.test.ts @@ -19,12 +19,7 @@ describe('cloudflarePagesPlugin', () => { await build({ root: testDir, - plugins: [ - cloudflarePagesPlugin({ - minify: false, - serveStaticDir: './public', - }), - ], + plugins: [cloudflarePagesPlugin()], }) expect(fs.existsSync(outputFile)).toBe(true) @@ -32,8 +27,6 @@ describe('cloudflarePagesPlugin', () => { 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( @@ -55,9 +48,7 @@ worker.get("/static/*", serveStatic());`) root: testDir, plugins: [ cloudflarePagesPlugin({ - serveStaticDir: './public', outputDir: 'customDir', - minify: false, }), ], build: { @@ -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(