From 4683d691ed4c5bfb89a1f7c6bdf0916ddbf3de07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 17 Apr 2024 09:16:38 +0200 Subject: [PATCH] Suggestions to the "BSON v6 upgrade" (#6618) * Fixed a warning in the realm-web Rollup config * Moved typescript and commonjs plugin down and stripping comments to lower IIFE bundle size * Adding an alias for "bson" pointing to the .cjs bundle * Upgrading ts-loader in the realm-web integration tests * Updating tsconfigs in the realm web integration tests * Using ESM when importing realm-web integration test files --- package-lock.json | 33 ++++++++++++++++++- package.json | 1 + .../realm-web-integration-tests/package.json | 2 +- .../realm-web-integration-tests/src/index.ts | 24 +++++++------- .../src/services.test.ts | 6 ++-- .../realm-web-integration-tests/tsconfig.json | 7 ++-- .../tsconfig.web.json | 2 +- packages/realm-web/rollup.config.mjs | 26 ++++++++++++--- 8 files changed, 75 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3e5970a1f73..d9c581118b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ ], "dependencies": { "@react-native/eslint-config": "0.73.2", + "@rollup/plugin-alias": "^5.1.0", "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", @@ -4872,6 +4873,36 @@ "node": ">=14.15" } }, + "node_modules/@rollup/plugin-alias": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-alias/-/plugin-alias-5.1.0.tgz", + "integrity": "sha512-lpA3RZ9PdIG7qqhEfv79tBffNaoDuukFDrmhLqg9ifv99u/ehn+lOg30x2zmhf8AQqQUZaMk/B9fZraQ6/acDQ==", + "dependencies": { + "slash": "^4.0.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-alias/node_modules/slash": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz", + "integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@rollup/plugin-commonjs": { "version": "25.0.7", "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-25.0.7.tgz", @@ -26239,7 +26270,7 @@ "html-webpack-plugin": "^5.5.0", "mongodb-realm-cli": "^1.3.2", "source-map-loader": "^5.0.0", - "ts-loader": "^9.3.0", + "ts-loader": "^9.5.1", "webpack-cli": "^5.1.4" }, "peerDependencies": { diff --git a/package.json b/package.json index 2272c017dab..08e59ce439e 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ ], "dependencies": { "@react-native/eslint-config": "0.73.2", + "@rollup/plugin-alias": "^5.1.0", "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", diff --git a/packages/realm-web-integration-tests/package.json b/packages/realm-web-integration-tests/package.json index b6dbbc6d1e5..41da146e3d0 100644 --- a/packages/realm-web-integration-tests/package.json +++ b/packages/realm-web-integration-tests/package.json @@ -54,7 +54,7 @@ "html-webpack-plugin": "^5.5.0", "mongodb-realm-cli": "^1.3.2", "source-map-loader": "^5.0.0", - "ts-loader": "^9.3.0", + "ts-loader": "^9.5.1", "webpack-cli": "^5.1.4" } } diff --git a/packages/realm-web-integration-tests/src/index.ts b/packages/realm-web-integration-tests/src/index.ts index 04d0cda8f20..e74bb8621cc 100644 --- a/packages/realm-web-integration-tests/src/index.ts +++ b/packages/realm-web-integration-tests/src/index.ts @@ -24,25 +24,25 @@ if (location.pathname.endsWith("-callback")) { Realm.handleAuthRedirect(); } else if (location.pathname.endsWith("/google-login")) { console.log("Hello to Google Login ..."); - require("./google-login"); + await import("./google-login"); } else { new MochaRemoteClient({ - tests: () => { + async tests() { beforeEach(function () { this.slow(1000); this.timeout(10000); }); - require("./environment.test"); - require("./app.test"); - require("./credentials.test"); - require("./user.test"); - require("./functions.test"); - require("./services.test"); - require("./api-key-auth.test"); - require("./email-password-auth.test"); - require("./iife.test"); - require("./bson.test"); + await import("./environment.test"); + await import("./app.test"); + await import("./credentials.test"); + await import("./user.test"); + await import("./functions.test"); + await import("./services.test"); + await import("./api-key-auth.test"); + await import("./email-password-auth.test"); + await import("./iife.test"); + await import("./bson.test"); }, }); } diff --git a/packages/realm-web-integration-tests/src/services.test.ts b/packages/realm-web-integration-tests/src/services.test.ts index 5808f4f8109..b90d19e8f29 100644 --- a/packages/realm-web-integration-tests/src/services.test.ts +++ b/packages/realm-web-integration-tests/src/services.test.ts @@ -16,7 +16,5 @@ // //////////////////////////////////////////////////////////////////////////// -describe("App services", () => { - require("./remote-mongodb-service.test"); - require("./http-service.test"); -}); +import "./remote-mongodb-service.test"; +import "./http-service.test"; diff --git a/packages/realm-web-integration-tests/tsconfig.json b/packages/realm-web-integration-tests/tsconfig.json index c402f161d8d..0849a62c5f6 100644 --- a/packages/realm-web-integration-tests/tsconfig.json +++ b/packages/realm-web-integration-tests/tsconfig.json @@ -2,10 +2,11 @@ "compilerOptions": { "strict": true, "sourceMap": true, - "module": "commonjs", - "target": "ES2019", + "module": "ES2022", + "target": "ES2022", + "moduleResolution": "Bundler", "lib": [ - "ES2019", + "ES2022", "DOM" ], "types": [ diff --git a/packages/realm-web-integration-tests/tsconfig.web.json b/packages/realm-web-integration-tests/tsconfig.web.json index c084f538c19..3c1e34dce2d 100644 --- a/packages/realm-web-integration-tests/tsconfig.web.json +++ b/packages/realm-web-integration-tests/tsconfig.web.json @@ -9,7 +9,7 @@ "mocha-remote-client" ], "lib": [ - "ES2020", + "ES2022", "DOM" ] }, diff --git a/packages/realm-web/rollup.config.mjs b/packages/realm-web/rollup.config.mjs index 4d7ac465424..b1a3249bc06 100644 --- a/packages/realm-web/rollup.config.mjs +++ b/packages/realm-web/rollup.config.mjs @@ -16,16 +16,23 @@ // //////////////////////////////////////////////////////////////////////////// +import alias from "@rollup/plugin-alias"; import commonjs from "@rollup/plugin-commonjs"; import typescript from "@rollup/plugin-typescript"; import nodeResolve from "@rollup/plugin-node-resolve"; import replace from "@rollup/plugin-replace"; import dts from "rollup-plugin-dts"; +import { createRequire } from "node:module"; +const require = createRequire(import.meta.url); + import pkg from "./package.json" assert { type: "json" }; const replacer = replace({ - __SDK_VERSION__: JSON.stringify(pkg.version), + preventAssignment: true, + values: { + __SDK_VERSION__: JSON.stringify(pkg.version), + }, }); export default [ @@ -97,9 +104,15 @@ export default [ }, ], plugins: [ - commonjs(), - typescript({ - tsconfig: "src/dom/tsconfig.json", + // Providing an alias for the "bson" package + // See https://github.com/mongodb/js-bson/pull/669#pullrequestreview-1994786536 for more context + alias({ + entries: [ + { + find: "bson", + replacement: require.resolve("bson"), + }, + ], }), nodeResolve({ mainFields: ["browser", "module", "main"], @@ -107,6 +120,11 @@ export default [ modulesOnly: true, preferBuiltins: false, }), + commonjs(), + typescript({ + tsconfig: "src/dom/tsconfig.json", + removeComments: true, + }), replacer, ], },