123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@jridgewell/trace-mapping')) :
- typeof define === 'function' && define.amd ? define(['@jridgewell/trace-mapping'], factory) :
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.remapping = factory(global.traceMapping));
- })(this, (function (traceMapping) { 'use strict';
-
- class OriginalSource {
- constructor(source, content) {
- this.source = source;
- this.content = content;
- }
-
- originalPositionFor(line, column, name) {
- return { column, line, name, source: this.source, content: this.content };
- }
- }
-
- let put;
-
- class FastStringArray {
- constructor() {
- this.indexes = Object.create(null);
- this.array = [];
- }
- }
- (() => {
- put = (strarr, key) => {
- const { array, indexes } = strarr;
-
- let index = indexes[key];
-
-
- if (index === undefined) {
- index = indexes[key] = array.length;
- array.push(key);
- }
- return index;
- };
- })();
- const INVALID_MAPPING = undefined;
- const SOURCELESS_MAPPING = null;
-
- let traceMappings;
-
- class SourceMapTree {
- constructor(map, sources) {
- this.map = map;
- this.sources = sources;
- }
-
- originalPositionFor(line, column, name) {
- const segment = traceMapping.traceSegment(this.map, line, column);
-
- if (segment == null)
- return INVALID_MAPPING;
-
-
- if (segment.length === 1)
- return SOURCELESS_MAPPING;
- const source = this.sources[segment[1]];
- return source.originalPositionFor(segment[2], segment[3], segment.length === 5 ? this.map.names[segment[4]] : name);
- }
- }
- (() => {
- traceMappings = (tree) => {
- const mappings = [];
- const names = new FastStringArray();
- const sources = new FastStringArray();
- const sourcesContent = [];
- const { sources: rootSources, map } = tree;
- const rootNames = map.names;
- const rootMappings = traceMapping.decodedMappings(map);
- let lastLineWithSegment = -1;
- for (let i = 0; i < rootMappings.length; i++) {
- const segments = rootMappings[i];
- const tracedSegments = [];
- let lastSourcesIndex = -1;
- let lastSourceLine = -1;
- let lastSourceColumn = -1;
- for (let j = 0; j < segments.length; j++) {
- const segment = segments[j];
- let traced = SOURCELESS_MAPPING;
-
-
- if (segment.length !== 1) {
- const source = rootSources[segment[1]];
- traced = source.originalPositionFor(segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
-
-
- if (traced === INVALID_MAPPING)
- continue;
- }
- const genCol = segment[0];
- if (traced === SOURCELESS_MAPPING) {
- if (lastSourcesIndex === -1) {
-
- continue;
- }
- lastSourcesIndex = lastSourceLine = lastSourceColumn = -1;
- tracedSegments.push([genCol]);
- continue;
- }
-
-
- const { column, line, name, content, source } = traced;
-
-
- const sourcesIndex = put(sources, source);
- sourcesContent[sourcesIndex] = content;
- if (lastSourcesIndex === sourcesIndex &&
- lastSourceLine === line &&
- lastSourceColumn === column) {
-
-
- continue;
- }
- lastLineWithSegment = i;
- lastSourcesIndex = sourcesIndex;
- lastSourceLine = line;
- lastSourceColumn = column;
-
-
-
-
- tracedSegments.push(name
- ? [genCol, sourcesIndex, line, column, put(names, name)]
- : [genCol, sourcesIndex, line, column]);
- }
- mappings.push(tracedSegments);
- }
- if (mappings.length > lastLineWithSegment + 1) {
- mappings.length = lastLineWithSegment + 1;
- }
- return traceMapping.presortedDecodedMap(Object.assign({}, tree.map, {
- mappings,
-
- sourceRoot: undefined,
- names: names.array,
- sources: sources.array,
- sourcesContent,
- }));
- };
- })();
- function asArray(value) {
- if (Array.isArray(value))
- return value;
- return [value];
- }
-
- function buildSourceMapTree(input, loader) {
- const maps = asArray(input).map((m) => new traceMapping.TraceMap(m, ''));
- const map = maps.pop();
- for (let i = 0; i < maps.length; i++) {
- if (maps[i].sources.length > 1) {
- throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
- 'Did you specify these with the most recent transformation maps first?');
- }
- }
- let tree = build(map, loader, '', 0);
- for (let i = maps.length - 1; i >= 0; i--) {
- tree = new SourceMapTree(maps[i], [tree]);
- }
- return tree;
- }
- function build(map, loader, importer, importerDepth) {
- const { resolvedSources, sourcesContent } = map;
- const depth = importerDepth + 1;
- const children = resolvedSources.map((sourceFile, i) => {
-
-
-
-
- const ctx = {
- importer,
- depth,
- source: sourceFile || '',
- content: undefined,
- };
-
-
- const sourceMap = loader(ctx.source, ctx);
- const { source, content } = ctx;
-
- if (!sourceMap) {
-
-
-
- const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
- return new OriginalSource(source, sourceContent);
- }
-
-
- return build(new traceMapping.TraceMap(sourceMap, source), loader, source, depth);
- });
- return new SourceMapTree(map, children);
- }
-
- class SourceMap {
- constructor(map, options) {
- this.version = 3;
- this.file = map.file;
- this.mappings = options.decodedMappings ? traceMapping.decodedMappings(map) : traceMapping.encodedMappings(map);
- this.names = map.names;
- this.sourceRoot = map.sourceRoot;
- this.sources = map.sources;
- if (!options.excludeContent && 'sourcesContent' in map) {
- this.sourcesContent = map.sourcesContent;
- }
- }
- toString() {
- return JSON.stringify(this);
- }
- }
-
- function remapping(input, loader, options) {
- const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
- const tree = buildSourceMapTree(input, loader);
- return new SourceMap(traceMappings(tree), opts);
- }
- return remapping;
- }));
|