Skip to content

Exclude Zod #824

Closed as not planned
Closed as not planned

Description

@HotariTobu

Feature Request

We cannot use Zod because of Entrypoint Side Effects so I request to exclude Zod from the effects.

Is your feature request related to a bug?

N/A

What are the alternatives?

Manual validation like with typeof, instanceof, and so on.

Additional context

WXT 0.18.13

Activity

  1. aklinker1 commented on Jul 15, 2024

    @aklinker1
    Member

    Can you share code? Zod shouldn't cause any problems, I use it in several of my extensions.

  2. HotariTobu commented on Jul 17, 2024

    @HotariTobu
    Author

    Reproduction steps

    Run

    npx wxt init wxt-zod-sample -t vanilla --pm npm
    cd wxt-zod-sample 
    npm install zod

    Update

    // entrypoints/background.ts
    
    import { z } from "zod";
    
    export const messageSchema = z.object({
      action: z.string()
    })
    export type Message = z.infer<typeof messageSchema>
    
    export default defineBackground(() => {
      browser.runtime.onMessage.addListener(message => {
        const parsedMessage = messageSchema.parse(message)
        console.log(parsedMessage.action)
      })
    });
    // entrypoints/content.ts
    
    import { Message } from "./background";
    
    export default defineContentScript({
      matches: ['<all_urls>'],
      main() {
        browser.runtime.sendMessage({
          action: 'Print me!'
        } satisfies Message)
      },
    });

    Run

    npm run dev

    Result

    > [email protected] dev
    > wxt
    
    
    WXT 0.18.13                                                                         18:18:56
    ✔ Started dev server @ http://localhost:3000                                       18:18:56
    ℹ Pre-rendering chrome-mv3 for development with Vite 5.3.4                         18:18:56
    ✖ Command failed after 235 ms                                                      18:18:56
    
    [18:18:56]  ERROR  entrypoints/background.ts: Cannot use imported variable "z" outside the main function. See https://wxt.dev/guide/go-further/entrypoint-side-effects.html
    
      at importEntrypointFile (node_modules/wxt/dist/cli.js:2656:13)
      at async Object.importEntrypoint (node_modules/wxt/dist/cli.js:1118:18)
      at async getBackgroundEntrypoint (node_modules/wxt/dist/cli.js:1805:27)
      at async node_modules/wxt/dist/cli.js:1609:18
      at async Promise.all (index 0)
      at async findEntrypoints (node_modules/wxt/dist/cli.js:1597:23)
      at async internalBuild (node_modules/wxt/dist/cli.js:3537:23)
      at async buildAndOpenBrowser (node_modules/wxt/dist/cli.js:3868:28)
      at async Object.start (node_modules/wxt/dist/cli.js:3833:9)
      at async node_modules/wxt/dist/cli.js:4347:5
    
  3. aklinker1 commented on Jul 17, 2024

    @aklinker1
    Member

    Ahh, I see. Added to #506, going to close this one as a duplicate.

    For now, you should put the zod schemas in a different file and import them or define them inside the main function. Either option should work for the short-term.

  4. HotariTobu commented on Jul 17, 2024

    @HotariTobu
    Author

    The error occurs even if I define zod schemas in other script files and import them.

  5. aklinker1 commented on Jul 17, 2024

    @aklinker1
    Member

    Don't use the schemas outside the main function and it will work

  6. HotariTobu commented on Jul 17, 2024

    @HotariTobu
    Author

    I avoided it by putting schemas files out of entrypoints.
    Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions