Skip to content

Commit caf0fde

Browse files
committed
fix: fix problem with missing worker in compiled version
1 parent 780cb9a commit caf0fde

6 files changed

Lines changed: 704 additions & 28 deletions

File tree

.claude/settings.local.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
"Bash(pnpm test:*)",
99
"Bash(CI=1 pnpm test:*)",
1010
"Bash(CI=1 npm test:*)",
11-
"Bash(pnpm remove:*)"
11+
"Bash(pnpm remove:*)",
12+
"Bash(npm test:*)",
13+
"Bash(npx tsc:*)",
14+
"Bash(ls:*)",
15+
"Bash(pnpm run build:*)",
16+
"Bash(pnpm exec vitest:*)"
1217
],
1318
"deny": [],
1419
"ask": []

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"module": "lib/esm/src/index",
3232
"typings": "lib/cjs/src/index",
3333
"scripts": {
34-
"build": "tsc --project tsconfig.json && tsc --project tsconfig.esm.json",
34+
"build": "tsc --project tsconfig.json && tsc --project tsconfig.esm.json && sed -i 's/computeWorker\\.ts/computeWorker.js/g' lib/cjs/src/compute-lines.js lib/esm/src/compute-lines.js",
3535
"build:examples": "vite build examples",
3636
"publish:examples": "NODE_ENV=production pnpm run build:examples && gh-pages -d examples/dist -r $GITHUB_REPO_URL",
3737
"publish:examples:local": "NODE_ENV=production pnpm run build:examples && gh-pages -d examples/dist",

src/compute-lines.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,24 +652,32 @@ const computeLineInformationWorker = (
652652
showLines: string[] = [],
653653
deferWordDiff = false
654654
): Promise<ComputedLineInformation> => {
655+
const fallback = () => computeLineInformation(oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff);
656+
655657
// Fall back to synchronous computation if Worker is not available (e.g., in Node.js/test environments)
656658
if (typeof Worker === 'undefined') {
657-
return Promise.resolve(
658-
computeLineInformation(oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff)
659-
);
659+
return Promise.resolve(fallback());
660660
}
661661

662-
return new Promise((resolve, reject) => {
663-
const worker = new Worker(new URL('./computeWorker.ts', import.meta.url), { type: 'module' });
662+
return new Promise((resolve) => {
663+
let worker: Worker;
664+
try {
665+
worker = new Worker(new URL('./computeWorker.ts', import.meta.url), { type: 'module' });
666+
} catch {
667+
// Worker instantiation failed - fall back to synchronous computation
668+
resolve(fallback());
669+
return;
670+
}
664671

665672
worker.onmessage = (e) => {
666673
resolve(e.data);
667674
worker.terminate();
668675
};
669676

670-
worker.onerror = (err) => {
671-
reject(err);
677+
worker.onerror = () => {
678+
// Worker error - fall back to synchronous computation
672679
worker.terminate();
680+
resolve(fallback());
673681
};
674682

675683
worker.postMessage({ oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff });

0 commit comments

Comments
 (0)