Skip to content

Commit

Permalink
Switch to source-map compatible @jridgewell/source-map
Browse files Browse the repository at this point in the history
This preserves API compatibility, so that loading source-map in the browser is still supported.
  • Loading branch information
jridgewell committed May 1, 2022
1 parent 8f938df commit 970a8c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
24 changes: 13 additions & 11 deletions lib/sourcemap.js
Expand Up @@ -43,11 +43,10 @@

"use strict";

import {GenMapping, maybeAddMapping, toDecodedMap, toEncodedMap, setSourceContent} from "@jridgewell/gen-mapping";
import {AnyMap, originalPositionFor} from "@jridgewell/trace-mapping";
import {SourceMapConsumer, SourceMapGenerator} from "@jridgewell/source-map";
import {defaults, HOP} from "./utils/index.js";

// a small wrapper around source-map and @jridgewell/trace-mapping
// a small wrapper around source-map and @jridgewell/source-map
function SourceMap(options) {
options = defaults(options, {
file : null,
Expand All @@ -57,7 +56,7 @@ function SourceMap(options) {
});

var orig_map;
var generator = new GenMapping({
var generator = new SourceMapGenerator({
file : options.file,
sourceRoot : options.root
});
Expand All @@ -68,9 +67,9 @@ function SourceMap(options) {
sourcesContent[name] = files[name];
}
if (options.orig) {
orig_map = new AnyMap(options.orig);
orig_map = new SourceMapConsumer(options.orig);
if (orig_map.sourcesContent) {
orig_map.resolvedSources.forEach(function(source, i) {
orig_map.sources.forEach(function(source, i) {
var content = orig_map.sourcesContent[i];
if (content) {
sourcesContent[source] = content;
Expand All @@ -81,7 +80,7 @@ function SourceMap(options) {

function add(source, gen_line, gen_col, orig_line, orig_col, name) {
if (orig_map) {
var info = originalPositionFor(orig_map, {
var info = orig_map.originalPositionFor({
line: orig_line,
column: orig_col
});
Expand All @@ -93,13 +92,13 @@ function SourceMap(options) {
orig_col = info.column;
name = info.name || name;
}
maybeAddMapping(generator, {
generator.addMapping({
generated : { line: gen_line, column: gen_col },
original : { line: orig_line, column: orig_col },
source : source,
name : name
});
setSourceContent(generator, source, sourcesContent[source]);
generator.setSourceContent(source, sourcesContent[source]);
}

function clean(map) {
Expand All @@ -112,8 +111,11 @@ function SourceMap(options) {

return {
add : add,
getDecoded : function() { return clean(toDecodedMap(generator)); },
getEncoded : function() { return clean(toEncodedMap(generator)); },
getDecoded : function() {
if (!generator.toDecodedMap) return null;
return clean(generator.toDecodedMap());
},
getEncoded : function() { return clean(generator.toJSON()); },
};
}

Expand Down
32 changes: 15 additions & 17 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -43,8 +43,7 @@
"main.js"
],
"dependencies": {
"@jridgewell/gen-mapping": "^0.3.0",
"@jridgewell/trace-mapping": "^0.3.5",
"@jridgewell/source-map": "^0.3.1",
"acorn": "^8.5.0",
"commander": "^2.20.0",
"source-map-support": "~0.5.20"
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Expand Up @@ -5,7 +5,7 @@ export default () => {
file: "dist/bundle.min.js",
format: "umd",
globals: {
"source-map": "sourceMap",
"@jridgewell/source-map": "sourceMap",
},
name: "Terser",
sourcemap: false,
Expand Down

0 comments on commit 970a8c9

Please sign in to comment.