btd / rollup-plugin-visualizer

📈⚖️ Visuallize your bundle
MIT License
1.91k stars 56 forks source link

Expose JS API to combine multiple outputs into one #188

Open aklinker1 opened 8 months ago

aklinker1 commented 8 months ago

I'm working on a framework for building chrome extensions, WXT, that includes this plugin when running wxt build --analyze. Chrome extensions contain multiple bulid steps, so each step outputs a separate stats JSON file, and then the rollup-plugin-visualizer CLI is used to combine them.

However, this doesn't work out of the box when using pnpm and shamefully-hoist=true (the default). This is because the binary isn't included in node_modules/.bin, so it's not in the path. Instead, the binary is stored at node_modules/wxt/node_modules/.bin/rollup-plugin-visualizer. There might be a similar problem for yarn PnP, haven't tested it.

See https://github.com/wxt-dev/wxt/issues/360 for more details.

I could add node_modules/wxt/node_modules/.bin to the path while calling spawn("rollup-plugin-visualizer") (and I likely will do that as a workaround for now), but I'd also like to just call a function instead of spawning a new process, something like this:

import { mergeStats } from 'rollup-plugin-visualizer';

await mergeStats({
  files: ["stats1.json", "stats2.json", ...],
  // other options...
});

After looking at the code, I think this is a pretty simple change. Just involves exporting runForPluginJson from plugin/index.ts, or another entrypoint if that doesn't make sense.

https://github.com/btd/rollup-plugin-visualizer/blob/dec839823db43af6b34d54779ed1db0486fe4e1c/bin/cli.ts#L56

If that sounds OK, I'm willing to work on this and open a PR!

btd commented 8 months ago

There is a CLI tool for this https://github.com/btd/rollup-plugin-visualizer#cli already. Not sure if i want to support merge as API

btd commented 8 months ago

Basically merge happen there https://github.com/btd/rollup-plugin-visualizer/blob/master/bin/cli.ts#L70C3-L102C5 I can extract this to the function, if it works for you

aklinker1 commented 8 months ago

I would prefer the entire runForPluginJson function be exported. Or else please include the call to renderTemplate, since that's not exported either.

https://github.com/btd/rollup-plugin-visualizer/blob/dec839823db43af6b34d54779ed1db0486fe4e1c/bin/cli.ts#L104-L107