Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-sourcemap-package-boundary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"loro-crdt": patch
---

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.
30 changes: 30 additions & 0 deletions crates/loro-wasm/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import fs from 'node:fs';
import path from 'node:path';
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';

const packageRoot = path.resolve();

// Rewrite source paths in the emitted sourcemap so they stay inside the
// published package. @rollup/plugin-typescript hands rollup a map whose
// `sources` are relative to TS's intended output location, but rollup
// resolves them against the .ts module's directory — yielding paths like
// "../../index.ts" relative to the published sourcemap that escape the
// package and trigger Vite/Vitest's "source file outside its package"
// warning when downstream users install loro-crdt. See #947.
const sourcemapPathTransform = (relativePath, sourcemapPath) => {
const sourcemapDir = path.dirname(sourcemapPath);
const absoluteSource = path.resolve(sourcemapDir, relativePath);
if (absoluteSource.startsWith(packageRoot + path.sep) && fs.existsSync(absoluteSource)) {
return relativePath.split(path.sep).join('/');
}
// The reported path resolves outside the package. Locate the real file
// inside the package by basename and produce a path relative to the
// sourcemap that stays inside the published tree.
const fileName = path.basename(absoluteSource);
const candidate = path.resolve(packageRoot, fileName);
if (fs.existsSync(candidate)) {
return path.relative(sourcemapDir, candidate).split(path.sep).join('/');
}
return relativePath;
};

const createConfig = (format, tsTarget, outputDir) => ({
input: {
'index': 'index.ts',
Expand All @@ -9,6 +37,7 @@ const createConfig = (format, tsTarget, outputDir) => ({
dir: outputDir,
format: format,
sourcemap: true,
sourcemapPathTransform,
entryFileNames: '[name].js',
},
plugins: [
Expand All @@ -18,6 +47,7 @@ const createConfig = (format, tsTarget, outputDir) => ({
target: tsTarget,
declaration: true,
outDir: outputDir,
inlineSources: true,
},
exclude: ['tests/**/*', 'vite.config.*']
}),
Expand Down
Loading