Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only export a CJS bundle (using rollup) #5882

Merged
merged 10 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## vNext (TBD)

### Breaking changes
* Now exporting only as CommonJS, to align with the way we exported from v11 in an attempt to keep breakage across the major version to an absolute minimum. This is a breaking change compared to the previous pre-releases of v12, since users have to update code which is doing named import of `Realm` to use default or `* as Realm` imports of the `Realm` constructor. ([#5882](/~https://github.com/realm/realm-js/pull/5882))
* `SyncSession` JS objects no longer keep their associated C++ objects, and therefore the sync network connection, alive. This was causing issues because JS garbage collection is lazy so the `SyncSession` may survive much longer than the last reference held to it. We now use the same technique as v11 to avoid keeping the C++ object alive (`std::weak_ptr`). ([#5815](/~https://github.com/realm/realm-js/pull/5815), since v12.0.0-alpha.0)
* Breaking change: On v11, if the C++ object had been destroyed already, we would often return `undefined` or some other default value when calling methods or accessing properties on the JS `SyncSession` object, even if that would violate our declared TS types. Now, in v12, we will throw from all methods and property accessors in this case.

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/hooks/import-app-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { Realm, AppConfiguration } from "realm";
import Realm, { AppConfiguration } from "realm";

import { importApp } from "../utils/import-app";
import { AppConfig } from "@realm/app-importer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { Realm } from "realm";
import Realm from "realm";

import { describePerformance } from "../utils/benchmark";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/schemas/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/* tslint:disable max-classes-per-file */

import { Realm } from "realm";
import Realm from "realm";

export interface IContact {
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/* tslint:disable max-classes-per-file */

import { Realm } from "realm";
import Realm from "realm";

export interface IPerson {
_id: Realm.BSON.ObjectId;
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/schemas/person-and-dogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/* tslint:disable max-classes-per-file */

import { Realm } from "realm";
import Realm from "realm";

export interface IPerson {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/setup-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Test Harness", function (this: Mocha.Suite) {
Context.prototype.longTimeout = longTimeout;
});

import { Realm } from "realm";
import Realm from "realm";

// Disable the logger to avoid console flooding
const { defaultLogLevel = "off" } = environment;
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/class-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { openRealmBeforeEach } from "../hooks";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";
import { openRealmBeforeEach } from "../hooks";

import { PersonSchema, DogSchema } from "../schemas/person-and-dogs";
Expand Down
10 changes: 10 additions & 0 deletions integration-tests/tests/src/tests/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
OpenRealmBehaviorType,
OpenRealmTimeOutBehavior,
SessionStopPolicy,
WaitForSync,
} from "realm";

describe("Enums", function () {
Expand Down Expand Up @@ -84,4 +85,13 @@ describe("Enums", function () {
});
});
});
describe("WaitForSync", function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part of CJS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me forgetting to add this enum test when doing the Flx Sync v2 PR 😉

it("is accessible", function () {
expect(WaitForSync).deep.equals({
Always: "always",
FirstTime: "first-time",
Never: "never",
});
});
});
});
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { Realm, BSON } from "realm";
import Realm, { BSON } from "realm";
import { expectArraysEqual, expectSimilar } from "../utils/comparisons";
import { expect } from "chai";
import { CanonicalObjectSchema } from "realm";
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";
import { openRealmBeforeEach } from "../hooks";

