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

Support source maps #3

Open
TomiBelan opened this issue Dec 8, 2012 · 4 comments
Open

Support source maps #3

TomiBelan opened this issue Dec 8, 2012 · 4 comments

Comments

@TomiBelan
Copy link
Contributor

Cool project! I really like how readable the generated code is (especially compared to Streamline, Tame etc). It'd be nice if it also supported source maps. Those are files that map locations in the source to locations in the generated code to make debugging easier.

Escodegen already supports source maps, so most of the hard work should hopefully be already done. But when the parser converts the AST nodes using syntax.factory, their "loc" property is lost. They need to keep it for source maps to work. (As for new nodes that weren't in the original AST, I guess their "loc" should just be null.)

Perhaps that's the only required change (well, plus adding the CLI option itself), but I'm not sure.

@BYVoid
Copy link
Owner

BYVoid commented Dec 9, 2012

Thank you for your solution. It is scheduled to support source maps.

@BYVoid
Copy link
Owner

BYVoid commented Dec 9, 2012

@TomiBelan Source map is supported experimentally. You could have a try using command below:

continuation script.js -o compiled.js

A file named compiled.js.map will be generated and //@ sourceMappingURL=compiled.js.map will be appended to compiled.js

@TomiBelan
Copy link
Contributor Author

That was fast! Looks like it's working. I had to deal with some Chrome/Webkit issues though... (For instance, console logs that happen during page load still show the compiled location. I thought it's a continuation.js issue, but when I put it in an onclick handler it started working...)

It'd be nice if the implementation didn't use a global variable. I think it'd be better API design to return both the code and the source map from compile() (perhaps only when options.sourceMap is true, if you care about API compatibility at this stage).

It's a pity that the "Generated by..." mark had to be moved to the bottom. I'm looking into whether escodegen can be convinced to put it on top.

@TomiBelan
Copy link
Contributor Author

As for avoiding the global variable and returning both code and source map: for instance, escodegen has an undocumented option "sourceMapWithCode" which makes the return value be { code: "the generated code", map: "the map" }. It might make sense to adopt the same output format, and perhaps even use that option directly (though that depends on what extra stuff continuation.js is doing with the source map).

Escodegen (and source-map) doesn't seem to support line offsets or comments on top, unfortunately. The toStringWithSourceMap() function always counts from line 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants