wxt icon indicating copy to clipboard operation
wxt copied to clipboard

fix: avoid leaking `@types/node` into client code

Open aleclarson opened this issue 10 months ago • 8 comments

Overview

The defineWxtPlugin function is referenced by the generated .wxt/types/imports.d.ts module, which is used by client code. Therefore, we need to avoid importing from wxt/src/types.ts, as it depends on Node.js-specific packages (like vite). This inadvertently introduces Node.js globals into the extension's client environment, which is undesirable (e.g. it breaks the return type of setTimeout).

Edit 1

Updated other "offending" imports. Split out the option-related types (into option-types.ts) and the client-related types (into client-types.ts).

Manual Testing

Without this change, you should see setTimeout return a NodeJS.Timeout object.

After this change, setTimeout will return a number as expected.

Related Issue

https://github.com/wxt-dev/wxt/issues/1459

aleclarson avatar Mar 22 '25 01:03 aleclarson

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
Latest commit a6969b0abfbc9b7bc3c0576dcf81459f4d0c1eee
Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/67f5641dfb1cf8000811180b
Deploy Preview https://deploy-preview-1531--creative-fairy-df92c4.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Mar 22 '25 01:03 netlify[bot]

I can retarget this to #1085 if you prefer. LMK!

aleclarson avatar Mar 22 '25 16:03 aleclarson

I can retarget this to #1085 if you prefer. LMK!

I think that would be better as @aklinker1 should be merging 0.20.0 very soon as he rebased everything from main into it and we've been testing it

Timeraa avatar Mar 28 '25 00:03 Timeraa

0.20.0 is released and out so make sure to rebase your changes on main!

Timeraa avatar Mar 29 '25 11:03 Timeraa

Rebased ✅

aleclarson avatar Apr 08 '25 18:04 aleclarson

I still see NodeJS.Timeout in the demo extension after building WXT on this branch:

Screenshot 2025-04-09 at 10 33 34 AM

Restarted my language server multiple times to be sure.

Isn't this... impossible to do? For example, you have to import import { defineConfig } from 'wxt' in the wxt.config.ts file. Doesn't that mean node types will always be referenced and included in your project?

aklinker1 avatar Apr 09 '25 15:04 aklinker1

Isn't this... impossible to do? For example, you have to import import { defineConfig } from 'wxt' in the wxt.config.ts file. Doesn't that mean node types will always be referenced and included in your project?

You can definitely avoid it. The key is to use multiple tsconfig.json files. Here's how I do it:

  • /tsconfig.json used by default (e.g. *.config.ts modules and maybe some /scripts/*.ts modules)
    • any default compilerOptions I use
    • types: ["node"]
  • /src/tsconfig.json used by /src/**/* modules
    • extends ../tsconfig.json
    • include: ["./"] (to prevent config/script modules from being pulled in)
    • types: [] (to prevent @types/node from being pulled in; you can add others as necessary, like vite/client)

This is off the top of my noggin, so could be missing a detail. I assume there isn't a demo with this setup already?

aleclarson avatar Apr 10 '25 16:04 aleclarson

I would love to get this merged. Let me know what the next step is.

aleclarson avatar Aug 20 '25 22:08 aleclarson