Skip to content

Feature Request: Allow JSON and HTML output at the same time. #375

Closed

Description

@leipert

Rather than adding the plugin twice with e.g.:

[
      new BundleAnalyzerPlugin({
        analyzerMode: 'static',
        openAnalyzer: false,
        reportFilename: 'webpack-report/index.html'),
      }),
      new BundleAnalyzerPlugin({
        analyzerMode: 'json',
        openAnalyzer: false,
        reportFilename: 'webpack-report/index.json'),
      }),
]

it would be nice to be able to generate JSON and HTML in one go, e.g.:

[
  new BundleAnalyzerPlugin({
    analyzerMode: "static",
    openAnalyzer: false,
    reportOutput: [
      { type: "json", fileName: "webpack-report/index.json" },
      { type: "html", fileName: "webpack-report/index.html" },
    ],
  }),
];

Activity

  1. leipert commented on Aug 11, 2020

    @leipert
    Author

    By the way, great work on this plugin. I don't have time right now to contribute this myself, but it would be a valuable addition to us and I just wanted to write the idea down somewhere. 😄

  2. leipert commented on Aug 11, 2020

    @leipert
    Author
  3. Gongreg commented on Aug 11, 2020

    @Gongreg
    Contributor

    Hey, I think it makes sense, but we should get @valscion opinion on it :)

  4. valscion commented on Aug 12, 2020

    @valscion
    Collaborator

    Thanks for the idea! I'm not sure if this works out nicely with all the possible options configurations — it looks like we could end up with configuration that is "invalid" and we would have to write quite a lot of error handling code to deal with this situation.

  5. pkuczynski commented on Nov 5, 2020

    @pkuczynski

    I believe this is possible already. Here is my example setup:

        plugins: [
            new BundleAnalyzerPlugin({
                analyzerMode: 'static',
                reportFilename: '../report/stats.html',
                statsFilename: '../report/stats.json',
                defaultSizes: 'gzip',
                generateStatsFile: true,
                openAnalyzer: false
            })
      ]]
    

    This produces at the same time stats.html and stats.json. Is this what you wanted to achieve?

  6. alex-knyazev commented on Apr 28, 2021

    @alex-knyazev

    @

    I believe this is possible already. Here is my example setup:

        plugins: [
            new BundleAnalyzerPlugin({
                analyzerMode: 'static',
                reportFilename: '../report/stats.html',
                statsFilename: '../report/stats.json',
                defaultSizes: 'gzip',
                generateStatsFile: true,
                openAnalyzer: false
            })
      ]]
    

    This produces at the same time stats.html and stats.json. Is this what you wanted to achieve?

    No, because stats.json is from webpack, report of webpuck-bundle-analyzer is different. You can see at json html and compare it with stats.json - they are different, webpack bundle analyzer report contains information only about chunks

  7. alex-knyazev commented on Apr 28, 2021

    @alex-knyazev

    I think it is important feature.
    I'm writing such script for two bundles comparsion:

    1. Go to git branch
    2. Build and analyze
    3. Save report to temp folder
    4. Go to other git branch
    5. Build and analyze
    6. Save report to temp folder
    7. Compare two reports.

    The issue is that I want to have html reports, but I also need reports in json format to be able to compare reports.
    There are no beautiful ways to do it: parse html file to get json from it or build twice using different modes.
    @valscion I think it could be very goo to have ability to pass array to "mode" property:

    new BundleAnalyzerPlugin({
            analyzerMode: ['json', 'static'],
    }),
    

    The expected result is to have to files in build directory: report.html and report.json.

    Maybe one of the ways to make it more easy is to not add report-json inline in html (window.chartData), but include it on html page using <script> where src is 'report.json' that is in the same folder. With this changes in static and server modes we always will have json to analyze

  8. valscion commented on Apr 28, 2021

    @valscion
    Collaborator

    Hmm yeah, I think adding support for analyzerMode possibly being an array would be reasonable.

    What do you think, @th0r?

  9. valscion commented on Apr 28, 2021

    @valscion
    Collaborator

    Also regarding this:

    Maybe one of the ways to make it more easy is to not add report-json inline in html, but include it on html page using <script> where src is 'report.json' that is in the same folder. With this changes in static and server modes we always will have json to analyze

    I don't think we need to do that. It would make the handling of report files more difficult as then the report would not be self-contained in a single HTML file anymore, like it now is. At any rate, we could discuss this change as its own change request, outside of the scope of supporting analyzerMode being an array.

  10. Gongreg commented on Apr 28, 2021

    @Gongreg
    Contributor

    Sorry, I don't have a lot of time currently, but as a workaround, you can also generate your own html file:

    Published webpack-bundle-analyzer has a file called:
    webpack-bundle-analyzer/public/viewer.js

    That file tries to read window.chartData. If you create a HTML file with the json data set to the window.chartData, and include the script file it should render correctly.

  11. alex-knyazev commented on Apr 28, 2021

    @alex-knyazev

    @valscion @Gongreg
    For now I came to this solution:

    
    const { getViewerData } = require("webpack-bundle-analyzer/lib/analyzer");
    
    const stats = require("./dist/stats.json");
    
    async function main() {
      const report = await getViewerData(stats);
      console.log(report) ; // report.json that can be used for two bundles comparing
    }
    
    main();
    
    

    thanks a lot!

  12. th0r commented on Apr 28, 2021

    @th0r
    Contributor

    Hmm yeah, I think adding support for analyzerMode possibly being an array would be reasonable.

    Well, I think we can do that, but it will require a change to reportFilename option to be able to return different filenames for different report types e.g. allow to make it a function (reportType) => <reportFilename> (which won't work for CLI) or an object {<reportType>: <reportFilename>}. Anyway, options becomes a mess... We definitely need to ship a new major version with completely revised API with pluggable external reporters.

  13. valscion commented on Apr 28, 2021

    @valscion
    Collaborator

    @valscion @Gongreg
    For now I came to this solution:

    
    const { getViewerData } = require("webpack-bundle-analyzer/lib/analyzer");
    
    const stats = require("./dist/stats.json");
    
    async function main() {
      const report = await getViewerData(stats);
      console.log(report) ; // report.json that can be used for two bundles comparing
    }
    
    main();
    

    thanks a lot!

    Do note that you are relying on internal logic and this logic can break even in patch releases. I'm glad that it works for you, though ☺️

    We definitely need to ship a new major version with completely revised API with pluggable external reporters.

    Yeah, this does indeed sound like it.

    So this isn't a trivial update in the end. It would require quite a lot of effort, so don't hold your breath 😅

  14. leipert commented on May 5, 2021

    @leipert
    Author

    @valscion Submitted a PR with a backwards-compatible implementation: #442

  15. langri-sha commented on Apr 28, 2022

    @langri-sha

    Really looking forward to this! For now it seems we either have to resort to adding the plugin twice, or disabling it to generate the stats file and then using the CLI.

  16. locked and limited conversation to collaborators on Feb 23, 2023
  17. converted this issue into a discussion #573 on Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions