-
Notifications
You must be signed in to change notification settings - Fork 34
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
22 changed files
with
404 additions
and
59 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { readdir, writeFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import type { Plugin, ResolvedConfig } from 'vite' | ||
import type { BuildOptions } from '../../base.js' | ||
import buildPlugin, { defaultOptions } from '../../base.js' | ||
|
||
export type CloudflarePagesBuildOptions = BuildOptions | ||
|
||
const WORKER_JS_NAME = '_worker.js' | ||
const ROUTES_JSON_NAME = '_routes.json' | ||
|
||
type StaticRoutes = { version: number; include: string[]; exclude: string[] } | ||
|
||
const bunBuildPlugin = (pluginOptions?: CloudflarePagesBuildOptions): Plugin => { | ||
let config: ResolvedConfig | ||
const staticPaths: string[] = [] | ||
|
||
return { | ||
...buildPlugin({ | ||
...pluginOptions, | ||
output: WORKER_JS_NAME, | ||
}), | ||
configResolved: async (resolvedConfig) => { | ||
config = resolvedConfig | ||
}, | ||
writeBundle: async () => { | ||
const paths = await readdir(resolve(config.root, config.build.outDir), { | ||
withFileTypes: true, | ||
}) | ||
// 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}`) | ||
} | ||
}) | ||
const staticRoutes: StaticRoutes = { | ||
version: 1, | ||
include: ['/*'], | ||
exclude: staticPaths, | ||
} | ||
const path = resolve( | ||
config.root, | ||
pluginOptions?.outputDir ?? defaultOptions.outputDir, | ||
'_routes.json' | ||
) | ||
await writeFile(path, JSON.stringify(staticRoutes)) | ||
} | ||
}, | ||
name: '@hono/vite-build/cloudflare-pages', | ||
} | ||
} | ||
|
||
export default bunBuildPlugin |
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,16 @@ | ||
import type { Plugin } from 'vite' | ||
import type { BuildOptions } from '../../base.js' | ||
import buildPlugin from '../../base.js' | ||
|
||
export type CloudflareWorkersBuildOptions = BuildOptions | ||
|
||
const nodeBuildPlugin = (pluginOptions?: CloudflareWorkersBuildOptions): Plugin => { | ||
return { | ||
...buildPlugin({ | ||
...pluginOptions, | ||
}), | ||
name: '@hono/vite-build/cloudflare-workers', | ||
} | ||
} | ||
|
||
export default nodeBuildPlugin |
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,40 @@ | ||
import type { Plugin } from 'vite' | ||
import type { BuildOptions } from '../../base.js' | ||
import buildPlugin from '../../base.js' | ||
import { serveStaticHook } from '../../entry/serve-static.js' | ||
|
||
export type NodeBuildOptions = { | ||
staticRoot?: string | undefined | ||
} & BuildOptions | ||
|
||
const nodeBuildPlugin = (pluginOptions?: NodeBuildOptions): Plugin => { | ||
return { | ||
...buildPlugin({ | ||
...{ | ||
entryContentBeforeHooks: [ | ||
async (appName, options) => { | ||
// eslint-disable-next-line quotes | ||
let code = "import { serveStatic } from '@hono/node-server/serve-static'\n" | ||
code += serveStaticHook(appName, { | ||
filePaths: options?.staticPaths, | ||
root: pluginOptions?.staticRoot, | ||
}) | ||
return code | ||
}, | ||
], | ||
entryContentAfterHooks: [ | ||
async (appName) => { | ||
// eslint-disable-next-line quotes | ||
let code = "import { serve } from '@hono/node-server'\n" | ||
code += `serve(${appName})` | ||
return code | ||
}, | ||
], | ||
}, | ||
...pluginOptions, | ||
}), | ||
name: '@hono/vite-build/node', | ||
} | ||
} | ||
|
||
export default nodeBuildPlugin |
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
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 @@ | ||
type ServeStaticHookOptions = { | ||
filePaths?: string[] | ||
root?: string | ||
} | ||
|
||
export const serveStaticHook = (appName: string, options: ServeStaticHookOptions) => { | ||
let code = '' | ||
for (const path of options.filePaths ?? []) { | ||
code += `${appName}.use('${path}', serveStatic({ root: '${options.root ?? './'}' }))\n` | ||
} | ||
return code | ||
} |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
import { defaultOptions } from './base.js' | ||
export { defaultOptions } | ||
|
||
import basePlugin from './base.js' | ||
export default basePlugin |
Oops, something went wrong.