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

Merge multi-source output sourcemaps #14246

Merged
merged 1 commit into from Feb 6, 2022

Conversation

jridgewell
Copy link
Member

@jridgewell jridgewell commented Feb 6, 2022

Q                       A
Fixed Issues? Fixes ampproject/remapping#159.
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Merge multi-source output sourcemaps

Surprisingly, Babel allows a transformer to mark the source file of
a node to allow it to be sourced from any file. When this happens, the
output sourcemap will contain multiple sources. I didn't realize this
when I created #14209, and this remapping will throw an error if the
output map has multiple sources.

This can be fixed by using remapping's graph building API (don't pass
an array). This allows us to return an input map for any source file,
and we just need some special handling to figure out which source is our
transformed file.

This actually adds a new feature, allowing us to remap these
multi-source outputs. Previously, the merging would silently fail and
generate a blank (no mappings) sourcemap. That's not great. The new
behavior will properly merge the maps, provided we can figure out which
source is the transformed file (which should always work, I can't think
of a case it wouldn't).

See the visualization of the test case.

Surprisingly, Babel allows a transformer to mark the source file of
a node to allow it to be sourced from any file. When this happens, the
output sourcemap will contain multiple `sources`. I didn't realize this
when I created babel#14209, and this `remapping` will throw an error if the
output map has multiple sources.

This can be fixed by using `remapping`'s graph building API (don't pass
an array). This allows us to return an input map for _any_ source file,
and we just need some special handling to figure out which source is our
transformed file.

This actually adds a new feature, allowing us to remap these
multi-source outputs. Previously, the merging would silently fail and
generate a blank (no `mappings`) sourcemap. That's not great. The new
behavior will properly merge the maps, provided we can figure out which
source is the transformed file (which should always work, I can't think
of a case it wouldn't).

Fixes ampproject/remapping#159.
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51177/

@nicolo-ribaudo
Copy link
Member

This might help with #14132 (cc @ForbiddenEra)

@nicolo-ribaudo nicolo-ribaudo added pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Feb 6, 2022
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit 1deccb0 into babel:main Feb 6, 2022
jridgewell added a commit to jridgewell/babel that referenced this pull request Feb 7, 2022
A follow-up to babel#14246. I finally thought of a case where the
sourceFileName's content is fully replaced, leading to only the injected
file existing in the output sourcemap. This can happen where only one
output source is created, or multiple.

In the single output source case, we'd incorrectly associate the
mappings through the inputMap, even though none of the content actually
comes from there.

In the multiple source case, we'd silently fail and output empty mappings.

Both cases are now fixed, with the correct remapping being done through
in all possible output cases now.
@jridgewell jridgewell deleted the multisource-output-sourcemap branch February 7, 2022 01:36
JLHwung pushed a commit that referenced this pull request Feb 8, 2022
…14247)

* Support merging sourcemaps when transformed file is fully replaced

A follow-up to #14246. I finally thought of a case where the
sourceFileName's content is fully replaced, leading to only the injected
file existing in the output sourcemap. This can happen where only one
output source is created, or multiple.

In the single output source case, we'd incorrectly associate the
mappings through the inputMap, even though none of the content actually
comes from there.

In the multiple source case, we'd silently fail and output empty mappings.

Both cases are now fixed, with the correct remapping being done through
in all possible output cases now.

* Update to remapping@2.1.0 to support source location override
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remapping when there's multiple sources
4 participants