vite icon indicating copy to clipboard operation
vite copied to clipboard

Add a way to universally disable sourcemaps

Open ascott18 opened this issue 3 years ago • 4 comments

Description

Since vite produces modern javascript, sourcemaps in development are often unnecessary as there is no significant transpilation that would make debugging difficult without them.

On the other hand, sourcemaps are notoriously difficult to get correct, and if they're ever wrong then it can ruin the whole debugging experience.

While they can be turned off for individual components of vite (e.g. esbuild.sourcemap and css.devSourcemap), the dev server will still unconditionally inject sourcemaps from any plugin that produces them (like vite-plugin-vue2).

Suggested solution

Add a setting server.sourcemap that can be set to false to prevent the automatic injection of sourcemaps that happens here https://github.com/vitejs/vite/blob/ba62be40b4f46c98872fb10990b559fee88f4a29/packages/vite/src/node/server/send.ts#L59-L63.

Or, a bit more ambitiously, add a top level sourcemap setting that, if set, would supercede build.sourcemap, esbuild.sourcemap, build.rollupOptions.output.sourcemap* and css.devSourcemap. I realize that individual plugins could still inject their own inline sourcemaps into their emitted code, but having a single central setting for this would also encourage plugin authors to respect this setting.

Alternative

Disabling sourcemaps in the browser dev tools can achieve this end, but this has a few problems:

  • Is a per-machine setting, so while one project might not benefit from sourcemaps, another project might still need them, requiring repeated toggling through context switches.
  • Is a per-machine setting, so if it is known that the sourcemaps for a particular project are problematic, they cannot be turned off for all developers on that project.

Additional context

This would likely allow users to overcome (not a fix, but at least allow for a workaround) for the following issues:

  • https://github.com/vitejs/vite-plugin-vue/issues/33
  • https://github.com/vitejs/vite/issues/5834
  • https://github.com/underfin/vite-plugin-vue2/issues/208

Validations

ascott18 avatar Aug 24 '22 18:08 ascott18

Or, a bit more ambitiously, add a top level sourcemap setting that, if set, would supercede build.sourcemap, esbuild.sourcemap, build.rollupOptions.output.sourcemap* and css.devSourcemap

A single top-level sourcemap option to control all emitted source maps would be nice. Similar to Webpack's SourceMapDevToolPlugin which fulfills the same purpose there.

I'm especially hoping for a option to exclude assets by path regexp from emitting a source map, so that for example third-party chunks can be excluded.

silverwind avatar Sep 26 '23 21:09 silverwind

I second this use case.

I was surprised to find the proposed setting didn't already exist in some form.

worstpractice avatar Oct 16 '23 00:10 worstpractice

Basically you can't stop vite from changing your source JS files, in dev mode. And in production mode you have to constantly rebuild because vite build won't. So the better option is to use tsc -w and python -m http.server if you want your files intact...

Kagami avatar Nov 19 '23 19:11 Kagami

I also need to disable it. Did anyone find a way to do this in the dev server ?

bhanuc avatar Sep 30 '24 19:09 bhanuc

You can achieve this by adding a plugin.

import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    {
      name: 'remove-sourcemaps',
      transform(code) {
        return {
          code,
          map: { mappings: '' }
        }
      }
    }
  ]
})

sapphi-red avatar Oct 15 '24 10:10 sapphi-red