Skip to content

Commit

Permalink
Resolve #1843 - avoid running dexie in double instances.
Browse files Browse the repository at this point in the history
1. Export ony one require-based module and a MJS wrapper for it. Skip dual export.
2. Detect double instances and let 2nd instance return same instance as was already imported.
  • Loading branch information
dfahlander committed Dec 11, 2023
1 parent 7c13770 commit fc572aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 6 additions & 1 deletion import-wrapper-prod.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Making the module version consumable via require - to prohibit
// multiple occurrancies of the same module in the same app
// (dual package hazard, https://nodejs.org/api/packages.html#dual-package-hazard)
import Dexie from "./dist/dexie.min.js";
import _Dexie from "./dist/dexie.min.js";
const DexieSymbol = Symbol.for("Dexie");
const Dexie = globalThis[DexieSymbol] || (globalThis[DexieSymbol] = _Dexie);
if (_Dexie.semVer !== Dexie.semVer) {
throw new Error(`Two different versions of Dexie loaded in the same app: ${_Dexie.semVer} and ${Dexie.semVer}`);
}
const { liveQuery, mergeRanges, rangesOverlap, RangeSet, cmp } = Dexie;
export { liveQuery, mergeRanges, rangesOverlap, RangeSet, cmp, Dexie };
export default Dexie;
7 changes: 6 additions & 1 deletion import-wrapper.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Making the module version consumable via require - to prohibit
// multiple occurrancies of the same module in the same app
// (dual package hazard, https://nodejs.org/api/packages.html#dual-package-hazard)
import Dexie from "./dist/dexie.js";
import _Dexie from "./dist/dexie.js";
const DexieSymbol = Symbol.for("Dexie");
const Dexie = globalThis[DexieSymbol] || (globalThis[DexieSymbol] = _Dexie);
if (_Dexie.semVer !== Dexie.semVer) {
throw new Error(`Two different versions of Dexie loaded in the same app: ${_Dexie.semVer} and ${Dexie.semVer}`);
}
const { liveQuery, mergeRanges, rangesOverlap, RangeSet, cmp } = Dexie;
export { liveQuery, mergeRanges, rangesOverlap, RangeSet, cmp, Dexie };
export default Dexie;
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@
"exports": {
".": {
"production": {
"browser": "./dist/modern/dexie.min.mjs",
"module": "./dist/modern/dexie.min.mjs",
"module": "./import-wrapper-prod.mjs",
"import": "./import-wrapper-prod.mjs",
"require": "./dist/dexie.min.js",
"default": "./dist/dexie.min.js"
},
"development": {
"browser": "./dist/modern/dexie.mjs",
"module": "./dist/modern/dexie.mjs",
"module": "./import-wrapper.mjs",
"import": "./import-wrapper.mjs",
"require": "./dist/dexie.js",
"default": "./dist/dexie.js"
},
"default": {
"browser": "./dist/modern/dexie.mjs",
"module": "./dist/modern/dexie.mjs",
"module": "./import-wrapper.mjs",
"import": "./import-wrapper.mjs",
"require": "./dist/dexie.js",
"default": "./dist/dexie.js"
Expand Down

0 comments on commit fc572aa

Please sign in to comment.