rusackas commented on code in PR #42511:
URL: https://github.com/apache/superset/pull/42511#discussion_r3668330931


##########
superset-frontend/scripts/bundle-size-summary.js:
##########
@@ -0,0 +1,85 @@
+#!/usr/bin/env node
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+// Reduces a webpack `--json` stats file down to the handful of headline
+// numbers worth tracking over time, in the flat array format
+// benchmark-action/github-action-benchmark expects for its
+// "customSmallerIsBetter" tool. The full stats file also includes a
+// `modules`/`chunks` graph across ~15k modules, which is enormous and not
+// useful for this purpose, so we only ever read `entrypoints`.
+//
+// Usage: node scripts/bundle-size-summary.js <path-to-stats.json>
+
+const fs = require('fs');
+
+// Entrypoints worth tracking: the two user-facing app shells. `menu`,
+// `preamble`, `theme`, and `service-worker` are small, low-variance
+// infrastructure chunks, not where bundle bloat actually shows up.
+const TRACKED_ENTRYPOINTS = ['spa', 'embedded'];
+
+function entrypointSizeByExt(entrypoint, ext) {
+  return (entrypoint.assets || [])
+    .filter(asset => asset.name.endsWith(ext))
+    .reduce((total, asset) => total + asset.size, 0);
+}
+
+function main() {
+  const statsPath = process.argv[2];
+  if (!statsPath) {
+    console.error('Usage: bundle-size-summary.js <path-to-stats.json>');
+    process.exit(1);
+  }
+
+  const stats = JSON.parse(fs.readFileSync(statsPath, 'utf8'));
+  const { entrypoints } = stats;
+  if (!entrypoints) {
+    console.error(
+      'stats.json has no `entrypoints` key -- was it generated with ' +
+        '`--stats=normal` (or richer)? `minimal`/`errors-only` stats omit it.',
+    );

Review Comment:
   Fixed — the message was actually pointing at the wrong config. This repo 
deliberately avoids `--stats=normal` (huge module graph, blows past Node's max 
string length), so the real switch is `BUNDLE_SIZE_STATS=true`. Updated the 
message to name that instead.



##########
superset-frontend/spec/scripts/bundle-size-summary.test.js:
##########
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+const fs = require('fs');
+const {
+  entrypointSizeByExt,
+  main,
+} = require('../../scripts/bundle-size-summary');
+
+function mockStats(entrypoints) {
+  jest
+    .spyOn(fs, 'readFileSync')
+    .mockReturnValue(JSON.stringify({ entrypoints }));
+}
+
+function mockExit() {
+  return jest.spyOn(process, 'exit').mockImplementation(() => {
+    throw new Error('process.exit called');
+  });
+}
+
+afterEach(() => {
+  jest.restoreAllMocks();
+});
+
+test('entrypointSizeByExt sums only assets matching the given extension', () 
=> {
+  const entrypoint = {
+    assets: [
+      { name: 'spa.entry.js', size: 100 },
+      { name: 'spa.entry.js.map', size: 500 },
+      { name: 'spa.entry.css', size: 20 },
+    ],
+  };
+  expect(entrypointSizeByExt(entrypoint, '.js')).toBe(100);
+  expect(entrypointSizeByExt(entrypoint, '.css')).toBe(20);
+});
+
+test('entrypointSizeByExt returns 0 when the entrypoint has no assets', () => {
+  expect(entrypointSizeByExt({}, '.js')).toBe(0);
+});
+
+test('main prints byte totals for every tracked entrypoint', () => {
+  mockStats({
+    spa: { assets: [{ name: 'spa.js', size: 100 }] },
+    embedded: { assets: [{ name: 'embedded.js', size: 50 }] },
+  });
+  const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
+  process.argv = ['node', 'bundle-size-summary.js', 'stats.json'];

Review Comment:
   Fixed — saving and restoring `process.argv` in `afterEach` now, alongside 
`jest.restoreAllMocks()`.



##########
superset-frontend/spec/scripts/bundle-size-summary.test.js:
##########
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+const fs = require('fs');
+const {
+  entrypointSizeByExt,
+  main,
+} = require('../../scripts/bundle-size-summary');
+
+function mockStats(entrypoints) {
+  jest
+    .spyOn(fs, 'readFileSync')
+    .mockReturnValue(JSON.stringify({ entrypoints }));
+}
+
+function mockExit() {
+  return jest.spyOn(process, 'exit').mockImplementation(() => {
+    throw new Error('process.exit called');
+  });
+}
+
+afterEach(() => {
+  jest.restoreAllMocks();
+});
+
+test('entrypointSizeByExt sums only assets matching the given extension', () 
=> {
+  const entrypoint = {
+    assets: [
+      { name: 'spa.entry.js', size: 100 },
+      { name: 'spa.entry.js.map', size: 500 },
+      { name: 'spa.entry.css', size: 20 },
+    ],
+  };
+  expect(entrypointSizeByExt(entrypoint, '.js')).toBe(100);
+  expect(entrypointSizeByExt(entrypoint, '.css')).toBe(20);
+});
+
+test('entrypointSizeByExt returns 0 when the entrypoint has no assets', () => {
+  expect(entrypointSizeByExt({}, '.js')).toBe(0);
+});
+
+test('main prints byte totals for every tracked entrypoint', () => {
+  mockStats({
+    spa: { assets: [{ name: 'spa.js', size: 100 }] },
+    embedded: { assets: [{ name: 'embedded.js', size: 50 }] },
+  });
+  const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
+  process.argv = ['node', 'bundle-size-summary.js', 'stats.json'];
+
+  main();
+
+  const printed = JSON.parse(logSpy.mock.calls[0][0]);
+  expect(printed).toEqual([
+    { name: 'spa entrypoint (JS)', unit: 'bytes', value: 100 },
+    { name: 'spa entrypoint (CSS)', unit: 'bytes', value: 0 },
+    { name: 'embedded entrypoint (JS)', unit: 'bytes', value: 50 },
+    { name: 'embedded entrypoint (CSS)', unit: 'bytes', value: 0 },
+  ]);
+});
+
+test('main exits with an error when a tracked entrypoint is missing from 
stats.json', () => {
+  mockStats({ spa: { assets: [] } });
+  const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
+  mockExit();
+  process.argv = ['node', 'bundle-size-summary.js', 'stats.json'];

Review Comment:
   Fixed — saving and restoring `process.argv` in `afterEach` now, alongside 
`jest.restoreAllMocks()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to