sveltejs / vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
MIT License
844 stars 103 forks source link

Compiled js map can be a null #730

Closed MartkCz closed 1 year ago

MartkCz commented 1 year ago

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:

import RenderTemplate from './RenderTemplate.svelte';

export { RenderTemplate };
export * from './Template.js';

Because of https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/src/utils/esbuild.js#L109 compiled.js.map is null

If 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

System:
    OS: Linux 5.15 Manjaro Linux
    CPU: (12) x64 AMD Ryzen 5 5600G with Radeon Graphics
    Memory: 16.78 GB / 30.70 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.5.0 - /usr/bin/node
    npm: 9.8.1 - /usr/bin/npm
  npmPackages:
    @sveltejs/vite-plugin-svelte: ^2.4.5 => 2.4.5 
    svelte: ^4.1.2 => 4.2.0 
    vite: ^4.4.9 => 4.4.9
dominikg commented 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?

bluwy commented 1 year ago

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.

MartkCz commented 1 year ago

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.

paulovieira commented 1 year ago

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.