Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add applySourceMap #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion package-lock.json

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

110 changes: 108 additions & 2 deletions src/gen-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SetArray, put } from '@jridgewell/set-array';
import { SetArray, put, get } from '@jridgewell/set-array';
import { encode } from '@jridgewell/sourcemap-codec';
import { TraceMap, decodedMappings } from '@jridgewell/trace-mapping';
import { TraceMap, decodedMappings, traceSegment } from '@jridgewell/trace-mapping';

import {
COLUMN,
Expand All @@ -10,6 +10,8 @@ import {
NAMES_INDEX,
} from './sourcemap-segment';

import { join, relative } from './util';

import type { SourceMapInput } from '@jridgewell/trace-mapping';
import type { SourceMapSegment } from './sourcemap-segment';
import type { DecodedSourceMap, EncodedSourceMap, Pos, Mapping } from './types';
Expand Down Expand Up @@ -139,6 +141,18 @@ export let fromMap: (input: SourceMapInput) => GenMapping;
*/
export let allMappings: (map: GenMapping) => Mapping[];

/**
* Applies the mappings of a sub-source-map for a specific source file to the
* source map being generated. Each mapping to the supplied source file is
* rewritten using the supplied source map.
*/
export let applySourceMap: (
map: GenMapping,
sourceMapConsumer: TraceMap,
sourceFile?: string,
sourceMapPath?: string,
) => void;

// This split declaration is only so that terser can elminiate the static initialization block.
let addSegmentInternal: <S extends string | null | undefined>(
skipable: boolean,
Expand Down Expand Up @@ -336,6 +350,98 @@ export class GenMapping {
: [genColumn, sourcesIndex, sourceLine, sourceColumn],
);
};

applySourceMap = (
map: GenMapping,
consumer: TraceMap,
rawSourceFile?: string,
sourceMapPath?: string,
) => {
let sourceFile = rawSourceFile;

if (sourceFile == null) {
if (consumer.file == null) {
throw new Error(
'applySourceMap requires either an explicit source file, ' +
'or the source map\'s "file" property. Both were omitted.',
);
}
sourceFile = consumer.file;
}

const sourceRoot = map.sourceRoot;

// Make "sourceFile" relative if an absolute Url is passed.
if (sourceRoot != null) {
sourceFile = relative(sourceRoot, sourceFile);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the expectation is that the sourceFile is always fully resolved, and we need to make it back into an unresolved path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the new map needs to be resolved relative to the existing sourcemap.
Removing this block will break all the tests with relative paths.
I've also checked locally with my pending PR to postcss and that's the same behaviour as source-map-js


const sourceIndex = get(map._sources, sourceFile);

// If the applied source map replaces a source entirely
// it's better to start with a fresh set of source, sourceContent and names
const newSources = new SetArray();
const newSourceContents: (string | null)[] = [];
const newNames = new SetArray();

for (const line of map._mappings) {
for (const seg of line) {
if (seg.length === 1) continue;

if (seg[1] !== sourceIndex) {
// This isn't the file we want to remap; keep the original mapping
const initialIndex = seg[1];
seg[1] = put(newSources, map._sources.array[initialIndex]);
newSourceContents[seg[1]] = map._sourcesContent[initialIndex];
if (seg.length === 5) {
seg[4] = put(newNames, map._names.array[seg[4]]);
}
continue;
}

const traced = traceSegment(consumer, seg[2], seg[3]);
if (traced == null) {
// Could not find a mapping; keep the original mapping
const initialIndex = seg[1];
seg[1] = put(newSources, map._sources.array[initialIndex]);
newSourceContents[seg[1]] = map._sourcesContent[initialIndex];
if (seg.length === 5) {
seg[4] = put(newNames, map._names.array[seg[4]]);
}
continue;
}

let source = consumer.sources[traced[1] as number] as string;
if (sourceMapPath != null) {
source = join(sourceMapPath, source);
}
if (sourceRoot != null) {
source = relative(sourceRoot, source);
}

const newSourceIndex = put(newSources, source);
newSourceContents[newSourceIndex] = consumer.sourcesContent
? consumer.sourcesContent[traced[1] as number]
: null;

seg[1] = newSourceIndex;
seg[2] = traced[2] as number;
seg[3] = traced[3] as number;

if (traced.length === 5) {
// Add the name mapping if found
seg[4] = put(newNames, consumer.names[traced[4]]);
} else if (seg.length == 5) {
// restore the previous name mapping if found
seg[4] = put(newNames, map._names.array[seg[4]]);
}
}
}

map._sources = newSources;
map._sourcesContent = newSourceContents;
map._names = newNames;
};
}
}

Expand Down
217 changes: 217 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
const urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.-]*)(?::(\d+))?(.*)$/;
const dataUrlRegexp = /^data:.+,.+$/;

interface ParsedURL {
scheme: string;
auth: string;
host: string;
port: string;
path: string;
}

export function urlParse(aUrl: string): ParsedURL | null {
const match = aUrl.match(urlRegexp);
if (!match) {
return null;
}
return {
scheme: match[1],
auth: match[2],
host: match[3],
port: match[4],
path: match[5],
};
}

