vite icon indicating copy to clipboard operation
vite copied to clipboard

Selectively exclude paths from sourcemap generation

Open silverwind opened this issue 10 months ago • 7 comments

Description

build.sourcemap currently only allows to enable or disable all sourcemap generation, but sometimes it would be useful to selectively choose for which paths to generate source maps for, to reduce overall asset output size.

Suggested solution

In webpack, one can specify include and exclude regexp patterns, but I would propose a more flexible API where a new option accepts a function which tests whether a sourcemap should be generated for each asset path:

defineConfig({
  build: {
    sourcemapFilter: path => !path.includes('node_modules');
  }
});

Semi-related is Rollup's sourcemapIgnoreList API, but unfortunately, it can not be used to exclude paths from sourcemap generation as per https://github.com/rollup/rollup/issues/5069.

Alternative

No alternatives currently because neither rollup nor vite offer options to exclude paths from sourcemaps.

Additional context

No response

Validations

silverwind avatar Feb 05 '25 17:02 silverwind

Perhaps duplicate of https://github.com/vitejs/vite/issues/19187 I haven't properly tested, but does this work in user land? https://github.com/vitejs/vite/issues/19187#issuecomment-2586109372

hi-ogawa avatar Feb 06 '25 00:02 hi-ogawa

I saw that one but it seems not clear to me what the author there wants and they are not proposing an API either, while I think my request is clear in itself.

I will try your plugin example as it looks like a good workaround, but it seems like a suboptimal solution as it removes sourcemaps after they have been already generated, while ideally they should not generate in first place.

silverwind avatar Feb 06 '25 00:02 silverwind

it seems like a suboptimal solution as it removes sourcemaps after they have been already generated, while ideally they should not generate in first place.

I'm not sure what "already generated" means and its performance cost. I have to dig deeper, but potentially most expensive operation of source map collapse (aka remapping) should become trivial/cheap when the last plugin returns { mappings: '' }. If any other custom plugins do heavy source map generation (e.g. custom babel transform with sourcemap or heavy magic string op), then any Vite side configuration cannot prevent that, so the cost of that won't be eliminated.

hi-ogawa avatar Feb 06 '25 00:02 hi-ogawa

I don't understand rollup in such detail, but do you mean if any plugin in the chain returns map: { mappings: '' }, rollup will skip the (computationally intensive) sourcemap generation because one plugin already returned a sourcemap?

silverwind avatar Feb 06 '25 13:02 silverwind

To elaborate on my last comment, source map collapse (e.g. rollup code) traces mapping from the last one back to original source, so for the following plugin transform chain example, rollup's source map collapse should become trivial. But when any other plugin generates map in their transform, the cost for that cannot be avoided even though such map won't be used. (However, normally plugins tends to skip transform for node_modules by default, this might not be a practical concern.)

plugin transform chain
{
  plugins: [
    {
      name: "someone's-plugin1",
      transform(code) {
        const result = babel.transform(code, {
          sourceMaps: true, // <-- unavoidable
          plugins: [/* ... some transform ... */]
        })
        return {
          code: result.code,
          map: result.map,
        }
      }
    },
    {
      name: "someone's-plugin2",
      transform(code) {
        const ms = new MagicString(code);
        // ... some transform ...
        return {
          code: ms.toString(),
          map: ms.generateMap({ hires: 'boundary' }) // <-- unavoidable
        }
      }
    },
    {
      name: "my-strip-mappings",
      transform(code) {
        return {
          code, 
          map: { mappings: '' }
        }
      }
    },
  ]
}

hi-ogawa avatar Feb 07 '25 00:02 hi-ogawa

i stumbled upon this issue as I am having an out of memory because of source maps.

This is how to do it:

// ...
    rollupOptions: {
      output: {
        sourcemapExcludeSources: true, // this helps further reduce including source code in prod builds
      },
      // this will hide the missing source maps which will for sure start happening after the plugin you introduce
      onLog: function ignoreMisingNodeModulesFromSourceMap(level, log, handler) {
        if (log.code === 'SOURCEMAP_ERROR' && log.message.includes('node_modules')) {
          return;
        }
        handler(level, log);
      },
    },
  },
plugins: [
//...
    {
      name: 'remove-node-modules-from-source-map',
      enforce: 'post',
      transform(code, id) {
        if (id.includes('node_modules')) {
          return { code, map: { mappings: '' } };
        }
      },
    },

dagadbm avatar Sep 01 '25 15:09 dagadbm

Hmm, does the suggestion from @dagadbm actually stop source maps from being generated, or does it simply clear them out at the end?

I've been trying to speed up a rather slow and memory intensive build, and adding them plugin didn't seem to help. The time/memory/cpu remains the same. The only effect is that the final map files are nearly empty.

KieranP avatar Nov 03 '25 22:11 KieranP