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

feat(dev-server): introduce env #30

Merged
merged 4 commits into from
Nov 16, 2023
Merged

Conversation

yusukebe
Copy link
Member

This PR introduces the concept of ENV and the env option.

For Cloudflare Pages, the current cf option allows us to use bindings. For example:

export default defineConfig({
  plugins: [
    devServer({
      entry: 'src/index.ts',
      cf: {
        bindings: {
          NAME: 'Hono',
        },
      },
    }),
  ],
})

With this configuration, we can access variables through c.env:

app.get('/', (c) => {
  return c.text(c.env.NAME)
})

This is currently exclusive to Cloudflare Pages, but in the future, it may be extended to other runtimes/platforms with specific environment values. While c.env seems to be used for Cloudflare, other platforms/runtimes may also use bindings/c.env. For instance, the AWS Lambda adapter currently uses env:

https://github.com/honojs/hono/blob/main/src/adapter/aws-lambda/handler.ts#L126-L143

This PR suggests separating the Cloudflare Pages-specific functionality from dev-server.ts. With this change, the getEnv() method for Cloudflare Pages is placed in cloudflare-pages/index.ts, and users can include the env in their vite.config.ts:

import { getEnv } from '@hono/vite-dev-server/cloudflare-pages'

// ...

export default defineConfig({
  plugins: [
    devServer({
      env: getEnv({
        bindings: {
          NAME: 'Hono',
        },
      }),
    }),
  ],
})

With this mechanism, adding support for another platform that uses environment variables becomes straightforward:

import { getEnv } from '@hono/vite-dev-server/another-one'

// ...

export default defineConfig({
  plugins: [
    devServer({
      env: getEnv({
        someFunction: {
          NAME: 'Hono',
        },
      }),
    }),
  ],
})

@yusukebe
Copy link
Member Author

Hey @Code-Hex !

Could you review this?

Copy link

@Code-Hex Code-Hex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me 😁

packages/dev-server/src/dev-server.ts Outdated Show resolved Hide resolved
Copy link

changeset-bot bot commented Nov 16, 2023

🦋 Changeset detected

Latest commit: 1e7f4ae

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/vite-dev-server Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@yusukebe
Copy link
Member Author

@Code-Hex

Thanks for reviewing. Let's go with this!

@yusukebe yusukebe merged commit 12a8b15 into main Nov 16, 2023
1 check passed
@yusukebe yusukebe deleted the feat/dev-server-introduce-env branch November 16, 2023 14:07
@github-actions github-actions bot mentioned this pull request Nov 16, 2023
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

Successfully merging this pull request may close these issues.

2 participants