diff --git a/packages/build/package.json b/packages/build/package.json index 7504f70..8e0a165 100644 --- a/packages/build/package.json +++ b/packages/build/package.json @@ -47,6 +47,10 @@ "./vercel": { "types": "./dist/adapter/vercel/index.d.ts", "import": "./dist/adapter/vercel/index.js" + }, + "./aws-lambda": { + "types": "./dist/adapter/aws-lambda/index.d.ts", + "import": "./dist/adapter/aws-lambda/index.js" } }, "typesVersions": { @@ -74,6 +78,9 @@ ], "vercel": [ "./dist/adapter/vercel/index.d.ts" + ], + "aws-lambda": [ + "./dist/adapter/aws-lambda/index.d.ts" ] } }, @@ -103,4 +110,4 @@ "engines": { "node": ">=18.14.1" } -} \ No newline at end of file +} diff --git a/packages/build/src/adapter/aws-lambda/index.ts b/packages/build/src/adapter/aws-lambda/index.ts new file mode 100644 index 0000000..fba380c --- /dev/null +++ b/packages/build/src/adapter/aws-lambda/index.ts @@ -0,0 +1,41 @@ +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 AwsLambdaBuildOptions = { + staticRoot?: string | undefined +} & BuildOptions + +const awsLambdaBuildPlugin = (pluginOptions?: AwsLambdaBuildOptions): 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 { handle } from 'hono/aws-lambda'\n" + code += `export const handler = handle(${appName})\n` + return code + }, + ], + // To avoid `Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module`, + // the output file is specified as .mjs. + output: 'index.mjs', + ...pluginOptions, + }), + name: '@hono/vite-build/aws-lambda', + } +} + +export default awsLambdaBuildPlugin