diff --git a/CHANGELOG.md b/CHANGELOG.md index 7926e7a09c6..016fb9d2627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ x.x.x Release notes (yyyy-MM-dd) ### Fixed * Logging out too fast can cause an error if the timeout behavior is set to `openLocalRealm` ([4453](https://github.com/realm/realm-js/issues/4453)) * Released `realm-network-transport` to adopt the changes published to fix `globalThis` undefined issue for older devices. ([#4350](https://github.com/realm/realm-js/4350)) +* Changed "react-native" main field to point to a `lib/index.native.js` file to help bundlers pick the right file when loading our library on React Native. ([#4459](https://github.com/realm/realm-js/issues/4459)) ### Compatibility * MongoDB Realm Cloud. @@ -4409,4 +4410,4 @@ Old files can still be opened and files open in read-only mode will not be modif 0.10.0 Release notes (2016-2-22) ============================================================= ### Enhancements -* Initial Release \ No newline at end of file +* Initial Release diff --git a/lib/index.native.js b/lib/index.native.js new file mode 100644 index 00000000000..8d85c4e607a --- /dev/null +++ b/lib/index.native.js @@ -0,0 +1,20 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2022 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. +// +//////////////////////////////////////////////////////////////////////////// + +const { Realm } = require("./react-native"); +module.exports = Realm; diff --git a/lib/react-native/index.js b/lib/react-native/index.js index 82cb98b9745..6e42713082d 100644 --- a/lib/react-native/index.js +++ b/lib/react-native/index.js @@ -18,6 +18,9 @@ import { Platform, NativeModules } from "react-native"; +import utils from "../utils"; +import extend from "../extensions"; + //switch how babel transpiled code creates children objects. //Inheriting from Realm.Object with class syntax does not support using Reflect.construct the way babel transpiles it. //Defining Reflect.construct.sham makes the transpiled code use different standard mechanism for inheriting. (Function.apply with setPrototypeOf) @@ -39,7 +42,7 @@ if (Platform.OS === "android") { // TODO: Remove the need to store Realm as a global // @see https://github.com/realm/realm-js/issues/2126 -const Realm = globalThis.Realm; +export const Realm = globalThis.Realm; // Otherwise, we must be in a "normal" react native situation. // In that case, the Realm type should have been injected by the native code. @@ -50,10 +53,7 @@ if (typeof Realm === "undefined") { ); } -require("../extensions")(Realm); +extend(Realm); -const utils = require("../utils"); const versions = utils.getVersions(); Realm.App._setVersions(versions); - -module.exports = Realm; diff --git a/package.json b/package.json index ed1170a1f7e..60e6e3693e6 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "types": "types/index.d.ts", "main": "lib/index.js", - "react-native": "lib/react-native/index.js", + "react-native": "lib/index.native.js", "files": [ "cmake", "lib", @@ -162,4 +162,4 @@ 4 ] } -} +} \ No newline at end of file