repack icon indicating copy to clipboard operation
repack copied to clipboard

feat: Allow storing compilation stats in file

Open eps1lon opened this issue 3 years ago • 1 comments

Summary

Allows creating JSON files of compilation stats that can be analyzed with https://statoscope.tech/ Command-line argumes take precedence over options in webpack.config.js

Copied behavior from webpack-cli: https://github.com/webpack/webpack-cli/blob/26ba6a82e7bc596e7fe48db9028f79e65e8131bc/packages/webpack-cli/src/webpack-cli.ts#L2291-L2299

Based on this local patch
diff --git a/node_modules/@klapp/native/node_modules/@callstack/repack/commands.js b/node_modules/@klapp/native/node_modules/@callstack/repack/commands.js
index 8342509..b045590 100644
--- a/node_modules/@klapp/native/node_modules/@callstack/repack/commands.js
+++ b/node_modules/@klapp/native/node_modules/@callstack/repack/commands.js
@@ -65,7 +65,16 @@ module.exports = [
         name: '--verbose',
         description: 'Enables verbose logging',
       },
-      webpackConfigOption
+      webpackConfigOption,
+      {
+        name: '--json <statsFile>',
+        description: "TODO webpack json",
+        parse: (val) => path.resolve(val),
+      },
+      {
+        name: '--stats <preset>',
+        description: "TODO webpack json",
+      }
     ),
     func: require('./dist/commands/bundle').bundle,
   },
diff --git a/node_modules/@klapp/native/node_modules/@callstack/repack/dist/commands/bundle.js b/node_modules/@klapp/native/node_modules/@callstack/repack/dist/commands/bundle.js
index 276ca72..5a6e4d0 100644
--- a/node_modules/@klapp/native/node_modules/@callstack/repack/dist/commands/bundle.js
+++ b/node_modules/@klapp/native/node_modules/@callstack/repack/dist/commands/bundle.js
@@ -10,6 +10,8 @@ var _webpack = _interopRequireDefault(require("webpack"));
 var _env = require("../env");

 var _getWebpackConfigPath = require("./utils/getWebpackConfigPath");
+const fs = require('fs')
+const {stringifyStream} = require('@discoveryjs/json-ext')

 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -45,11 +47,31 @@ function bundle(_, config, args) {
   }

   const compiler = (0, _webpack.default)(require(webpackConfigPath));
-  compiler.run(error => {
+  compiler.run((error, stats) => {
     if (error) {
       console.error(error);
       process.exit(2);
     }
+
+    if (args.json) {
+      console.log('Writing compiler stats', args.stats)
+      const statsJson = stats.toJson(args.stats)
+      const outputStream = fs.createWriteStream(args.json)
+
+      stringifyStream(statsJson).on(
+        'error', (error) => {
+          console.error(error);
+          process.exit(2);
+        }
+      ).pipe(outputStream).on(
+        'error', (error) => {
+          console.error(error);
+          process.exit(2);
+        }
+      ).on("close", () => {
+        console.log(`Wrote compiler stats to ${args.json}`)
+      })
+    }
   });
 }
 //# sourceMappingURL=bundle.js.map

Test plan

  • [x] Tested patch in Klarna mobile app
  • [x] in packages/TesterApp: yarn bundle --stats normal --json stats.json:
    $ yarn bundle --stats normal --json stats.json
    ....
    webpack 5.50.0 compiled successfully in 24543 ms 
    Writing compiler stats
    Wrote compiler stats to /Users/sebastian.silbermann/repack/packages/TesterApp/stats.json
    
  • [x] in packages/TesterApp: yarn bundle --json stats.json with stats: { preset: 'normal' } in webpack.config.mjs
  • [ ] CI (needs maintainer approval)

eps1lon avatar Aug 24 '22 10:08 eps1lon

🦋 Changeset detected

Latest commit: 4e902443b126b629b5bc1997cc532dd54c61d848

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@callstack/repack Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Aug 24 '22 10:08 changeset-bot[bot]

Hi @eps1lon thank you for PR - stats.json will be very useful. I approved the PR but are you able to rebase ? . I'll merge then

Done. Deploy failure looks unrelated (missing auth)

eps1lon avatar Oct 25 '22 08:10 eps1lon

Hi @eps1lon thank you for PR - stats.json will be very useful. I approved the PR but are you able to rebase ? . I'll merge then

Done. Deploy failure looks unrelated (missing auth)

yes, deploy PR is only docs preview and it doesn't allow to deploy docs from forks because of lack access to tokens.

Thank you once again for PR !

jbinda avatar Oct 25 '22 10:10 jbinda

seams that this feature is not available in 3.1.0, any prevision when it will be? Thanks so much

piccinnigius avatar Feb 09 '23 18:02 piccinnigius