Skip to content

Commit 6e6dc44

Browse files
magic-akarikdy1
authored andcommitted
refactor(es/react): Split jsx into automatic/classic (#10616)
1 parent 1976d8e commit 6e6dc44

File tree

61 files changed

+2244
-1658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2244
-1658
lines changed

.changeset/fast-hairs-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_common: major
3+
---
4+
5+
refactor(es/react): Split jsx into automatic/classic

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/jsdoc/tests/fixture.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,17 @@ impl Comments for SwcComments {
154154
leading.push(pure_comment);
155155
}
156156
}
157+
158+
fn for_each(&self, f: &mut dyn FnMut(&Comment)) {
159+
for entry in self.leading.iter() {
160+
for cmt in entry.value() {
161+
f(cmt);
162+
}
163+
}
164+
for entry in self.trailing.iter() {
165+
for cmt in entry.value() {
166+
f(cmt);
167+
}
168+
}
169+
}
157170
}

crates/swc/benches/oxc.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ fn full_group(c: &mut Criterion) {
6262
})),
6363
transform: Some(TransformConfig {
6464
react: swc_ecma_transforms::react::Options {
65-
runtime: Some(Runtime::Automatic),
66-
development: Some(react_dev),
65+
runtime: Runtime::Automatic(Default::default()),
66+
common: swc_ecma_transforms::react::CommonConfig {
67+
development: react_dev.into(),
68+
..Default::default()
69+
},
6770
..Default::default()
6871
},
6972
..Default::default()

crates/swc/src/config/mod.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use swc_ecma_transforms::{
5757
decorators, explicit_resource_management::explicit_resource_management,
5858
export_default_from, import_attributes,
5959
},
60-
react::{self, default_pragma, default_pragma_frag},
60+
react::{self},
6161
resolver,
6262
typescript::{self, TsImportExportAssignConfig},
6363
Assumptions,
@@ -785,30 +785,29 @@ impl Options {
785785
typescript::typescript(ts_config, unresolved_mark, top_level_mark),
786786
syntax.typescript() && !syntax.jsx(),
787787
),
788+
// [TODO]: Remove tsx
788789
Optional::new(
789-
typescript::tsx::<Option<&dyn Comments>>(
790-
cm.clone(),
791-
ts_config,
792-
typescript::TsxConfig {
793-
pragma: Some(
794-
transform
795-
.react
796-
.pragma
797-
.clone()
798-
.unwrap_or_else(default_pragma),
790+
{
791+
let (pragma, pragma_frag) = match transform.react.runtime {
792+
react::Runtime::Classic(ref config) => (
793+
Some(config.pragma.clone()),
794+
Some(config.pragma_frag.clone()),
799795
),
800-
pragma_frag: Some(
801-
transform
802-
.react
803-
.pragma_frag
804-
.clone()
805-
.unwrap_or_else(default_pragma_frag),
806-
),
807-
},
808-
comments.map(|v| v as _),
809-
unresolved_mark,
810-
top_level_mark,
811-
),
796+
_ => (None, None),
797+
};
798+
799+
typescript::tsx::<Option<&dyn Comments>>(
800+
cm.clone(),
801+
ts_config,
802+
typescript::TsxConfig {
803+
pragma,
804+
pragma_frag,
805+
},
806+
comments.map(|v| v as _),
807+
unresolved_mark,
808+
top_level_mark,
809+
)
810+
},
812811
syntax.typescript() && syntax.jsx(),
813812
),
814813
)
@@ -819,9 +818,9 @@ impl Options {
819818
custom_before_pass(&program),
820819
// handle jsx
821820
Optional::new(
822-
react::react::<&dyn Comments>(
821+
react::react(
823822
cm.clone(),
824-
comments.map(|v| v as _),
823+
comments.cloned(),
825824
transform.react,
826825
top_level_mark,
827826
unresolved_mark,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
var _require = require("foo/jsx-runtime"), _jsx = _require.jsx;
21
;
3-
_jsx("a", {});
2+
React.createElement("a", null);

crates/swc/tests/fixture/issues-4xxx/4006/1/output/index.map

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"mappings": ";AAAA,SAASA,IAAI,EAAEC,GAAG,EAAEC,UAAU,QAAQ,MAAM;AAE5C,OAAO,MAAMC,YAAYD;IAkBrBE,SAAS;QACL,OAAOJ,IAAI,CAAC,UAAU,EAAE,IAAI,CAACK,IAAI,CAAC,KAAK,CAAC;IAC5C;IAPA,aAAc;QACV,KAAK,IAbTA,uBAAAA,QAAAA,KAAAA;QAcI,IAAI,CAACA,IAAI,GAAG;IAChB;AAKJ;AAlBI,iBAHSF,KAGFG,UAASL,GAAG,CAAC;;;;IAIpB,CAAC;AAED,iBATSE,KASFI,cAAa;IAChBF,MAAM;QAAEG,MAAMC;IAAO;AACzB;AAWJC,eAAeC,MAAM,CAAC,OAAOR",
2+
"mappings": ";AAAA,SAASA,IAAI,EAAEC,GAAG,EAAEC,UAAU,QAAQ,MAAM;AAE5C,OAAO,MAAMC,YAAYD;IAkBrBE,SAAS;QACL,OAAOJ,IAAI,CAAC,UAAU,EAAE,IAAI,CAACK,IAAI,CAAC,KAAK,CAAC;IAC5C;IAPA,aAAc;QACV,KAAK,IAbTA,uBAAAA,QAAAA;QAcI,IAAI,CAACA,IAAI,GAAG;IAChB;AAKJ;AAlBI,iBAHSF,KAGFG,UAASL,GAAG,CAAC;;;;IAIpB,CAAC;AAED,iBATSE,KASFI,cAAa;IAChBF,MAAM;QAAEG,MAAMC;IAAO;AACzB;AAWJC,eAAeC,MAAM,CAAC,OAAOR",
33
"names": [
44
"html",
55
"css",

crates/swc/tests/tsc-references/intraExpressionInferencesJsx.1.normal.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//// [intraExpressionInferencesJsx.tsx]
22
/// <reference path="/.lib/react16.d.ts" />
33
// repro from #52798
4+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
45
var Component = function(param) {
56
var animations = param.animations, style = param.style;
6-
return /*#__PURE__*/ React.createElement(React.Fragment, null);
7+
return /*#__PURE__*/ _jsx(_Fragment, {});
78
};
8-
/*#__PURE__*/ React.createElement(Component, {
9+
/*#__PURE__*/ _jsx(Component, {
910
animations: {
1011
test: {
1112
kind: "a",
@@ -17,7 +18,7 @@ var Component = function(param) {
1718
return "";
1819
}
1920
});
20-
/*#__PURE__*/ React.createElement(Component, {
21+
/*#__PURE__*/ _jsx(Component, {
2122
animations: {
2223
test: {
2324
kind: "a",
@@ -34,7 +35,7 @@ var Component = function(param) {
3435
return "";
3536
}
3637
});
37-
/*#__PURE__*/ React.createElement(Component, {
38+
/*#__PURE__*/ _jsx(Component, {
3839
animations: {
3940
test: {
4041
kind: "a",
@@ -52,25 +53,25 @@ var Component = function(param) {
5253
}
5354
});
5455
function Foo(props) {
55-
return /*#__PURE__*/ React.createElement("div", null);
56+
return /*#__PURE__*/ _jsx("div", {});
5657
}
57-
/*#__PURE__*/ React.createElement(Foo, {
58+
/*#__PURE__*/ _jsx(Foo, {
5859
a: function() {
5960
return 10;
6061
},
6162
b: function(arg) {
6263
arg.toString();
6364
}
6465
});
65-
/*#__PURE__*/ React.createElement(Foo, {
66+
/*#__PURE__*/ _jsx(Foo, {
6667
a: function(x) {
6768
return 10;
6869
},
6970
b: function(arg) {
7071
arg.toString();
7172
}
7273
});
73-
/*#__PURE__*/ React.createElement(Foo, {
74+
/*#__PURE__*/ _jsx(Foo, {
7475
a: function(x) {
7576
return 10;
7677
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
//// [intraExpressionInferencesJsx.tsx]
2+
import "react/jsx-runtime";

crates/swc/tests/tsc-references/jsxJsxsCjsTransformChildren.1.normal.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44
Object.defineProperty(exports, "__esModule", {
55
value: true
66
});
7-
var a = /*#__PURE__*/ React.createElement("div", null, "text");
7+
var _jsxdevruntime = require("react/jsx-dev-runtime");
8+
var a = /*#__PURE__*/ (0, _jsxdevruntime.jsxDEV)("div", {
9+
children: "text"
10+
}, void 0, false, {
11+
fileName: "jsxJsxsCjsTransformChildren.tsx",
12+
lineNumber: 2,
13+
columnNumber: 11
14+
}, void 0);

0 commit comments

Comments
 (0)