Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading env from wrangler.toml in dev server #133

Open
jamiehaywood opened this issue May 5, 2024 · 1 comment
Open

Support reading env from wrangler.toml in dev server #133

jamiehaywood opened this issue May 5, 2024 · 1 comment

Comments

@jamiehaywood
Copy link

Currently I have a rudimentary function called getWranglerEnv that reads the env from the wrangler.toml and adds it to the env of the honox dev server. This is a proposal to add this as a plugin.

import pagesBuild from '@hono/vite-cloudflare-pages'
import pagesPlugin from '@hono/vite-dev-server/cloudflare-pages'
import { Env } from 'hono/types'
import honox from 'honox/vite'
import clientBuild from 'honox/vite/client'
import { defineConfig } from 'vite'

export default defineConfig(async ({ mode }) => {
  if (mode === 'client') {
    return {
      plugins: [clientBuild()]
    }
  } else {
    return {
      ssr: {
        external: ['react', 'react-dom', 'prop-types', 'react-is', 'hoist-non-react-statics', '@babel/runtime']
      },
      plugins: [
        honox({
          islands: true,
          devServer: {
            env: await getWranglerEnv(),
            plugins: [
              pagesPlugin({
                d1Databases: ['DB'],
                d1Persist: true
              })
            ]
          }
        }),
        pagesBuild()
      ]
    }
  }
})
async function getWranglerEnv(): Promise<Env> {
  const fs = await import('fs/promises')
  const toml = await import('toml')

  const wranglerToml = await fs.readFile('wrangler.toml', 'utf-8')
  const config = toml.parse(wranglerToml)

  console.log("Environment variables loaded from wrangler.toml")
  return config.vars[0]
}
@yusukebe
Copy link
Member

yusukebe commented May 5, 2024

Hi @jamiehaywood

Have you tried to use the adapter?

https://github.com/honojs/vite-plugins/tree/main/packages/dev-server#adapter-1

It will load bindings and environment variables from wrangler.toml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants