forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 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.
- Loading branch information
1 parent
89e26a0
commit 205983a
Showing
8 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...est/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/input.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...fixtures/transformation/source-maps/input-source-map-multiple-output-sources/input.js.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
...fixtures/transformation/source-maps/input-source-map-multiple-output-sources/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"inputSourceMap": true, | ||
"plugins": ["./plugin.js"] | ||
} |
5 changes: 5 additions & 0 deletions
5
...st/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"bar"; | ||
|
||
function foo(bar) { | ||
throw new Error('Intentional.'); | ||
} |
32 changes: 32 additions & 0 deletions
32
...st/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module.exports = function (babel) { | ||
const { types: t } = babel; | ||
|
||
return { | ||
visitor: { | ||
CallExpression(path) { | ||
const { file } = this; | ||
const { sourceFileName } = file.opts.generatorOpts; | ||
const callee = path.node; | ||
const { loc } = callee; | ||
|
||
// This filename will cause a second source file to be generated in the | ||
// output sourcemap. | ||
loc.filename = "test.js"; | ||
loc.start.column = 1; | ||
loc.end.column = 4; | ||
|
||
const node = t.stringLiteral('bar'); | ||
node.loc = loc; | ||
path.replaceWith(node); | ||
|
||
// This injects the sourcesContent, though I don't imagine anyone's | ||
// doing it. | ||
file.code = { | ||
[sourceFileName]: file.code, | ||
'test.js': '<bar />', | ||
}; | ||
path.stop(); | ||
}, | ||
}, | ||
}; | ||
}; |
17 changes: 17 additions & 0 deletions
17
...tures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;AACpB,QAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH", | ||
"names": [ | ||
"foo", | ||
"bar", | ||
"Error" | ||
], | ||
"sources": [ | ||
"test.js", | ||
"input.tsx" | ||
], | ||
"sourcesContent": [ | ||
"<bar />", | ||
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}" | ||
], | ||
"version": 3 | ||
} |