From 25bfb6170ecb7b9aec827a2b0e4838c8e6e7a78d Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 9 May 2024 22:35:25 +0900 Subject: [PATCH] feat(cloudflare-pages): custom `_routes.json` (#135) * feat(cloudflare-pages): custom `_routes.json` * change path name on exclude like an actual path * add changeset * add description to README for custom `_routes.json` --- .changeset/moody-islands-wave.md | 5 +++ packages/cloudflare-pages/README.md | 2 + .../cloudflare-pages/src/cloudflare-pages.ts | 42 +++++++++++-------- .../test/cloudflare-pages.test.ts | 22 ++++++++++ .../project/public-routes-json/_routes.json | 1 + 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 .changeset/moody-islands-wave.md create mode 100644 packages/cloudflare-pages/test/project/public-routes-json/_routes.json diff --git a/.changeset/moody-islands-wave.md b/.changeset/moody-islands-wave.md new file mode 100644 index 0000000..423664e --- /dev/null +++ b/.changeset/moody-islands-wave.md @@ -0,0 +1,5 @@ +--- +"@hono/vite-cloudflare-pages": minor +--- + +feat: creating a `public/_routes.json` will override the automatic generation diff --git a/packages/cloudflare-pages/README.md b/packages/cloudflare-pages/README.md index 7ba7202..fe9c937 100644 --- a/packages/cloudflare-pages/README.md +++ b/packages/cloudflare-pages/README.md @@ -79,6 +79,8 @@ export const defaultOptions = { } ``` +This plugin generates `_routes.json` automatically. The automatic generation can be overridden by creating a `public/_routes.json`. See [Create a `_routes.json` file](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) on Cloudflare Docs for more details. + ## Build a client If you also want to build a client-side script, you can configure it as follows. diff --git a/packages/cloudflare-pages/src/cloudflare-pages.ts b/packages/cloudflare-pages/src/cloudflare-pages.ts index 2179213..a5a79bf 100644 --- a/packages/cloudflare-pages/src/cloudflare-pages.ts +++ b/packages/cloudflare-pages/src/cloudflare-pages.ts @@ -30,6 +30,7 @@ export const defaultOptions: Required { - if (p.isDirectory()) { - staticPaths.push(`/${p.name}/*`) - } else { - if (p.name === WORKER_JS_NAME) { - return + // If _routes.json already exists, don't create it + if (paths.some((p) => p.name === ROUTES_JSON_NAME)) { + return + } else { + paths.forEach((p) => { + if (p.isDirectory()) { + staticPaths.push(`/${p.name}/*`) + } else { + if (p.name === WORKER_JS_NAME) { + return + } + staticPaths.push(`/${p.name}`) } - staticPaths.push(`/${p.name}`) + }) + const staticRoutes: StaticRoutes = { + version: 1, + include: ['/*'], + exclude: staticPaths, } - }) - const staticRoutes: StaticRoutes = { - version: 1, - include: ['/*'], - exclude: staticPaths, + const path = resolve( + config.root, + options?.outputDir ?? defaultOptions.outputDir, + '_routes.json' + ) + await writeFile(path, JSON.stringify(staticRoutes)) } - const path = resolve( - config.root, - options?.outputDir ?? defaultOptions.outputDir, - '_routes.json' - ) - await writeFile(path, JSON.stringify(staticRoutes)) }, config: async (): Promise => { return { diff --git a/packages/cloudflare-pages/test/cloudflare-pages.test.ts b/packages/cloudflare-pages/test/cloudflare-pages.test.ts index 06b8a36..3147ce4 100644 --- a/packages/cloudflare-pages/test/cloudflare-pages.test.ts +++ b/packages/cloudflare-pages/test/cloudflare-pages.test.ts @@ -67,4 +67,26 @@ describe('cloudflarePagesPlugin', () => { '{"version":1,"include":["/*"],"exclude":["/favicon.ico","/static/*"]}' ) }) + + it('Should not create a new _routes.json when _routes.json on output directory.', async () => { + const outputFile = `${testDir}/dist/_worker.js` + const routesFile = `${testDir}/dist/_routes.json` + + expect(fs.existsSync(entryFile)).toBe(true) + + await build({ + publicDir: 'public-routes-json', + root: testDir, + 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') + + const routes = fs.readFileSync(routesFile, 'utf-8') + expect(routes).toContain('{"version":1,"include":["/"],"exclude":["/customRoute"]}') + }) }) diff --git a/packages/cloudflare-pages/test/project/public-routes-json/_routes.json b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json new file mode 100644 index 0000000..2a43272 --- /dev/null +++ b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json @@ -0,0 +1 @@ +{"version":1,"include":["/"],"exclude":["/customRoute"]} \ No newline at end of file