Skip to content

Commit a42f4ea

Browse files
committed
chore(web): add a local polyfill for tr46; add bundle size checks
1 parent 12ebc97 commit a42f4ea

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

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)