Open stevebeauge opened 3 weeks ago
In v0.19.1, I polyfilled browser globals when loading entrypoints in Node to get their options.
Specifically, setting a global document
is causing this problem because the vite plugin is trying to create a URL, and document.baseURI
isn't valid.
Commenting out the document
polyfill in WXT fixes the problem. However, this isn't a generic a solution - I want the document global available in case devs use it outside the main
function of entrypoints, or in other files with side-effects.
I could use jsdom
instead of linkedom
when setting up the globals, but JSDom is big and slow and I already had linkedom
installed...
I tried messing around and doing things like setting a custom baseURI
on the document, but that's not working either.
For now, while I figure out a solution I'd recommend patching WXT (pnpm or patch-package
) and commenting out document
in node_modules/wxt/dist/core/utils/environments/browser-environment.mjs
:
return {
...global,
window,
- document,
+ // This document is not compatible with vite-plugin-node-polyfill. Removing this means we can't use document outside the main function of entrypoints
+ // See https://github.com/wxt-dev/wxt/issues/931
+ // document,
self: global
};
@aklinker1 I guess this error is reading the cjs file and hitting the polyfill in import.meta.resolve
... but why is it resolving to cjs? The plugin package.json looks like it is ESM compliant. (https://github.com/davidmyersdev/vite-plugin-node-polyfills/blob/main/package.json)
@1natsu172 No, this error is caused by new URL(..., docuemnt.baseURI)
, my document
polyfill isn't providing a valid baseURI
that the plugin accepts.
As for why it's resolving to CJS... Because we're using c12
to load config files, which uses jiti
, which loads modules as CJS. See #297
Yes, that understood, I was just saying that if I had loaded the ESM, it would not be an error only for this plugin. (Of course, that's still not the fundamental solution.)
As for why it's resolving to CJS... Because we're using c12 to load config files, which uses jiti, which loads modules as CJS. See https://github.com/wxt-dev/wxt/issues/297
OK, let's have a conversation about ESM load in #297 😃
Describe the bug
I'm importing the
vite-plugin-node-polyfills
plugin in my project because I need some polyfills (Buffer, ...).Starting from
wxt@0.19.1
, adding the plugin breaks the compilation (both usingdev
andbuild
commands) with a unclear error:Reverting back to the
0.19.0
version removes the issue.Reproduction
This repo highlight the issue : https://github.com/stevebeauge/error-polyfill
Steps to reproduce
System Info
Used Package Manager
pnpm
Validations