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.
Support merging sourcemaps when transformed file is fully replaced
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.
- Loading branch information
1 parent
1deccb0
commit 5031a53
Showing
14 changed files
with
169 additions
and
61 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
5 changes: 5 additions & 0 deletions
5
...sformation/source-maps/input-source-map-multiple-output-sources-complete-replace/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
...mation/source-maps/input-source-map-multiple-output-sources-complete-replace/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
...mation/source-maps/input-source-map-multiple-output-sources-complete-replace/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"] | ||
} |
2 changes: 2 additions & 0 deletions
2
...formation/source-maps/input-source-map-multiple-output-sources-complete-replace/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,2 @@ | ||
"bar"; | ||
"baz"; |
52 changes: 52 additions & 0 deletions
52
...formation/source-maps/input-source-map-multiple-output-sources-complete-replace/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,52 @@ | ||
module.exports = function (babel) { | ||
const { types: t } = babel; | ||
|
||
return { | ||
visitor: { | ||
Program(path) { | ||
const { file } = this; | ||
const { sourceFileName } = file.opts.generatorOpts; | ||
|
||
// This injects the sourcesContent, though I don't imagine anyone's | ||
// doing it. | ||
file.code = { | ||
[sourceFileName]: file.code, | ||
'bar.js': '<bar />', | ||
'baz.js': 'baz();', | ||
}; | ||
}, | ||
|
||
CallExpression(path) { | ||
const callee = path.node; | ||
const { loc } = callee; | ||
|
||
// This filename will cause a second source file to be generated in the | ||
// output sourcemap. | ||
loc.filename = "bar.js"; | ||
loc.start.column = 1; | ||
loc.end.column = 4; | ||
|
||
const node = t.stringLiteral('bar'); | ||
node.loc = loc; | ||
path.replaceWith(node); | ||
}, | ||
|
||
Function(path) { | ||
const callee = path.node; | ||
const { loc } = callee; | ||
|
||
// This filename will cause a second source file to be generated in the | ||
// output sourcemap. | ||
loc.filename = "baz.js"; | ||
loc.start.column = 0; | ||
loc.start.line = 1; | ||
loc.end.column = 3; | ||
loc.end.line = 1; | ||
|
||
const node = t.stringLiteral('baz'); | ||
node.loc = loc; | ||
path.replaceWith(node); | ||
}, | ||
}, | ||
}; | ||
}; |
13 changes: 13 additions & 0 deletions
13
...ion/source-maps/input-source-map-multiple-output-sources-complete-replace/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,13 @@ | ||
{ | ||
"mappings": "AAAC;ACAD,K", | ||
"names": [], | ||
"sources": [ | ||
"bar.js", | ||
"baz.js" | ||
], | ||
"sourcesContent": [ | ||
"<bar />", | ||
"baz();" | ||
], | ||
"version": 3 | ||
} |
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
...st/fixtures/transformation/source-maps/input-source-map-sources-complete-replace/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
...ixtures/transformation/source-maps/input-source-map-sources-complete-replace/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
...ixtures/transformation/source-maps/input-source-map-sources-complete-replace/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"] | ||
} |
1 change: 1 addition & 0 deletions
1
...t/fixtures/transformation/source-maps/input-source-map-sources-complete-replace/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 @@ | ||
"bar"; |
38 changes: 38 additions & 0 deletions
38
...t/fixtures/transformation/source-maps/input-source-map-sources-complete-replace/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,38 @@ | ||
module.exports = function (babel) { | ||
const { types: t } = babel; | ||
|
||
return { | ||
visitor: { | ||
Program(path) { | ||
const { file } = this; | ||
const { sourceFileName } = file.opts.generatorOpts; | ||
|
||
// This injects the sourcesContent, though I don't imagine anyone's | ||
// doing it. | ||
file.code = { | ||
[sourceFileName]: file.code, | ||
'test.js': '<bar />', | ||
}; | ||
}, | ||
|
||
CallExpression(path) { | ||
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); | ||
}, | ||
|
||
Function(path) { | ||
path.remove(); | ||
}, | ||
}, | ||
}; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...ures/transformation/source-maps/input-source-map-sources-complete-replace/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,7 @@ | ||
{ | ||
"mappings": "AAAC", | ||
"names": [], | ||
"sources": ["test.js"], | ||
"sourcesContent": ["<bar />"], | ||
"version": 3 | ||
} |