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
53 changes: 45 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
"@github/copilot-sdk": "^1.0.16",
"@openai/codex-sdk": "^0.159.0",
"@types/node": "^25.9.1",
"source-map-js": "file:packages/source-map-compat",
"typescript": "^5.8.0",
"vitest": "^4.1.11",
"zx": "^8.8.5"
},
"overrides": {
"source-map-js": "$source-map-js"
}
}
21 changes: 21 additions & 0 deletions packages/source-map-compat/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright GitHub, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions packages/source-map-compat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Build-time source maps

The root npm override resolves every `source-map-js` dependency to this local
package instead of fetching the `source-map-js@1.2.2` registry tarball.
Run `npm ci` normally; no install script or post-install patch is needed.

Adapted from [githubnext/gh-aw-cao's source-map adapter](https://github.com/githubnext/gh-aw-cao/tree/main/dashboard/site/packages/source-map-compat)
under the included MIT license. The synchronous generator and consumer APIs
used by PostCSS delegate encoding, decoding, indexed maps, and position lookup
to `@jridgewell/gen-mapping` and `@jridgewell/trace-mapping`. Chained maps,
original-source diagnostics, and embedded source content remain enabled.

This is scoped to build-tool consumers, not the complete `source-map-js` API:
`SourceNode`, reverse lookups, and original-order iteration are not implemented.

Run `npx vitest run src/source-map-compat.test.ts` to check dependency resolution
and source-map behavior, including PostCSS integration.
173 changes: 173 additions & 0 deletions packages/source-map-compat/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
const { posix: path } = require("node:path");
const { URL } = require("node:url");
const {
GenMapping, addMapping, fromMap, setSourceContent, toDecodedMap, toEncodedMap,
} = require("@jridgewell/gen-mapping");
const {
AnyMap, eachMapping, originalPositionFor, sourceContentFor,
} = require("@jridgewell/trace-mapping");

/** @typedef {{ line: number, column: number }} Position */
/** @typedef {{ generated: Position, original?: Position | null, source?: string | null, name?: string | null }} Mapping */

/** @param {string | null | undefined} root @param {string} source */
function joinSource(root, source) {
if (!root || /^\w+:/.test(source) || source.startsWith("//")) return source;
if (/^\w+:\/\//.test(root)) return new URL(source, root.replace(/\/?$/, "/")).href;
if (path.isAbsolute(source)) return source;
return path.join(root, source);
}

/** @param {string | null | undefined} root @param {string} source */
function relativeSource(root, source) {
if (!root) return source;
if (/^\w+:\/\//.test(root)) {
const base = new URL(root.replace(/\/?$/, "/"));
const target = new URL(source, base);
if (base.protocol !== target.protocol || base.host !== target.host) return source;
return path.relative(base.pathname, target.pathname) + target.search + target.hash;
}
if (/^\w+:/.test(source) || path.isAbsolute(root) !== path.isAbsolute(source)) return source;
if (!path.isAbsolute(root) && path.normalize(root).split("/")[0] !== path.normalize(source).split("/")[0]) {
return source;
}
return path.relative(root, source);
}

class SourceMapConsumer {
/** @param {Parameters<typeof AnyMap>[0]} map */
constructor(map) {
this._map = AnyMap(map);
}

/** @param {SourceMapGenerator} generator */
static fromSourceMap(generator) {
return new SourceMapConsumer(generator.toJSON());
}

get file() { return this._map.file; }
set file(value) { this._map.file = value; }
get sourceRoot() { return this._map.sourceRoot; }
get sources() { return this._map.resolvedSources; }
get sourcesContent() { return this._map.sourcesContent; }
/** @param {(string | null)[] | null | undefined} value */
set sourcesContent(value) { this._map.sourcesContent = value ?? undefined; }

/** @param {import("@jridgewell/trace-mapping").Needle} position */
originalPositionFor(position) {
return originalPositionFor(this._map, position);
}

/** @param {(mapping: import("@jridgewell/trace-mapping").EachMapping) => void} callback @param {unknown} [context] */
eachMapping(callback, context) {
eachMapping(this._map, context === undefined ? callback : callback.bind(context));
}

/** @param {string} source @param {boolean} [nullOnMissing] */
sourceContentFor(source, nullOnMissing = false) {
if (!this._map.sources.includes(source) && !this.sources.includes(source)) {
if (nullOnMissing) return null;
throw new Error(`Source not found in source map: ${source}`);
}
return sourceContentFor(this._map, source);
}

hasContentsOfAllSources() {
const content = this.sourcesContent;
return content != null && this.sources.every((_, index) => content[index] != null);
}
}

class SourceMapGenerator {
/** @param {import("@jridgewell/gen-mapping").Options & { ignoreInvalidMapping?: boolean }} [options] */
constructor(options = {}) {
this._map = new GenMapping(options);
this._ignoreInvalidMapping = options.ignoreInvalidMapping === true;
}

get _file() { return this._map.file; }
set _file(value) { this._map.file = value; }

/** @param {SourceMapConsumer} consumer @param {{ ignoreInvalidMapping?: boolean }} [options] */
static fromSourceMap(consumer, options = {}) {
const generator = new SourceMapGenerator(options);
generator._map = fromMap(consumer._map);
return generator;
}

/** @param {Mapping} mapping */
addMapping(mapping) {
/** @param {Position | null | undefined} position */
const validPosition = (position) => position != null &&
Number.isInteger(position.line) && position.line > 0 &&
Number.isInteger(position.column) && position.column >= 0;
const valid = validPosition(mapping.generated) &&
(mapping.original == null
? mapping.source == null && mapping.name == null
: validPosition(mapping.original) && mapping.source != null);
if (!valid) {
if (this._ignoreInvalidMapping) return;
throw new Error("Invalid source map mapping");
}
if (mapping.original != null && mapping.source != null) {
const entry = { generated: mapping.generated, original: mapping.original, source: mapping.source };
if (mapping.name == null) addMapping(this._map, entry);
else addMapping(this._map, { ...entry, name: mapping.name });
} else {
addMapping(this._map, { generated: mapping.generated });
}
}

/** @param {string} source @param {string | null} content */
setSourceContent(source, content) {
setSourceContent(this._map, relativeSource(this._map.sourceRoot, source), content);
}

/** @param {SourceMapConsumer} consumer @param {string | null} [sourceFile] @param {string} [sourceMapPath] */
applySourceMap(consumer, sourceFile = consumer.file, sourceMapPath) {
if (sourceFile == null) throw new Error("A source file is required to apply a source map");
const current = toDecodedMap(this._map);
const previous = new SourceMapConsumer({ ...current, sourceRoot: undefined });
const target = relativeSource(current.sourceRoot, sourceFile);
const next = new SourceMapGenerator({
file: current.file,
sourceRoot: current.sourceRoot,
});
previous.eachMapping((mapping) => {
let source = mapping.source;
let line = mapping.originalLine;
let column = mapping.originalColumn;
let name = mapping.name;
let content = source == null ? null : previous.sourceContentFor(source);
if (source === target && line != null && column != null) {
const original = consumer.originalPositionFor({ line, column });
if (original.source != null) {
source = relativeSource(current.sourceRoot, joinSource(sourceMapPath, original.source));
line = original.line;
column = original.column;
name = original.name ?? name;
content = consumer.sourceContentFor(original.source);
}
}
const generated = { line: mapping.generatedLine, column: mapping.generatedColumn };
if (source != null && line != null && column != null) {
next.addMapping({ generated, source, original: { line, column }, name });
} else {
next.addMapping({ generated });
}
if (source != null && content != null) setSourceContent(next._map, source, content);
});
this._map = next._map;
}

toJSON() {
return toEncodedMap(this._map);
}

toString() {
return JSON.stringify(this.toJSON());
}
}

exports.SourceMapConsumer = SourceMapConsumer;
exports.SourceMapGenerator = SourceMapGenerator;
18 changes: 18 additions & 0 deletions packages/source-map-compat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@rig/source-map-compat",
"version": "1.0.0",
"private": true,
"description": "Synchronous source-map APIs used by PostCSS, without source-map-js.",
"license": "MIT",
"main": "index.cjs",
"exports": {
".": "./index.cjs",
"./lib/source-map-generator.js": "./index.cjs",
"./lib/source-map-consumer.js": "./index.cjs",
"./package.json": "./package.json"
},
"dependencies": {
"@jridgewell/gen-mapping": "0.3.13",
"@jridgewell/trace-mapping": "0.3.31"
}
}
Loading
Loading