From cfca7b6a0a68e93a59de3013826bb6573875dd26 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Mon, 25 May 2026 13:53:11 +0000 Subject: [PATCH] fix: keep loro-crdt sourcemap sources inside the package (#947) 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: Claude Opus 4.7 (1M context) --- .changeset/fix-sourcemap-package-boundary.md | 5 ++++ crates/loro-wasm/rollup.config.mjs | 30 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 .changeset/fix-sourcemap-package-boundary.md diff --git a/.changeset/fix-sourcemap-package-boundary.md b/.changeset/fix-sourcemap-package-boundary.md new file mode 100644 index 000000000..8401afbcb --- /dev/null +++ b/.changeset/fix-sourcemap-package-boundary.md @@ -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. diff --git a/crates/loro-wasm/rollup.config.mjs b/crates/loro-wasm/rollup.config.mjs index d8dffa3b8..b511509c2 100644 --- a/crates/loro-wasm/rollup.config.mjs +++ b/crates/loro-wasm/rollup.config.mjs @@ -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', @@ -9,6 +37,7 @@ const createConfig = (format, tsTarget, outputDir) => ({ dir: outputDir, format: format, sourcemap: true, + sourcemapPathTransform, entryFileNames: '[name].js', }, plugins: [ @@ -18,6 +47,7 @@ const createConfig = (format, tsTarget, outputDir) => ({ target: tsTarget, declaration: true, outDir: outputDir, + inlineSources: true, }, exclude: ['tests/**/*', 'vite.config.*'] }),