Skip to content

Commit f173a4e

Browse files
authored
chore(web): reduce bundle size and add regression checks when packaging CLOUDP-278538 (#6452)
* chore(schema): replace moment usage with Date * chore(webpack-config): make sure lg testing tools are not bundled with the app * chore(web): add a local polyfill for tr46; add bundle size checks
1 parent 69ac5e8 commit f173a4e

File tree

8 files changed

+80
-11
lines changed

8 files changed

+80
-11
lines changed

configs/webpack-config-compass/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ const sharedResolveOptions = (
107107
// This is an optional dependency of the AWS SDK that doesn't look like
108108
// an optional dependency to webpack because it's not wrapped in try/catch.
109109
'@aws-sdk/client-sso-oidc': false,
110+
111+
// Some lg test helpers that are getting bundled due to re-exporting from
112+
// the actual component packages, never needed in the webpack bundles
113+
'@lg-tools/test-harnesses': false,
110114
},
111115
};
112116
};

package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-schema/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"leaflet-defaulticon-compatibility": "^0.1.1",
9090
"leaflet-draw": "^1.0.4",
9191
"lodash": "^4.17.21",
92-
"moment": "^2.29.4",
9392
"mongodb": "^6.9.0",
9493
"mongodb-query-util": "^2.2.9",
9594
"mongodb-schema": "^12.2.0",

packages/compass-schema/src/modules/date.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable no-use-before-define */
22
import d3 from 'd3';
33
import { isEqual, range, minBy, maxBy, sortBy, groupBy, map } from 'lodash';
4-
import moment from 'moment';
54
import { inValueRange } from 'mongodb-query-util';
65
import { palette, spacing } from '@mongodb-js/compass-components';
76

@@ -33,7 +32,15 @@ const minicharts_d3fns_date = (changeQueryFn) => {
3332
const options = {};
3433
const subcharts = [];
3534

36-
const weekdayLabels = moment.weekdays();
35+
const weekdayLabels = [
36+
'Sunday',
37+
'Monday',
38+
'Tuesday',
39+
'Wednesday',
40+
'Thursday',
41+
'Friday',
42+
'Saturday',
43+
];
3744

3845
// A formatter for dates
3946
const format = d3.time.format.utc('%Y-%m-%d %H:%M:%S');
@@ -215,7 +222,7 @@ const minicharts_d3fns_date = (changeQueryFn) => {
215222

216223
// group by weekdays
217224
const w = groupBy(values, function (d) {
218-
return moment(d.ts).weekday();
225+
return new Date(d.ts).getDay();
219226
});
220227
const wd = { ...generateDefaults(7), ...w };
221228
const weekdays = map(wd, function (d, i) {

packages/compass-web/.mocharc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
'use strict';
2-
module.exports = require('@mongodb-js/mocha-config-compass/compass-plugin');
2+
const config = require('@mongodb-js/mocha-config-compass/compass-plugin');
3+
4+
module.exports = {
5+
...config,
6+
spec: [...config.spec, 'polyfills/**/*.spec.*', 'polyfills/**/*.test.*'],
7+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect } from 'chai';
2+
import { toASCII } from './index';
3+
4+
// Keep in sync with https://github.com/jsdom/tr46/blob/main/scripts/getLatestTests.js when updating whatwg-url
5+
const wptSHA = '72b915d4b3754f081ef5899bf6a777efe71b2fc5';
6+
7+
describe('tr46 polyfill', function () {
8+
describe('toASCII', function () {
9+
let tests: { input: string; output: string }[] = [];
10+
11+
before(async function () {
12+
tests = await fetch(
13+
`https://raw.githubusercontent.com/web-platform-tests/wpt/${wptSHA}/url/resources/toascii.json`
14+
).then((res) => res.json());
15+
});
16+
17+
it('should pass wpt specs', function () {
18+
for (const test of tests) {
19+
// String items are just comments in the test data
20+
if (typeof test === 'string') {
21+
return;
22+
}
23+
expect(toASCII(test.input)).to.eq(test.output);
24+
}
25+
});
26+
});
27+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* This is a dependency of whatwg-url package, we can't fully replace it with
3+
* the globalThis.URL due to subtle differences in packages behavior, but we can
4+
* substitue one of the biggest chunks of the package (tr46) with a browser
5+
* implementation.
6+
*/
7+
export function toASCII(domain: string) {
8+
try {
9+
return new window.URL(`http://${domain}`).hostname;
10+
} catch {
11+
return null;
12+
}
13+
}
14+
15+
export function toUnicode() {
16+
throw new Error('Not implemented');
17+
}

packages/compass-web/webpack.config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ function localPolyfill(name) {
1515
return path.resolve(__dirname, 'polyfills', ...name.split('/'), 'index.ts');
1616
}
1717

18+
/**
19+
* Atlas Cloud uses in-flight compression that doesn't compress anything that is
20+
* bigger than 10MB, we want to make sure that compass-web assets stay under the
21+
* limit so that they are compressed when served
22+
*/
23+
const MAX_COMPRESSION_FILE_SIZE = 10_000_000;
24+
1825
module.exports = (env, args) => {
1926
const serve = isServe({ env });
2027

@@ -28,7 +35,6 @@ module.exports = (env, args) => {
2835

2936
config = merge(config, {
3037
context: __dirname,
31-
3238
resolve: {
3339
alias: {
3440
// Dependencies for the unsupported connection types in data-service
@@ -90,12 +96,13 @@ module.exports = (env, args) => {
9096
vm: require.resolve('vm-browserify'),
9197

9298
// TODO(NODE-5408): requires a polyfill to be able to parse connection
93-
// string correctly at the moment, but we should also omit some
94-
// depdendencies that might not be required for this to work in the
95-
// browser
99+
// string correctly at the moment
96100
url: require.resolve('whatwg-url'),
97101
// Make sure we're not getting multiple versions included
98102
'whatwg-url': require.resolve('whatwg-url'),
103+
// Heavy dependency of whatwg-url that we can replace in the browser
104+
// environment
105+
tr46: localPolyfill('tr46'),
99106

100107
// Polyfills that are required for the driver to function in browser
101108
// environment
@@ -140,6 +147,11 @@ module.exports = (env, args) => {
140147
process: [localPolyfill('process'), 'process'],
141148
}),
142149
],
150+
performance: {
151+
hints: serve ? 'warning' : 'error',
152+
maxEntrypointSize: MAX_COMPRESSION_FILE_SIZE,
153+
maxAssetSize: MAX_COMPRESSION_FILE_SIZE,
154+
},
143155
});
144156

145157
if (serve) {

0 commit comments

Comments
 (0)