fix(common): Fix logic for excluding FileName from source maps (#7900)

**Description:**

This PR also modifies React transform to use `FileName::Internal` and makes `SourceMapGenConfig` skips `FileName::Internal`.

**Related issue:**

 - Closes #5272
This commit is contained in:
Donny/강동윤 2023-08-31 22:23:11 +09:00 committed by GitHub
parent e5f7a9dab0
commit aa6495519b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 133 additions and 10 deletions

View File

@ -681,10 +681,10 @@ impl SourceMapGenConfig for SwcSourceMapConfig<'_> {
} }
fn skip(&self, f: &FileName) -> bool { fn skip(&self, f: &FileName) -> bool {
if let FileName::Custom(s) = f { match f {
s.starts_with('<') FileName::Internal(..) => true,
} else { FileName::Custom(s) => s.starts_with('<'),
false _ => false,
} }
} }
} }

View File

@ -0,0 +1,20 @@
{
"sourceMaps": true,
"module": {
"type": "commonjs",
"strict": false,
"strictMode": false
},
"jsc": {
"target": "es5",
"parser": {
"syntax": "typescript",
"dynamicImport": true
},
"transform": {
"hidden": {
"jest": true
}
}
},
}

View File

@ -0,0 +1,9 @@
import { Foo } from "./a";
describe("a", () => {
it("does its thing", () => {
const a = new Foo();
expect(a.bar()).toBe(3);
expect(a.foo()).toBe(2);
});
});

View File

@ -0,0 +1,6 @@
import { Base } from "../b/base";
export class Foo extends Base {
bar() {
return 1 + this.foo();
}
}

View File

@ -0,0 +1,16 @@
{
"mappings": ";;;+BACaA;;;eAAAA;;;;;;;oBADQ;AACd,IAAA,AAAMA,oBAAN;;gBAAMA;iCAAAA;aAAAA;kCAAAA;;;oBAAAA;;YACTC,KAAAA;mBAAAA,SAAAA;gBACI,OAAO,IAAI,IAAI,CAACC,GAAG;YACvB;;;WAHSF;EAAYG,UAAI",
"names": [
"Foo",
"bar",
"foo",
"Base"
],
"sources": [
"../../input/source/a/a.ts"
],
"sourcesContent": [
"import { Base } from \"../b/base\";\nexport class Foo extends Base {\n bar() {\n return 1 + this.foo();\n }\n}"
],
"version": 3
}

View File

@ -0,0 +1,20 @@
{
"mappings": ";;;iBAAoB;AAEpBA,SAAS,KAAK;IACVC,GAAG,kBAAkB;QACjB,IAAMC,IAAI,IAAIC,MAAG;QACjBC,OAAOF,EAAEG,GAAG,IAAIC,IAAI,CAAC;QACrBF,OAAOF,EAAEK,GAAG,IAAID,IAAI,CAAC;IACzB;AACJ",
"names": [
"describe",
"it",
"a",
"Foo",
"expect",
"bar",
"toBe",
"foo"
],
"sources": [
"../../input/source/a/a.spec.ts"
],
"sourcesContent": [
"import { Foo } from \"./a\";\n\ndescribe(\"a\", () => {\n it(\"does its thing\", () => {\n const a = new Foo();\n expect(a.bar()).toBe(3);\n expect(a.foo()).toBe(2);\n });\n});"
],
"version": 3
}

View File

@ -0,0 +1,11 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
var _a = require("./a");
describe("a", function() {
it("does its thing", function() {
var a = new _a.Foo();
expect(a.bar()).toBe(3);
expect(a.foo()).toBe(2);
});
});

View File

@ -0,0 +1,32 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Foo", {
enumerable: true,
get: function() {
return Foo;
}
});
var _class_call_check = require("@swc/helpers/_/_class_call_check");
var _create_class = require("@swc/helpers/_/_create_class");
var _inherits = require("@swc/helpers/_/_inherits");
var _create_super = require("@swc/helpers/_/_create_super");
var _base = require("../b/base");
var Foo = /*#__PURE__*/ function(Base) {
"use strict";
_inherits._(Foo, Base);
var _super = _create_super._(Foo);
function Foo() {
_class_call_check._(this, Foo);
return _super.apply(this, arguments);
}
_create_class._(Foo, [
{
key: "bar",
value: function bar() {
return 1 + this.foo();
}
}
]);
return Foo;
}(_base.Base);

View File

@ -1266,6 +1266,9 @@ impl SourceMap {
Some(ref f) if f.start_pos <= pos && pos < f.end_pos => f, Some(ref f) if f.start_pos <= pos && pos < f.end_pos => f,
_ => { _ => {
f = self.lookup_source_file(pos); f = self.lookup_source_file(pos);
if config.skip(&f.name) {
continue;
}
src_id = builder.add_source(&config.file_name_to_source(&f.name)); src_id = builder.add_source(&config.file_name_to_source(&f.name));
inline_sources_content = config.inline_sources_content(&f.name); inline_sources_content = config.inline_sources_content(&f.name);
@ -1447,8 +1450,9 @@ pub trait SourceMapGenConfig {
true true
} }
fn skip(&self, _f: &FileName) -> bool { /// By default, we skip internal files.
false fn skip(&self, f: &FileName) -> bool {
matches!(f, FileName::Internal(..))
} }
} }

View File

@ -29,8 +29,10 @@ impl Config {
.into_iter() .into_iter()
.map(|(k, v)| { .map(|(k, v)| {
let parse = |s| { let parse = |s| {
let fm = cm let fm = cm.new_source_file(
.new_source_file(FileName::Custom(format!("<umd-config-{}.js>", s)), s); FileName::Internal(format!("<umd-config-{}.js>", s)),
s,
);
parse_file_as_expr( parse_file_as_expr(
&fm, &fm,

View File

@ -46,7 +46,10 @@ pub fn const_modules(
fn parse_option(cm: &SourceMap, name: &str, src: String) -> Arc<Expr> { fn parse_option(cm: &SourceMap, name: &str, src: String) -> Arc<Expr> {
static CACHE: Lazy<DashMap<String, Arc<Expr>, ARandomState>> = Lazy::new(DashMap::default); static CACHE: Lazy<DashMap<String, Arc<Expr>, ARandomState>> = Lazy::new(DashMap::default);
let fm = cm.new_source_file(FileName::Custom(format!("<const-module-{}.js>", name)), src); let fm = cm.new_source_file(
FileName::Internal(format!("<const-module-{}.js>", name)),
src,
);
if let Some(expr) = CACHE.get(&**fm.src) { if let Some(expr) = CACHE.get(&**fm.src) {
return expr.clone(); return expr.clone();
} }

View File

@ -118,7 +118,7 @@ pub fn parse_expr_for_jsx(
src: String, src: String,
top_level_mark: Mark, top_level_mark: Mark,
) -> Arc<Box<Expr>> { ) -> Arc<Box<Expr>> {
let fm = cm.new_source_file(FileName::Custom(format!("<jsx-config-{}.js>", name)), src); let fm = cm.new_source_file(FileName::Internal(format!("<jsx-config-{}.js>", name)), src);
parse_file_as_expr( parse_file_as_expr(
&fm, &fm,