Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectively exclude paths from sourcemap generation #19365

Open
4 tasks done
silverwind opened this issue Feb 5, 2025 · 5 comments
Open
4 tasks done

Selectively exclude paths from sourcemap generation #19365

silverwind opened this issue Feb 5, 2025 · 5 comments

Comments

@silverwind
Copy link

silverwind commented Feb 5, 2025

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 rollup/rollup#5069.

Alternative

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

Additional context

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 6, 2025

Perhaps duplicate of #19187 I haven't properly tested, but does this work in user land? #19187 (comment)

@silverwind
Copy link
Author

silverwind commented Feb 6, 2025

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.

@hi-ogawa hi-ogawa marked this as a duplicate of #19187 Feb 6, 2025
@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 6, 2025

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.

@silverwind
Copy link
Author

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?

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 7, 2025

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: '' }
        }
      }
    },
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants