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

Switch to TraceMap for sourcemap's originalPositionFor API #1181

Merged
merged 3 commits into from Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 14 additions & 17 deletions lib/sourcemap.js
Expand Up @@ -44,12 +44,11 @@
"use strict";

import MOZ_SourceMap from "source-map";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We still use the SourceMapGenerator API to generate a new sourcemap for the minified output. TraceMap (and AnyMap) are only for consuming and tracing an already existing sourcemap.

import {
defaults,
} from "./utils/index.js";
import {AnyMap, originalPositionFor} from "@jridgewell/trace-mapping";
import {defaults} from "./utils/index.js";

// a small wrapper around fitzgen's source-map library
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
async function SourceMap(options) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is the only reason minify is async right now. I'm wondering if there should be a minifySync

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually I don't need to wonder much, it was the one thing people asked for when Terser 5 was released.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would you like me to go through and remove all asyncs?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not in this PR :)

Also if the minify function becomes sync, it would break people who are calling .then directly. It should be a new function

function SourceMap(options) {
options = defaults(options, {
file : null,
root : null,
Expand All @@ -66,18 +65,20 @@ async function SourceMap(options) {
});

if (options.orig) {
orig_map = await new MOZ_SourceMap.SourceMapConsumer(options.orig);
orig_map.sources.forEach(function(source) {
var sourceContent = orig_map.sourceContentFor(source, true);
if (sourceContent) {
generator.setSourceContent(source, sourceContent);
}
});
orig_map = new AnyMap(options.orig);
if (orig_map.sourcesContent) {
orig_map.resolvedSources.forEach(function(source, i) {
var sourceContent = orig_map.sourcesContent[i];
if (sourceContent) {
generator.setSourceContent(source, sourceContent);
}
});
}
}

function add(source, gen_line, gen_col, orig_line, orig_col, name) {
if (orig_map) {
var info = orig_map.originalPositionFor({
var info = originalPositionFor(orig_map, {
line: orig_line,
column: orig_col
});
Expand All @@ -101,11 +102,7 @@ async function SourceMap(options) {
add : add,
get : function() { return generator; },
toString : function() { return generator.toString(); },
destroy : function () {
if (orig_map && orig_map.destroy) {
orig_map.destroy();
}
}
destroy : function() {}
};
}

Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -43,6 +43,7 @@
"main.js"
],
"dependencies": {
"@jridgewell/trace-mapping": "0.3.5",
"acorn": "^8.5.0",
"commander": "^2.20.0",
"source-map": "~0.8.0-beta.0",
Expand Down
2 changes: 1 addition & 1 deletion test/input/issue-520/output.js

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