Closed cdellacqua closed 3 years ago
Same underlying issue as #124, the workaround now is to add that library to optimizeDeps.exclude
. There's a feature request to automate this at #125. The real issue is within Vite and is tracked in https://github.com/vitejs/vite/issues/3910.
Thank you. When adding the library I'm using to optimizeDeps.exclude
this bug doesn't occur, though I understand it's not a perfect solution.
I was wondering if excluding all possible dependencies could prevent weird behavior from ever happening (it's better to have longer build time than a buggy output), so I did this:
import {svelte} from '@sveltejs/vite-plugin-svelte';
import {readFileSync} from 'fs';
const pkg = JSON.parse(readFileSync('package.json'));
const exclude = [
...Object.keys(pkg.devDependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {}),
];
export default defineConfig({
base: './',
plugins: [svelte()],
optimizeDeps: {
exclude,
},
});
And it works most of the time, except when using import 'path/to/some-module'
because then I get "Uncaught SyntaxError: The requested module '/node_modules/... does not provide an export named '...'"
inside some-module
.
After some testing I found out that replacing import 'path/to/some-module'
with import {} from 'path/to/some-module'
solves the problem, but I guess this should be another issue
Excluding all isn't recommended since Vite needs to pre-bundle CJS-only packages to ESM (Explanation). You might be lucky with your setup of having all its dependencies obediently exports ESM code.
Re "Uncaught SyntaxError: The requested module '/node_modules/... does not provide an export named '...'"
, that could be an issue of not prebundling CJS to ESM, I'm not sure why using import {}
would work around that though.
You have to turn of source maps to use the debugger on this (otherwise they both get mapped to utils.js
). The two files being loaded are:
http://localhost:3000/src/main.js http://localhost:3000/src/App.svelte http://localhost:3000/node_modules/.vite/@cdellacqua_svelte-vite-double-import-lib.js?v=04d0ce69 (one console.log is here)
http://localhost:3000/src/main.js http://localhost:3000/src/App.svelte http://localhost:3000/node_modules/.vite/@cdellacqua_svelte-vite-double-import-lib.js?v=04d0ce69 http://localhost:3000/node_modules/@cdellacqua/svelte-vite-double-import-lib/Component.svelte http://localhost:3000/node_modules/@cdellacqua/svelte-vite-double-import-lib/utils.js (one console.log is here)
So this bug resides within the single optimized module. The optimized module should not be importing things from the non-optimized module.
Should we close this in favor of #125?
automatic handling of svelte dependencies optimizeDeps has been released.
Please check if that fixes your issue https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/CHANGELOG.md#100-next19
Hi @dominikg , yes it does! Awesome!
Describe the bug
When using a library that contains Svelte components a module can get initialized twice. This happens only when running Vite in development mode.
The bug occurs when you have:
Running Vite in development mode results in module.js getting executed/initialized twice, thus causing reference mismatches and all sort of strange behavior.
On the other hand, when running Vite for bundling for production the module resolution process correctly detects that module.js has already been imported and does not replicate it in the output bundle.
Reproduction
A reproduction of this bug can be found at this repository:
https://github.com/cdellacqua/svelte-vite-double-import-demo
The repository contains an
app
directory with a minimal example based on the template obtained by runningnpm init vite@latest test-vite-svelte -- --template svelte
.It also contains
svelte-vite-double-import-lib
which is a minimal library that can cause this behavior.Steps to reproduce:
app
directorynpm install
npm run dev
Logs
System Info
Severity
blocking all usage of vite-plugin-svelte