sveltejs / vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
MIT License
864 stars 105 forks source link

Default Vite mainFields is incorrect (missing "browser") #808

Closed WoodyWoodsta closed 12 months ago

WoodyWoodsta commented 12 months ago

Describe the bug

With Vite 5, browserField is removed, requiring "browser" to be included in the config resolve.mainFields to ensure correct resolving of browser exports.

The default value for this field includes "browser", but this plugin incorrectly overrides the default with a value which does not include it.

The result is that upgrading to Vite 5 and v3 of this plugin causes node build conditions to be imported, and errors when those builds contain imports to node builtins.

Reproduction URL

https://stackblitz.com/edit/vitejs-vite-tvsiq8?file=.vscode%2Fextensions.json

Reproduction

Logs

error during build:
RollupError: "TextEncoder" is not exported by "__vite-browser-external", imported by "node_modules/axios/lib/helpers/formDataToStream.js".
    at error (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/parseAst.js:469:30)
    at error (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:12753:29)
    at traceVariable (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:13194:29)
    at findVariable (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:11607:39)
    at bind (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:7209:40)
    at bind (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:4650:23)
    at bind (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:4650:23)
    at bind (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:4646:32)
    at bind (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:4646:32)
    at bindReferences (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:12749:18)
    at sortModules (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:18910:20)
    at build (/home/projects/vitejs-vite-tvsiq8/node_modules/rollup/dist/es/shared/node-entry.js:18815:14)

System Info

System:
    OS: macOS 14.1.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 302.92 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.2.0 - /usr/local/bin/node
    Yarn: 4.0.1 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 119.0.6045.159
    Chrome Canary: 117.0.5871.0
    Edge: 100.0.1185.50
    Safari: 17.1
    Safari Technology Preview: 17.4
  npmPackages:
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.0
    svelte: ^4.2.2 => 4.2.2
    vite: ^5.0.0 => 5.0.0