export function urlGenerate(aParsedUrl: ParsedURL) {
let url = '';
if (aParsedUrl.scheme) {
url += aParsedUrl.scheme + ':';
}
url += '//';
if (aParsedUrl.auth) {
url += aParsedUrl.auth + '@';
}
if (aParsedUrl.host) {
url += aParsedUrl.host;
}
if (aParsedUrl.port) {
url += ':' + aParsedUrl.port;
}
if (aParsedUrl.path) {
url += aParsedUrl.path;
}
return url;
}

export function isAbsolute(aPath: string) {
return aPath.charAt(0) === '/' || urlRegexp.test(aPath);
}

/**
* Normalizes a path, or the path portion of a URL:
*
* - Replaces consecutive slashes with one slash.
* - Removes unnecessary '.' parts.
* - Removes unnecessary '<dir>/..' parts.
*
* Based on code in the Node.js 'path' core module.
*
* @param aPath The path or url to normalize.
*/
export function normalize(aPath: string) {
let path = aPath;
const url = urlParse(aPath);
if (url) {
if (!url.path) {
return aPath;
}
path = url.path;
}
const isAbsoluteBool = isAbsolute(path);
// Split the path into parts between `/` characters. This is much faster than
// using `.split(/\/+/g)`.
const parts = [];
let start = 0;
let i = 0;
while (true) {
start = i;
i = path.indexOf('/', start);
if (i === -1) {
parts.push(path.slice(start));
break;
} else {
parts.push(path.slice(start, i));
while (i < path.length && path[i] === '/') {
i++;
}
}
}

for (let part, up = 0, i = parts.length - 1; i >= 0; i--) {
part = parts[i];
if (part === '.') {
parts.splice(i, 1);
} else if (part === '..') {
up++;
} else if (up > 0) {
if (part === '') {
// The first part is blank if the path is absolute. Trying to go
// above the root is a no-op. Therefore we can remove all '..' parts
// directly after the root.
parts.splice(i + 1, up);
up = 0;
} else {
parts.splice(i, 2);
up--;
}
}
}
path = parts.join('/');

if (path === '') {
path = isAbsoluteBool ? '/' : '.';
}

if (url) {
url.path = path;
return urlGenerate(url);
}
return path;
}

/**
* Joins two paths/URLs.
*
* @param aRoot The root path or URL.
* @param aPath The path or URL to be joined with the root.
*
* - If aPath is a URL or a data URI, aPath is returned, unless aPath is a
* scheme-relative URL: Then the scheme of aRoot, if any, is prepended
* first.
* - Otherwise aPath is a path. If aRoot is a URL, then its path portion
* is updated with the result and aRoot is returned. Otherwise the result
* is returned.
* - If aPath is absolute, the result is aPath.
* - Otherwise the two paths are joined with a slash.
* - Joining for example 'http://' and 'www.example.com' is also supported.
*/
export function join(aRoot: string, aPath: string) {
if (aRoot === '') {
aRoot = '.';
}
if (aPath === '') {
aPath = '.';
}
const aPathUrl = urlParse(aPath);
const aRootUrl = urlParse(aRoot);
if (aRootUrl) {
aRoot = aRootUrl.path || '/';
}

// `join(foo, '//www.example.org')`
if (aPathUrl && !aPathUrl.scheme) {
if (aRootUrl) {
aPathUrl.scheme = aRootUrl.scheme;
}
return urlGenerate(aPathUrl);
}

if (aPathUrl || aPath.match(dataUrlRegexp)) {
return aPath;
}

// `join('http://', 'www.example.com')`
if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
aRootUrl.host = aPath;
return urlGenerate(aRootUrl);
}

const joined =
aPath.charAt(0) === '/' ? aPath : normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);

if (aRootUrl) {
aRootUrl.path = joined;
return urlGenerate(aRootUrl);
}
return joined;
}

/**
* Make a path relative to a URL or another path.
*
* @param aRoot The root path or URL.
* @param aPath The path or URL to be made relative to aRoot.
*/
export function relative(aRoot: string, aPath: string) {
if (aRoot === '') {
aRoot = '.';
}

aRoot = aRoot.replace(/\/$/, '');

// It is possible for the path to be above the root. In this case, simply
// checking whether the root is a prefix of the path won't work. Instead, we
// need to remove components from the root one by one, until either we find
// a prefix that fits, or we run out of components to remove.
let level = 0;
while (aPath.indexOf(aRoot + '/') !== 0) {
const index = aRoot.lastIndexOf('/');
if (index < 0) {
return aPath;
}

// If the only part of the root that is left is the scheme (i.e. http://,
// file:///, etc.), one or more slashes (/), or simply nothing at all, we
// have exhausted all components, so the path is not relative to the root.
aRoot = aRoot.slice(0, index);
if (aRoot.match(/^([^/]+:\/)?\/*$/)) {
return aPath;
}

++level;
}

// Make sure we add a "../" for each component we removed from the root.
return Array(level + 1).join('../') + aPath.substr(aRoot.length + 1);
}
Loading