Skip to content

renderedLength from sourcemaps is ignored, pre-plugins module sizes are used instead #169

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

Closed
t2t2 opened this issue Oct 12, 2023 · 2 comments

Comments

@t2t2
Copy link

t2t2 commented Oct 12, 2023

Yep it's continuation of #163 (and externally KusStar/vite-bundle-visualizer#8)

Even after 442517c the sizes in the report mismatch with final output, especially when using minifier (eg. terser). Simple test case:

index.js (size: 117 bytes, no tailing newline)

/* This is a long comment that any terser should remove from final output */
const message = 'hello';
alert(message);

rollup.config.mjs:

import terser from '@rollup/plugin-terser';
import { visualizer } from 'rollup-plugin-visualizer';

export default {
  input: 'index.js',
  output: {
    dir: 'output',
    format: 'cjs',
    sourcemap: true
  },
  plugins: [
    terser(),
    visualizer({
      sourcemap: true,
      template: 'list',
    }),
  ]
};

rollup output output/index.js: (size 63 bytes)

"use strict";alert("hello");
//# sourceMappingURL=index.js.map

rollup-plugin-visualizer output stats.yml:

index.js:
  \index.js:
    rendered: 117

117 is more than 63 and suspiciously close to source file size, so I took debugger and figured out why.


When looking at the ModuleLengths function, it prefers using the length of code compared to renderedLength, however debugger reveals that the code used is the raw, unprocessed by plugins source of the module. renderLength calculated from sourcemap is correct (16 = length of alert("hello");+newline):

image

code is taken from output bundle modules, and debugger confirms that this is indeed from before all plugins:

image


There seems to be 2 potential approaches to fix:

The quick and dirty fix is to just use renderedLength (idk if length from code is needed as fallback?)

        const result = {
          id,
          gzipLength: isCodeEmpty ? 0 : await gzipSizeGetter(code),
          brotliLength: isCodeEmpty ? 0 : await brotliSizeGetter(code),
--          renderedLength: isCodeEmpty ? renderedLength : Buffer.byteLength(code, "utf-8"),
++          renderedLength: renderedLength || (isCodeEmpty ? 0 : Buffer.byteLength(code, "utf-8")),
        };
        return result;

stats.yml:

index.js:
  \index.js:
    rendered: 16

(note: in the sourcemap the "use strict"; and sourceMappingURL are unmapped, so it showing slightly lower value than bundle size is kinda normal)

Other option is to read the actually rendered code from sourcemap and use it instead of bundle.modules[id].code. This is actually something #164 did, but seems like long feedback cycle and some misunderstandings in review got @thoughtspile to abandon it. But as seen by PR, this is the better approach as it also allows gzip and brotli sizes of modules in sourcemap mode (even if those are misleading as gzip + brotli sizes are highly influenced by how much repeating code there is, but the same issue also applies to sourcemap: false mode)


So I created this issue to check if it's better to

@btd
Copy link
Owner

btd commented Nov 24, 2023

Thank you for deep investigation. This place changed so much i already do not remember all edge cases. I will take a look. But no any ETA

#164 core idea was already merged. I closed it

@btd
Copy link
Owner

btd commented Dec 9, 2023

Added fix for sourcemap build. Thanks again for details in issue, it really helped

@btd btd closed this as completed Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants