Skip to content

Commit

Permalink
feat(pages): use virtual entry (#55)
Browse files Browse the repository at this point in the history
* feat(pages): use virtual entry

* changeset
  • Loading branch information
yusukebe authored Jan 22, 2024
1 parent 1eb65a9 commit 876e258
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-cows-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-cloudflare-pages': minor
---

feat: use virtual entry
4 changes: 2 additions & 2 deletions packages/cloudflare-pages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
5 changes: 2 additions & 3 deletions packages/cloudflare-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -53,4 +52,4 @@
"engines": {
"node": ">=18.14.1"
}
}
}
34 changes: 20 additions & 14 deletions packages/cloudflare-pages/src/cloudflare-pages.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<CloudflarePagesOptions> = {
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<UserConfig> => {
return {
ssr: {
Expand All @@ -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',
},
},
},
Expand Down
22 changes: 22 additions & 0 deletions packages/cloudflare-pages/src/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type Options = {
entry?: string
}

export const defaultOptions: Required<Options> = {
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`
}
12 changes: 0 additions & 12 deletions packages/cloudflare-pages/src/entry/_worker.js

This file was deleted.

0 comments on commit 876e258

Please sign in to comment.