-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
310 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# @hono/vite-cloudflare-pages | ||
|
||
`@hono/vite-cloudflare-pages` is a Vite plugin to build your Hono application for Cloudflare Pages. | ||
|
||
## Usage | ||
|
||
### Installation | ||
|
||
You can install `vite` and `@hono/vite-cloudflare-pages` via npm. | ||
|
||
```plain | ||
npm i -D vite @hono/vite-cloudflare-pages | ||
``` | ||
|
||
Or you can install them with Bun. | ||
|
||
```plain | ||
bun add vite @hono/vite-cloudflare-pages | ||
``` | ||
|
||
### Settings | ||
|
||
Add `"type": "module"` to your `package.json`. Then, create `vite.config.ts` and edit it. | ||
|
||
```ts | ||
import { defineConfig } from 'vite' | ||
import pages from '@hono/vite-cloudflare-pages' | ||
|
||
export default defineConfig({ | ||
plugins: [pages()], | ||
}) | ||
``` | ||
|
||
### Build | ||
|
||
Just run `vite build`. | ||
|
||
```text | ||
npm exec vite build | ||
``` | ||
|
||
Or | ||
|
||
```text | ||
bunx --bun vite build | ||
``` | ||
|
||
### Deploy to Cloudflare Pages | ||
|
||
Run the `wrangler` command. | ||
|
||
```text | ||
wrangler pages deploy ./dist | ||
``` | ||
|
||
## Options | ||
|
||
The options are below. | ||
|
||
```ts | ||
type CloudflarePagesOptions = { | ||
entry?: string | ||
outputDir?: string | ||
external?: string[] | ||
minify?: boolean | ||
emptyOutDir?: boolean | ||
} | ||
``` | ||
Default values: | ||
```ts | ||
export const defaultOptions = { | ||
entry: defaultEntry, // node_modules/@hono/vite-cloudflare-pages/dist/entry/_worker.js | ||
outputDir: './dist', | ||
external: ['react', 'react-dom'], | ||
minify: true, | ||
emptyOutDir: true, | ||
} | ||
``` | ||
|
||
## Build a client | ||
|
||
If you also want to build a client-side script, you can configure it as follows. | ||
|
||
```ts | ||
export default defineConfig(({ mode }) => { | ||
if (mode === 'client') { | ||
return { | ||
build: { | ||
lib: { | ||
entry: './src/client.ts', | ||
formats: ['es'], | ||
fileName: 'client', | ||
name: 'client', | ||
}, | ||
rollupOptions: { | ||
output: { | ||
dir: './dist/static', | ||
}, | ||
}, | ||
copyPublicDir: false, | ||
}, | ||
} | ||
} else { | ||
return { | ||
plugins: [pages()], | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
The build command: | ||
|
||
```text | ||
vite build && vite build --mode client | ||
``` | ||
|
||
## Authors | ||
|
||
- Yusuke Wada <https://github.com/yusukebe> | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "@hono/vite-cloudflare-pages", | ||
"description": "Vite plugin to build your Hono app for Cloudflare Pages", | ||
"version": "0.0.0", | ||
"types": "dist/index.d.ts", | ||
"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", | ||
"watch": "tsup --watch", | ||
"prepublishOnly": "yarn build", | ||
"release": "bumpp --tag \"@hono/cloudflare-pages@v%s\" --commit \"chore(cloudflare-pages): release v%s\" && yarn publish" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"types": [ | ||
"./dist/types" | ||
] | ||
} | ||
}, | ||
"author": "Yusuke Wada <[email protected]> (https://github.com/yusukebe)", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/honojs/vite-plugins.git" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org", | ||
"access": "public" | ||
}, | ||
"homepage": "https://github.com/honojs/vite-plugins", | ||
"devDependencies": { | ||
"bumpp": "^9.2.0", | ||
"glob": "^10.3.10", | ||
"hono": "^3.8.3", | ||
"publint": "^0.1.12", | ||
"rimraf": "^5.0.1", | ||
"tsup": "^7.2.0", | ||
"vite": "^4.4.9" | ||
}, | ||
"peerDependencies": { | ||
"hono": "*" | ||
}, | ||
"engines": { | ||
"node": ">=18.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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') | ||
|
||
type CloudflarePagesOptions = { | ||
entry?: string | ||
outputDir?: string | ||
external?: string[] | ||
minify?: boolean | ||
emptyOutDir?: boolean | ||
} | ||
|
||
export const defaultOptions = { | ||
entry: defaultEntry, // node_modules/@hono/vite-cloudflare-pages/dist/entry/_worker.js | ||
outputDir: './dist', | ||
external: ['react', 'react-dom'], | ||
minify: true, | ||
emptyOutDir: true, | ||
} | ||
|
||
export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin => { | ||
const entry = options?.entry ?? defaultOptions.entry | ||
return { | ||
name: '@hono/vite-cloudflare-pages', | ||
config: async (): Promise<UserConfig> => { | ||
return { | ||
ssr: { | ||
external: options?.external ?? defaultOptions.external, | ||
noExternal: true, | ||
}, | ||
build: { | ||
emptyOutDir: options?.emptyOutDir ?? defaultOptions.emptyOutDir, | ||
ssr: entry, | ||
minify: options?.minify ?? defaultOptions.minify, | ||
rollupOptions: { | ||
external: [...builtinModules, /^node:/], | ||
input: entry, | ||
output: { | ||
dir: options?.outputDir ?? defaultOptions.outputDir, | ||
}, | ||
}, | ||
}, | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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) | ||
|
||
export default worker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { cloudflarePagesPlugin } from './cloudflare-pages' | ||
export default cloudflarePagesPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src/" | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"src/**/*.test.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": [ | ||
"src", | ||
"test" | ||
], | ||
"compilerOptions": { | ||
"module": "ES2022", | ||
"target": "ES2022", | ||
"types": [ | ||
"vite/client" | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { glob } from 'glob' | ||
import { defineConfig } from 'tsup' | ||
|
||
const entryPoints = glob.sync('./src/**/*.+(ts|tsx|json)', { | ||
ignore: ['./src/**/*.test.+(ts|tsx)'], | ||
}) | ||
|
||
export default defineConfig({ | ||
entry: entryPoints, | ||
dts: true, | ||
tsconfig: './tsconfig.build.json', | ||
splitting: false, | ||
minify: false, | ||
format: ['esm'], | ||
bundle: false, | ||
platform: 'node', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters