From 219a738a752e44bcbf5681f2f77766f5581439c3 Mon Sep 17 00:00:00 2001 From: limerick Date: Sat, 15 Apr 2023 08:35:04 +0800 Subject: [PATCH] fix(es): Allow extra comments after `sourceMappingURL` (#7262) --- crates/swc/src/lib.rs | 10 ++++++- crates/swc/tests/source_map.rs | 5 ++++ .../srcmap/case-inline-extra-content/.swcrc | 3 +++ .../srcmap/case-inline-extra-content/index.js | 27 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 crates/swc/tests/srcmap/case-inline-extra-content/.swcrc create mode 100644 crates/swc/tests/srcmap/case-inline-extra-content/index.js diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index b297ed270e2..d0a70b8e768 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -309,7 +309,15 @@ impl Compiler { let read_sourcemap = || -> Result, Error> { let s = "sourceMappingURL="; let idx = fm.src.rfind(s); - let data_url = idx.map(|idx| &fm.src[idx + s.len()..]); + + let data_url = idx.map(|idx| { + let data_idx = idx + s.len(); + if let Some(end) = fm.src[data_idx..].find('\n').map(|i| i + data_idx + 1) { + &fm.src[data_idx..end] + } else { + &fm.src[data_idx..] + } + }); match read_inline_sourcemap(data_url) { Ok(r) => Ok(r), diff --git a/crates/swc/tests/source_map.rs b/crates/swc/tests/source_map.rs index 4770b24c34e..b275d64bfad 100644 --- a/crates/swc/tests/source_map.rs +++ b/crates/swc/tests/source_map.rs @@ -74,6 +74,11 @@ fn case_inline() { inline("tests/srcmap/case-inline/index.js"); } +#[test] +fn case_inline_extra_content() { + inline("tests/srcmap/case-inline-extra-content/index.js"); +} + #[test] fn issue_622() { file("tests/srcmap/issue-622/index.js", Default::default()).unwrap(); diff --git a/crates/swc/tests/srcmap/case-inline-extra-content/.swcrc b/crates/swc/tests/srcmap/case-inline-extra-content/.swcrc new file mode 100644 index 00000000000..8a045416438 --- /dev/null +++ b/crates/swc/tests/srcmap/case-inline-extra-content/.swcrc @@ -0,0 +1,3 @@ +{ + "inputSourceMap": true, +} \ No newline at end of file diff --git a/crates/swc/tests/srcmap/case-inline-extra-content/index.js b/crates/swc/tests/srcmap/case-inline-extra-content/index.js new file mode 100644 index 00000000000..69fff8bf17c --- /dev/null +++ b/crates/swc/tests/srcmap/case-inline-extra-content/index.js @@ -0,0 +1,27 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Foo = /** @class */ (function () { + function Foo() { + } + return Foo; +}()); +var Bar = /** @class */ (function (_super) { + __extends(Bar, _super); + function Bar() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Bar; +}(Foo)); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7O0FBQUE7SUFBQTtJQUVBLENBQUM7SUFBRCxVQUFDO0FBQUQsQ0FBQyxBQUZELElBRUM7QUFFRDtJQUFrQix1QkFBRztJQUFyQjs7SUFFQSxDQUFDO0lBQUQsVUFBQztBQUFELENBQUMsQUFGRCxDQUFrQixHQUFHLEdBRXBCIn0= +// extra content \ No newline at end of file