Skip to content

window not defined in content shared file in v0.19 #864

Closed

Description

@mehimanshupatil

Describe the bug

I have a content script which has a react code.
I have created constant file from which i export window variable.
the wxt prepare command fails

Error logs
 > wxt prepare


WXT 0.19.0                                                                                         5:51:11 PM
ℹ Generating types...                                                                             5:51:13 PM
✖ Command failed after 3.837 s                                                                    5:51:15 PM

 ERROR  window is not defined                                                                      5:51:15 PM

  at entrypoints/content/constant.ts:1:21
  at ViteNodeRunner.runModule (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:399:11)
  at ViteNodeRunner.directRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:381:16)
  at ViteNodeRunner.cachedRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:206:14)
  at ViteNodeRunner.dependencyRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:259:12)
  at entrypoints/content/App.tsx:3:31
  at ViteNodeRunner.runModule (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:399:5)
  at ViteNodeRunner.directRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:381:5)
  at ViteNodeRunner.cachedRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:206:14)
  at ViteNodeRunner.dependencyRequest (node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite-node/dist/client.mjs:259:12)

Reproduction

https://github.com/mehimanshupatil/wxt-example

Steps to reproduce

pnpm i

System Info

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M1
    Memory: 65.75 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 20.15.0 - ~/.nvm/versions/node/v20.15.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.15.0/bin/npm
    pnpm: 9.6.0 - ~/.nvm/versions/node/v20.15.0/bin/pnpm
    Watchman: 2024.06.10.00 - /opt/homebrew/bin/watchman
Browsers:
    Brave Browser: 127.1.68.128
    Edge: 127.0.2651.74
    Safari: 17.5
npmPackages:
    wxt: ^0.19.0 => 0.19.0

Used Package Manager

pnpm

Validations

Activity

  1. added
    pending-triage
    on Jul 26, 2024
  2. aiktb commented on Jul 26, 2024

    @aiktb
    Contributor

    I have the same issue, the side effect of the import statement is triggered in v0.19.0, but not in v.0.18.0.

  3. Timeraa commented on Jul 26, 2024

    @Timeraa
    Member

    Pinged @aklinker1 about this in the Discord but seems to be an issue from the move to tsup, same behaviour with self

  4. added and removed
    pending-triage
    on Jul 26, 2024
  5. aklinker1 commented on Jul 26, 2024

    @aklinker1
    Member

    @aiktb @Timeraa @mehimanshupatil Can you guys try out [email protected] and let me know if it works for you?

    Just an FYI - you shouldn't be using browser environment globals, like window or self, outside the main function of an entrypoint. In this new version, I made it so most browser environment globals are at least defined, but you will still run into issues with things like window.location.href, because I didn't implement all the APIs.

  6. aiktb commented on Jul 27, 2024

    @aiktb
    Contributor

    @aiktb @Timeraa @mehimanshupatil Can you guys try out [email protected] and let me know if it works for you?

    Just an FYI - you shouldn't be using browser environment globals, like window or self, outside the main function of an entrypoint. In this new version, I made it so most browser environment globals are at least defined, but you will still run into issues with things like window.location.href, because I didn't implement all the APIs.

    Fixed.👍

    Thank you for your answer, I have a Selector Class, in which the static member new a Class with a document.querySelector operation, and that is the problem.

  7. mehimanshupatil commented on Jul 27, 2024

    @mehimanshupatil
    Author

    getting error when running prepare

    
    WXT 0.19.1-alpha1                                                                           2:37:21 PM
    ℹ Generating types...                                                                      2:37:21 PM
    SETUP
    TEARDOWN
    ✖ Command failed after 1.177 s                                                             2:37:22 PM
    
    [2:37:22 PM]  ERROR  Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. Received protocol 'virtual:'
    
      at throwIfUnsupportedURLScheme (node:internal/modules/esm/load:239:11)
      at defaultLoad (node:internal/modules/esm/load:130:3)
      at ModuleLoader.load (node:internal/modules/esm/loader:396:13)
      at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:56)
      at new ModuleJob (node:internal/modules/esm/module_job:67:26)
      at ModuleLoader.#createModuleJob (node:internal/modules/esm/loader:290:17)
      at ModuleLoader.getJobFromResolveResult (node:internal/modules/esm/loader:247:34)
      at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:17)
      at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:21)
    
    
    
  8. aklinker1 commented on Jul 27, 2024

    @aklinker1
    Member

    @mehimanshupatil Interesting, could you share your project?

  9. mehimanshupatil commented on Jul 27, 2024

    @mehimanshupatil
    Author

    @aklinker1
    its reproducible on Reproduction repo
    https://github.com/mehimanshupatil/wxt-example
    tried running by removing window.location.href import but still Received protocol 'virtual:' error is thrown

  10. aklinker1 commented on Jul 27, 2024

    @aklinker1
    Member

    Yup, got it now, thanks. Looks like vite-node doesn't like the wxt/client import because it eventually imports virtual:app-config... For now, [email protected] fixes this for your case, but I need to do additional work before releasing 0.19.1 based on this feedback and this thread: #866

  11. mehimanshupatil commented on Jul 27, 2024

    @mehimanshupatil
    Author

    cool window error is resolved
    another error related to scss file import
    when scss file is imported in index.tsx error is thrown
    [sass] Cannot read properties of undefined (reading 'pop')
    have updated Reproduction repo to reproduce sass erorr

  12. aklinker1 commented on Jul 27, 2024

    @aklinker1
    Member

    Ok, sounds good! Thanks for helping me test this out btw 🙏

  13. aklinker1 commented on Jul 27, 2024

    @aklinker1
    Member

    @mehimanshupatil Sass error fixed in 0.19.1-alpha3, confirmed with your latest reproduction.

  14. aklinker1 commented on Jul 27, 2024

    @aklinker1
    Member

    Fixes released in v0.19.1. If people are still having problems, feel free to re-open or create another issue!

  15. added theissue type on Mar 21, 2025
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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions