Skip to content

Commit 30a5de9

Browse files
committed
test: use correct options
1 parent 0b51243 commit 30a5de9

File tree

4 files changed

+116
-156
lines changed

4 files changed

+116
-156
lines changed

test/__snapshots__/index.ts.snap

Lines changed: 73 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,74 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`react-compiler-webpack (rspack) should optimize complex component 1`] = `
4-
"function _array_like_to_array(arr, len) {
5-
if (len == null || len > arr.length) len = arr.length;
6-
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
7-
return arr2;
8-
}
9-
function _array_with_holes(arr) {
10-
if (Array.isArray(arr)) return arr;
11-
}
12-
function _iterable_to_array_limit(arr, i) {
13-
var _i = arr == null ? null : typeof Symbol !== \\"undefined\\" && arr[Symbol.iterator] || arr[\\"@@iterator\\"];
14-
if (_i == null) return;
15-
var _arr = [];
16-
var _n = true;
17-
var _d = false;
18-
var _s, _e;
19-
try {
20-
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
21-
_arr.push(_s.value);
22-
if (i && _arr.length === i) break;
23-
}
24-
} catch (err) {
25-
_d = true;
26-
_e = err;
27-
} finally{
28-
try {
29-
if (!_n && _i[\\"return\\"] != null) _i[\\"return\\"]();
30-
} finally{
31-
if (_d) throw _e;
32-
}
33-
}
34-
return _arr;
35-
}
36-
function _non_iterable_rest() {
37-
throw new TypeError(\\"Invalid attempt to destructure non-iterable instance.\\\\\\\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\\");
38-
}
39-
function _sliced_to_array(arr, i) {
40-
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
41-
}
42-
function _unsupported_iterable_to_array(o, minLen) {
43-
if (!o) return;
44-
if (typeof o === \\"string\\") return _array_like_to_array(o, minLen);
45-
var n = Object.prototype.toString.call(o).slice(8, -1);
46-
if (n === \\"Object\\" && o.constructor) n = o.constructor.name;
47-
if (n === \\"Map\\" || n === \\"Set\\") return Array.from(n);
48-
if (n === \\"Arguments\\" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
49-
}
4+
"import { jsx as _jsx, jsxs as _jsxs } from \\"react/jsx-runtime\\";
505
import { c as _c } from \\"react/compiler-runtime\\";
51-
import { useState, useRef } from \\"react\\";
6+
import { useState, useRef } from 'react';
527
export default function Sukka() {
53-
var $ = _c(13);
54-
var _useState = _sliced_to_array(useState(0), 2), count = _useState[0], setCount = _useState[1];
55-
var singletonRef = useRef(null);
8+
const $ = _c(13);
9+
const [count, setCount] = useState(0);
10+
const singletonRef = useRef(null);
5611
if (!singletonRef.current) singletonRef.current = new AbortController();
57-
var t0;
12+
let t0;
5813
if ($[0] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
59-
t0 = function() {
60-
return setCount(function(count_0) {
61-
return count_0 + 1;
62-
});
63-
};
14+
t0 = ()=>setCount((count_0)=>count_0 + 1);
6415
$[0] = t0;
6516
} else t0 = $[0];
66-
var handleButtonClick = t0;
17+
const handleButtonClick = t0;
6718
if (count > 10) return null;
68-
var t1;
19+
let t1;
6920
if ($[1] !== count) {
70-
t1 = /*#__PURE__*/ React.createElement(\\"p\\", null, \\"Count: \\", count);
21+
t1 = /*#__PURE__*/ _jsxs(\\"p\\", {
22+
children: [
23+
\\"Count: \\",
24+
count
25+
]
26+
});
7127
$[1] = count;
7228
$[2] = t1;
7329
} else t1 = $[2];
74-
var t2;
30+
let t2;
7531
if ($[3] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
76-
t2 = /*#__PURE__*/ React.createElement(\\"button\\", {
77-
onClick: handleButtonClick
78-
}, \\"Increment\\");
32+
t2 = /*#__PURE__*/ _jsx(\\"button\\", {
33+
onClick: handleButtonClick,
34+
children: \\"Increment\\"
35+
});
7936
$[3] = t2;
8037
} else t2 = $[3];
81-
var t3;
38+
let t3;
8239
if ($[4] !== count) {
83-
t3 = count > 10 && /*#__PURE__*/ React.createElement(\\"p\\", null, \\"Count is higher than 10\\");
40+
t3 = count > 10 && /*#__PURE__*/ _jsx(\\"p\\", {
41+
children: \\"Count is higher than 10\\"
42+
});
8443
$[4] = count;
8544
$[5] = t3;
8645
} else t3 = $[5];
87-
var t4 = count > 0;
88-
var t5;
46+
const t4 = count > 0;
47+
let t5;
8948
if ($[6] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
90-
t5 = function() {
91-
return setCount(0);
92-
};
49+
t5 = ()=>setCount(0);
9350
$[6] = t5;
9451
} else t5 = $[6];
95-
var t6;
52+
let t6;
9653
if ($[7] !== t4) {
97-
t6 = /*#__PURE__*/ React.createElement(\\"button\\", {
54+
t6 = /*#__PURE__*/ _jsx(\\"button\\", {
9855
disabled: t4,
99-
onClick: t5
100-
}, \\"Reset Count\\");
56+
onClick: t5,
57+
children: \\"Reset Count\\"
58+
});
10159
$[7] = t4;
10260
$[8] = t6;
10361
} else t6 = $[8];
104-
var t7;
62+
let t7;
10563
if ($[9] !== t1 || $[10] !== t3 || $[11] !== t6) {
106-
t7 = /*#__PURE__*/ React.createElement(\\"div\\", null, t1, t2, t3, t6);
64+
t7 = /*#__PURE__*/ _jsxs(\\"div\\", {
65+
children: [
66+
t1,
67+
t2,
68+
t3,
69+
t6
70+
]
71+
});
10772
$[9] = t1;
10873
$[10] = t3;
10974
$[11] = t6;
@@ -115,10 +80,11 @@ export default function Sukka() {
11580
`;
11681

11782
exports[`react-compiler-webpack (rspack) should work 1`] = `
118-
"import { c as _c } from \\"react/compiler-runtime\\";
83+
"import { jsx as _jsx } from \\"react/jsx-runtime\\";
84+
import { c as _c } from \\"react/compiler-runtime\\";
11985
export default function Example() {
120-
var $ = _c(2);
121-
var t0;
86+
const $ = _c(2);
87+
let t0;
12288
if ($[0] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
12389
t0 = [
12490
1,
@@ -128,14 +94,14 @@ export default function Example() {
12894
];
12995
$[0] = t0;
13096
} else t0 = $[0];
131-
var value = t0;
132-
var t1;
97+
const value = t0;
98+
let t1;
13399
if ($[1] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
134-
t1 = /*#__PURE__*/ React.createElement(\\"div\\", null, value.map(function(i) {
135-
return /*#__PURE__*/ React.createElement(\\"p\\", {
136-
key: i
137-
}, i);
138-
}));
100+
t1 = /*#__PURE__*/ _jsx(\\"div\\", {
101+
children: value.map((i)=>/*#__PURE__*/ _jsx(\\"p\\", {
102+
children: i
103+
}, i))
104+
});
139105
$[1] = t1;
140106
} else t1 = $[1];
141107
return t1;
@@ -144,10 +110,11 @@ export default function Example() {
144110
`;
145111

146112
exports[`react-compiler-webpack (rspack) should work with tsx 1`] = `
147-
"import { c as _c } from \\"react/compiler-runtime\\";
113+
"import { jsx as _jsx } from \\"react/jsx-runtime\\";
114+
import { c as _c } from \\"react/compiler-runtime\\";
148115
export default function Example() {
149-
var $ = _c(2);
150-
var t0;
116+
const $ = _c(2);
117+
let t0;
151118
if ($[0] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
152119
t0 = [
153120
1,
@@ -157,21 +124,21 @@ export default function Example() {
157124
];
158125
$[0] = t0;
159126
} else t0 = $[0];
160-
var value = t0;
161-
var t1;
127+
const value = t0;
128+
let t1;
162129
if ($[1] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
163-
t1 = /*#__PURE__*/ React.createElement(\\"div\\", null, value.map(function(i) {
164-
return /*#__PURE__*/ React.createElement(\\"p\\", {
165-
key: i
166-
}, i);
167-
}));
130+
t1 = /*#__PURE__*/ _jsx(\\"div\\", {
131+
children: value.map((i)=>/*#__PURE__*/ _jsx(\\"p\\", {
132+
children: i
133+
}, i))
134+
});
168135
$[1] = t1;
169136
} else t1 = $[1];
170137
return t1;
171138
}
172-
export var AnotherExmaple = function() {
173-
var $ = _c(2);
174-
var t0;
139+
export const AnotherExmaple = ()=>{
140+
const $ = _c(2);
141+
let t0;
175142
if ($[0] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
176143
t0 = [
177144
1,
@@ -181,14 +148,14 @@ export var AnotherExmaple = function() {
181148
];
182149
$[0] = t0;
183150
} else t0 = $[0];
184-
var value = t0;
185-
var t1;
151+
const value = t0;
152+
let t1;
186153
if ($[1] === Symbol.for(\\"react.memo_cache_sentinel\\")) {
187-
t1 = /*#__PURE__*/ React.createElement(\\"div\\", null, value.map(function(i) {
188-
return /*#__PURE__*/ React.createElement(\\"p\\", {
189-
key: i
190-
}, i);
191-
}));
154+
t1 = /*#__PURE__*/ _jsx(\\"div\\", {
155+
children: value.map((i)=>/*#__PURE__*/ _jsx(\\"p\\", {
156+
children: i
157+
}, i))
158+
});
192159
$[1] = t1;
193160
} else t1 = $[1];
194161
return t1;

test/utils/get-rspack-compiler.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { externalModules } from './get-webpack-compiler';
99
import type { ReactCompilerLoaderOption } from '../../src';
1010

1111
import { reactCompilerLoader } from '../../dist';
12+
import { useSwcLoader } from './get-swc-loader';
1213

1314
export default (fixture: string, loaderOptions?: ReactCompilerLoaderOption, config: RspackOptions = {}) => {
1415
process.env.RSPACK_CONFIG_VALIDATE = 'loose';
@@ -30,34 +31,26 @@ export default (fixture: string, loaderOptions?: ReactCompilerLoaderOption, conf
3031
module: {
3132
rules: [
3233
{
33-
test: /\.jsx$/,
34-
use: {
35-
loader: 'builtin:swc-loader',
36-
options: {
37-
jsc: {
38-
parser: {
39-
syntax: 'ecmascript',
40-
jsx: true
41-
}
42-
}
34+
test: /\.[cm]?jsx$/i,
35+
exclude: /node_modules/,
36+
use: [
37+
useSwcLoader(false, 'builtin:swc-loader'),
38+
{
39+
loader: reactCompilerLoader,
40+
options: loaderOptions
4341
}
44-
},
45-
type: 'javascript/auto'
42+
]
4643
},
4744
{
48-
test: /\.tsx$/,
49-
use: {
50-
loader: 'builtin:swc-loader',
51-
options: {
52-
jsc: {
53-
parser: {
54-
syntax: 'typescript',
55-
tsx: true
56-
}
57-
}
45+
test: /\.[cm]?tsx$/i,
46+
exclude: /node_modules/,
47+
use: [
48+
useSwcLoader(true, 'builtin:swc-loader'),
49+
{
50+
loader: reactCompilerLoader,
51+
options: loaderOptions
5852
}
59-
},
60-
type: 'javascript/auto'
53+
]
6154
},
6255
{
6356
test: /\.[cm]?[jt]sx$/i,

test/utils/get-swc-loader.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Options as SwcOptions } from '@swc/core';
2+
3+
export const useSwcLoader = (isTSX: boolean, loader = 'swc-loader') => ({
4+
loader,
5+
options: {
6+
jsc: {
7+
parser: {
8+
syntax: isTSX ? 'typescript' : 'ecmascript',
9+
...(
10+
isTSX
11+
? { tsx: true }
12+
: { jsx: true }
13+
)
14+
},
15+
target: 'esnext',
16+
transform: {
17+
react: {
18+
runtime: 'automatic',
19+
refresh: false,
20+
development: false
21+
}
22+
}
23+
}
24+
} satisfies SwcOptions
25+
});

test/utils/get-webpack-compiler.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,14 @@ import type { ReactCompilerLoaderOption } from '../../src';
77

88
import pkgJson from '../../package.json';
99
import { builtinModules } from 'module';
10-
import type { Options as SwcOptions } from '@swc/core';
10+
import { useSwcLoader } from './get-swc-loader';
1111

1212
import { reactCompilerLoader } from '../../dist';
1313

1414
export const externalModules = Object.keys(pkgJson.dependencies)
1515
.concat(Object.keys(pkgJson.peerDependencies))
1616
.concat(builtinModules)
1717
.concat(['react', 'react/jsx-runtime', 'forgetti', 'forgetti/runtime', 'preact/hooks', 'preact/compat', 'preact']);
18-
19-
const useSwcLoader = (isTSX: boolean) => ({
20-
loader: 'swc-loader',
21-
options: {
22-
jsc: {
23-
parser: {
24-
syntax: isTSX ? 'typescript' : 'ecmascript',
25-
...(
26-
isTSX
27-
? { tsx: true }
28-
: { jsx: true }
29-
)
30-
},
31-
target: 'esnext',
32-
transform: {
33-
react: {
34-
runtime: 'automatic',
35-
refresh: false,
36-
development: false
37-
}
38-
}
39-
}
40-
} satisfies SwcOptions
41-
});
42-
4318
export default (fixture: string, loaderOptions?: ReactCompilerLoaderOption, config: webpack.Configuration = {}) => {
4419
const fullConfig: webpack.Configuration = {
4520
mode: 'development',

0 commit comments

Comments
 (0)