Closed MartkCz closed 1 year ago
The optimizer is only used during dev and having sourcemaps is better in dev. You can use the function arg of defineConfig to disable sourcemaps only on build.
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
plugins: [
svelte({
compilerOptions: {
enableSourcemap: command !== 'build',
},
}),
],
}));
That being said we should either be able to handle missing sourcemaps on dev or force it to true. @bluwy what do you think?
I think we could handle missing sourcemaps in dev, rather than forcing it to true 🤔 We do force some options but maybe we should do it sparingly and only when needed, in this case, it's probably fine if there's no sourcemaps if that's what the user wanted.
I don't know why I disabled sourcemaps, but when I installed a 3rd party package, this error appeared and it was hard to detect the problem. Imho a better error message would have sufficed.
I was also caught by this problem today when trying to extract a component from a sveltekit app to a separate package. I actually prefer to have compilerOptions.enableSourcemap
as false because it's easier to understand what svelte is doing behind the scenes.
I tried to fix the problem in different ways, and an alternative that seems to work is to add this to svelte.config.js
:
const config = {
...
vitePlugin: { prebundleSvelteLibraries: false }
}
This way I can import svelte components from external packages and still have source maps disabled.
Either way, it would be great if this could be fixed in src/utils/esbuild.js
.
Thanks.
Describe the bug
Cannot import plugin if compilerOptions -> enableSourcemap is set to false.
Error: [plugin: vite-plugin-svelte:optimize-svelte] Cannot read properties of null (reading 'toUrl') at compileSvelte
The problem line of plugin:
import RenderTemplate from './RenderTemplate.svelte';"
Full code of problem file:
Because of https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/src/utils/esbuild.js#L109
compiled.js.map
is nullIf compilerOptions -> enableSourcemap is true, everything works fine.
Reproduction URL
https://stackblitz.com/edit/vitejs-vite-9dzyn1?file=vite.config.js
Reproduction
No response
Logs
No response
System Info