import { PersonSchema, DogSchema } from "../schemas/person-and-dogs";
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { IPerson, Person, PersonSchema } from "../schemas/person-and-dogs";
import {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/realm-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { IPerson, PersonSchema, DogSchema } from "../schemas/person-and-dogs";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { expect } from "chai";
import { openRealmBefore } from "../hooks";
import { Realm } from "realm";
import Realm from "realm";

interface Test {
primary: number;
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

type DefaultObject = Record<string, unknown>;

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { openRealmBefore } from "../hooks";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/shared-realms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm, List } from "realm";
import Realm, { List } from "realm";

import { openRealmBefore, openRealmBeforeEach } from "../hooks";
import { createLocalConfig } from "../utils/open-realm";
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/tests/src/tests/sync/flexible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
// fraction too long.

import { expect } from "chai";
import {
import Realm, {
BSON,
ClientResetMode,
ConfigurationWithSync,
ErrorCallback,
FlexibleSyncConfiguration,
Realm,
SessionStopPolicy,
SubscriptionSetState,
CompensatingWriteError,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/sync/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { importAppBefore, authenticateUserBefore, openRealmBefore } from "../../hooks";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/sync/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { authenticateUserBefore, importAppBefore } from "../../hooks";
import { buildAppConfig } from "../../utils/build-app-config";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { Realm, BSON } from "realm";
import Realm, { BSON } from "realm";
import { expect } from "chai";
import { importAppBefore } from "../../hooks";
import { getRegisteredEmailPassCredentials } from "../../utils/credentials";
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/sync/sync-as-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

import { PersonSchema, IPerson } from "../../schemas/person-and-dog-with-object-ids";
import { authenticateUserBefore, importAppBefore, openRealmBefore } from "../../hooks";
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/sync/sync-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm, ConnectionState, ObjectSchema, BSON, User, SyncConfiguration } from "realm";
import Realm, { ConnectionState, ObjectSchema, BSON, User, SyncConfiguration } from "realm";
import { importAppBefore } from "../../hooks";
import { DogSchema } from "../../schemas/person-and-dog-with-object-ids";
import { getRegisteredEmailPassCredentials } from "../../utils/credentials";
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/sync/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
randomVerifiableEmail,
} from "../../utils/generators";
import { KJUR } from "jsrsasign";
import { Realm, UserState } from "realm";
import Realm, { UserState } from "realm";

import { buildAppConfig } from "../../utils/build-app-config";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/tests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { Realm } from "realm";
import Realm from "realm";

describe("Realm.Types namespace", () => {
// We specify explicit types on the instance so TS will error if the type def is wrong
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/utils/open-realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { Realm, Configuration, SyncConfiguration, User, BSON } from "realm";
import Realm, { Configuration, SyncConfiguration, User, BSON } from "realm";

// Either the sync property is left out (local Realm)
export type LocalConfiguration = Omit<Configuration, "sync"> & { sync?: never };
Expand Down
30 changes: 30 additions & 0 deletions packages/realm-react/custom-resolver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed 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.
//
////////////////////////////////////////////////////////////////////////////

/* eslint-env node */

module.exports = function (request, options) {
if (request === "react-native") {
// Ensure that "react-native" always resolve relative to the rootDir
options.basedir = options.rootDir;
} else if (request === "realm") {
// Ensure the node binding is used when testing with Jest on node
options.conditions = ["require", "default", "node"];
}
return options.defaultResolver(request, options);
};
Comment on lines +21 to +30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@takameyer to be honest, I don't understand why this worked before.

  • The first branch will ensure "react-native" from @realm/react is loaded, instead of the peer dependency of realm(realm/node_modules/react-native), as that yielded Invariant Violation: __fbBatchedBridgeConfig is not set, cannot invoke native modules errors
  • The second branch is to ensure the Node.js version of the SDK is loaded. If I didn't do this, I would be getting TypeError: Cannot read properties of undefined (reading 'Helpers_get_table') errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I've never seen those issues before. Why would this happen with cjs?

1 change: 1 addition & 0 deletions packages/realm-react/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"verbose": true,
"preset": "react-native",
"resolver": "./custom-resolver.cjs",
"roots": [
"<rootDir>/src"
],
Expand Down
22 changes: 22 additions & 0 deletions packages/realm/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed 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.
//
////////////////////////////////////////////////////////////////////////////

/* eslint-disable @typescript-eslint/no-var-requires -- We're exporting using CJS assignment */
/* eslint-env commonjs */

module.exports = require("./dist/bundle.node").Realm;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love it, but it works 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we have proper entry points set for node and React Native. Let's break everything in v13 💣

22 changes: 22 additions & 0 deletions packages/realm/index.react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed 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.
//
////////////////////////////////////////////////////////////////////////////

/* eslint-disable @typescript-eslint/no-var-requires -- We're exporting using CJS assignment */
/* eslint-env commonjs */

module.exports = require("./dist/bundle.react-native").Realm;
14 changes: 6 additions & 8 deletions packages/realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
"email": "help@realm.io",
"url": "https://realm.io"
},
"main": "./dist/bundle.node.js",
"module": "./dist/bundle.node.mjs",
"types": "./dist/bundle.d.ts",
"react-native": "./dist/bundle.react-native.mjs",
"types": "./types.d.cts",
"main": "./index.node.js",
"react-native": "./index.react-native.js",
"exports": {
".": {
"types": "./dist/bundle.d.ts",
"require": "./dist/bundle.node.js",
"node": "./dist/bundle.node.mjs",
"react-native": "./dist/bundle.react-native.mjs"
"types": "./types.d.cts",
"node": "./index.node.js",
"react-native": "./index.react-native.js"
},
"./scripts/submit-analytics": "./scripts/submit-analytics.mjs",
"./react-native.config.js": "./react-native.config.js",
Expand Down
Loading