Skip to content

Commit f3ece99

Browse files
lodyai[bot]zxch3nclaude
authored
fix: keep loro-crdt sourcemap sources inside the package (#947) (#987)
Rewrite rollup output `sources` so they resolve inside the published package and embed `sourcesContent`, removing the Vite/Vitest "source file outside its package" warning users see for loro-crdt. Co-authored-by: Zixuan Chen <zx@loro.dev> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent bb6cd6c commit f3ece99

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"loro-crdt": patch
3+
---
4+
5+
Fix published sourcemap `sources` pointing outside the package. The rollup TypeScript plugin's emitted maps were resolved by rollup against the `.ts` source directory, leaving `../../index.ts` in the published `sources`. Vite/Vitest warned about source files escaping the package. The sourcemap now resolves inside the package and `sourcesContent` is included so debuggers don't need to fetch the TypeScript source.

crates/loro-wasm/rollup.config.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
13
import typescript from '@rollup/plugin-typescript';
24
import { nodeResolve } from '@rollup/plugin-node-resolve';
35

6+
const packageRoot = path.resolve();
7+
8+
// Rewrite source paths in the emitted sourcemap so they stay inside the
9+
// published package. @rollup/plugin-typescript hands rollup a map whose
10+
// `sources` are relative to TS's intended output location, but rollup
11+
// resolves them against the .ts module's directory — yielding paths like
12+
// "../../index.ts" relative to the published sourcemap that escape the
13+
// package and trigger Vite/Vitest's "source file outside its package"
14+
// warning when downstream users install loro-crdt. See #947.
15+
const sourcemapPathTransform = (relativePath, sourcemapPath) => {
16+
const sourcemapDir = path.dirname(sourcemapPath);
17+
const absoluteSource = path.resolve(sourcemapDir, relativePath);
18+
if (absoluteSource.startsWith(packageRoot + path.sep) && fs.existsSync(absoluteSource)) {
19+
return relativePath.split(path.sep).join('/');
20+
}
21+
// The reported path resolves outside the package. Locate the real file
22+
// inside the package by basename and produce a path relative to the
23+
// sourcemap that stays inside the published tree.
24+
const fileName = path.basename(absoluteSource);
25+
const candidate = path.resolve(packageRoot, fileName);
26+
if (fs.existsSync(candidate)) {
27+
return path.relative(sourcemapDir, candidate).split(path.sep).join('/');
28+
}
29+
return relativePath;
30+
};
31+
432
const createConfig = (format, tsTarget, outputDir) => ({
533
input: {
634
'index': 'index.ts',
@@ -9,6 +37,7 @@ const createConfig = (format, tsTarget, outputDir) => ({
937
dir: outputDir,
1038
format: format,
1139
sourcemap: true,
40+
sourcemapPathTransform,
1241
entryFileNames: '[name].js',
1342
},
1443
plugins: [
@@ -18,6 +47,7 @@ const createConfig = (format, tsTarget, outputDir) => ({
1847
target: tsTarget,
1948
declaration: true,
2049
outDir: outputDir,
50+
inlineSources: true,
2151
},
2252
exclude: ['tests/**/*', 'vite.config.*']
2353
}),

0 commit comments

Comments
 (0)