diff --git a/.changeset/cuddly-cows-tie.md b/.changeset/cuddly-cows-tie.md new file mode 100644 index 0000000..836cf8f --- /dev/null +++ b/.changeset/cuddly-cows-tie.md @@ -0,0 +1,5 @@ +--- +'@hono/vite-cloudflare-pages': minor +--- + +feat: use virtual entry diff --git a/packages/cloudflare-pages/README.md b/packages/cloudflare-pages/README.md index 0d854fd..144e1d7 100644 --- a/packages/cloudflare-pages/README.md +++ b/packages/cloudflare-pages/README.md @@ -71,9 +71,9 @@ Default values: ```ts export const defaultOptions = { - entry: defaultEntry, // node_modules/@hono/vite-cloudflare-pages/dist/entry/_worker.js + entry: '/src/index', outputDir: './dist', - external: ['react', 'react-dom'], + external: [], minify: true, emptyOutDir: true, } diff --git a/packages/cloudflare-pages/package.json b/packages/cloudflare-pages/package.json index aab0585..3187c4b 100644 --- a/packages/cloudflare-pages/package.json +++ b/packages/cloudflare-pages/package.json @@ -6,8 +6,7 @@ "module": "dist/index.js", "type": "module", "scripts": { - "build:entry": "tsup --no-config --format esm --external /src/index -d dist/entry ./src/entry/_worker.js", - "build": "rimraf dist && tsup && yarn build:entry && publint", + "build": "rimraf dist && tsup && publint", "watch": "tsup --watch", "prerelease": "yarn build", "release": "yarn publish" @@ -53,4 +52,4 @@ "engines": { "node": ">=18.14.1" } -} +} \ No newline at end of file diff --git a/packages/cloudflare-pages/src/cloudflare-pages.ts b/packages/cloudflare-pages/src/cloudflare-pages.ts index 949fd6d..e59e152 100644 --- a/packages/cloudflare-pages/src/cloudflare-pages.ts +++ b/packages/cloudflare-pages/src/cloudflare-pages.ts @@ -1,12 +1,6 @@ import { builtinModules } from 'module' -import path from 'node:path' -import { fileURLToPath } from 'url' import type { Plugin, UserConfig } from 'vite' - -const __filename = fileURLToPath(import.meta.url) -const __dirname = path.dirname(__filename) - -const defaultEntry = path.join(__dirname, 'entry', '_worker.js') +import { getEntryContent } from './entry.js' type CloudflarePagesOptions = { entry?: string @@ -16,18 +10,30 @@ type CloudflarePagesOptions = { emptyOutDir?: boolean } -export const defaultOptions = { - entry: defaultEntry, // node_modules/@hono/vite-cloudflare-pages/dist/entry/_worker.js +export const defaultOptions: Required = { + entry: '/src/index', outputDir: './dist', - external: ['react', 'react-dom'], + external: [], minify: true, emptyOutDir: true, } export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin => { - const entry = options?.entry ?? defaultOptions.entry + const virtualEntryId = 'virtual:cloudflare-pages-entry-module' + const resolvedVirtualEntryId = '\0' + virtualEntryId + return { name: '@hono/vite-cloudflare-pages', + resolveId(id) { + if (id === virtualEntryId) { + return resolvedVirtualEntryId + } + }, + load(id) { + if (id === resolvedVirtualEntryId) { + return getEntryContent({ entry: options?.entry }) + } + }, config: async (): Promise => { return { ssr: { @@ -36,13 +42,13 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin }, build: { emptyOutDir: options?.emptyOutDir ?? defaultOptions.emptyOutDir, - ssr: entry, minify: options?.minify ?? defaultOptions.minify, + ssr: true, rollupOptions: { external: [...builtinModules, /^node:/], - input: entry, + input: virtualEntryId, output: { - dir: options?.outputDir ?? defaultOptions.outputDir, + entryFileNames: '_worker.js', }, }, }, diff --git a/packages/cloudflare-pages/src/entry.ts b/packages/cloudflare-pages/src/entry.ts new file mode 100644 index 0000000..f2f108f --- /dev/null +++ b/packages/cloudflare-pages/src/entry.ts @@ -0,0 +1,22 @@ +export type Options = { + entry?: string +} + +export const defaultOptions: Required = { + entry: '/src/index', +} + +export const getEntryContent = (options: Options) => { + return `import { Hono } from 'hono' +import { serveStatic } from 'hono/cloudflare-pages' +import app from '${options.entry ?? defaultOptions.entry}' + +const worker = new Hono() +worker.get('/favicon.ico', serveStatic()) +worker.get('/static/*', serveStatic()) + +worker.route('/', app) +worker.notFound(app.notFoundHandler) + +export default worker` +} diff --git a/packages/cloudflare-pages/src/entry/_worker.js b/packages/cloudflare-pages/src/entry/_worker.js deleted file mode 100644 index d7fd232..0000000 --- a/packages/cloudflare-pages/src/entry/_worker.js +++ /dev/null @@ -1,12 +0,0 @@ -import { Hono } from 'hono' -import { serveStatic } from 'hono/cloudflare-pages' -import app from '/src/index' - -const worker = new Hono() -worker.get('/favicon.ico', serveStatic()) -worker.get('/static/*', serveStatic()) - -worker.route('/', app) -worker.notFound(app.notFoundHandler) - -export default worker