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

Broken sourcemaps for two outputs of different entryFileNames in same dir #3439

Closed
danimoh opened this issue Mar 12, 2020 · 4 comments · Fixed by #3440
Closed

Broken sourcemaps for two outputs of different entryFileNames in same dir #3439

danimoh opened this issue Mar 12, 2020 · 4 comments · Fixed by #3440

Comments

@danimoh
Copy link
Contributor

danimoh commented Mar 12, 2020

Expected Behavior

Generated sourcemaps for individual bundles generated in an output dir should reference their respective bundle file.

Actual Behavior

All sourcemaps for bundles named via entryFileNames in the same output dir reference the last generated file ("file":"bundle.cjs.js" in the example repl).
Chrome DevTools fails to parse the erroneous sourcemaps.

Use case

The repl setup is somewhat artificial. The desired behavior can in the example be achieved more elegantly and without issues by using output.file instead of output.dir in combination with output.entryFileNames. However, there are uses cases where such a setup makes sense, for example when one wants to generate declaration files with the typescript plugin which requires specifying the dir option.

@danimoh
Copy link
Contributor Author

danimoh commented Mar 13, 2020

For anyone running into this issue:
This can be fixed with a very simple output plugin, for example for the repl linked in the issue:

import path from 'path';

// Fixes broken sourcemaps generated by rollup which might mess up association to a sourcemap's bundle file when writing
// multiple outputs to the same directory, see https://github.com/rollup/rollup/issues/3439.
function fixSourcemaps() {
    return {
        name: 'fix-sourcemaps',
        generateBundle: (options, bundle) => {
            for (const assetOrChunkInfo of Object.values(bundle)) {
                if (!assetOrChunkInfo.map) return; // has no sourcemap
                assetOrChunkInfo.map.file = path.basename(assetOrChunkInfo.fileName);
            }
        },
    };
}

export default {
  input: 'input.js',
  output: [
    {
      dir: 'output',
      format: 'es',
      entryFileNames: 'bundle.es.js',
      sourcemap: true,
      plugins: [fixSourcemaps()],
    },
    {
      dir: 'output',
      format: 'cjs',
      entryFileNames: 'bundle.cjs.js',
      sourcemap: true,
      plugins: [fixSourcemaps()],
    },
  ],
};

@lukastaegert
Copy link
Member

Sorry, for this, fix at #3440

@lukastaegert
Copy link
Member

Respect you found such an easy workaround using the plugin API 👍

@danimoh
Copy link
Contributor Author

danimoh commented Mar 13, 2020

Awesome, thank you for the fix :)

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