Skip to content

Commit 13aeb36

Browse files
authored
fix(new-compiler): use dynamic import for lmdb to avoid CJS transform errors (#2015)
1 parent e08bbca commit 13aeb36

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

.changeset/hungry-taxes-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lingo.dev/compiler": patch
3+
---
4+
5+
Fixed SyntaxError caused by bundlers/require hooks transforming lmdb's CJS bundle

packages/new-compiler/src/metadata/manager.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs";
22
import path from "path";
3-
import { open, type RootDatabase } from "lmdb";
3+
import type { RootDatabase } from "lmdb";
44
import type { MetadataSchema, PathConfig, TranslationEntry } from "../types";
55
import { getLingoDir } from "../utils/path-helpers";
66
import { logger } from "../utils/logger";
@@ -13,10 +13,14 @@ const METADATA_DIR_BUILD = "metadata-build";
1313
*
1414
* lmdb-js deduplicates open() calls to the same path (ref-counted at C++ level),
1515
* so this is cheap. Each open() also clears stale readers from terminated workers.
16+
*
17+
* lmdb is loaded via dynamic import() to prevent bundlers and require hooks
18+
* from transforming its CJS bundle, which contains syntax they can't handle.
1619
*/
17-
function openDatabaseConnection(dbPath: string, noSync: boolean): RootDatabase {
20+
async function openDatabaseConnection(dbPath: string, noSync: boolean): Promise<RootDatabase> {
1821
try {
1922
fs.mkdirSync(dbPath, { recursive: true });
23+
const { open } = await import("lmdb");
2024
return open({
2125
path: dbPath,
2226
compression: true,
@@ -52,7 +56,7 @@ async function runWithDbConnection<T>(
5256
noSync: boolean,
5357
fn: (db: RootDatabase) => T,
5458
): Promise<T> {
55-
const db = openDatabaseConnection(dbPath, noSync);
59+
const db = await openDatabaseConnection(dbPath, noSync);
5660
try {
5761
return fn(db);
5862
} finally {

0 commit comments

Comments
 (0)