Skip to content

Commit 9f00bcc

Browse files
committed
feat: use JSX factory approach
1 parent a6a203b commit 9f00bcc

12 files changed

Lines changed: 109 additions & 43 deletions

File tree

apps/playground/rn-harness.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ export default {
6969
resetEnvironmentBetweenTestFiles: true,
7070
unstable__skipAlreadyIncludedModules: false,
7171
forwardClientLogs: true,
72+
disableViewFlattening: true,
7273
};

apps/playground/src/__tests__/ui/out-of-bounds.harness.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('Out of bounds', () => {
88
test('should screenshot specific element only', async () => {
99
await render(
1010
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
11-
<View collapsable={false} testID="out-of-bounds" style={{ width: 1000, height: 100, backgroundColor: 'yellow', flexDirection: 'row' }}>
11+
<View testID="out-of-bounds" style={{ width: 1000, height: 100, backgroundColor: 'yellow', flexDirection: 'row' }}>
1212
{Array.from({ length: 10 }).map((_, index) => (
1313
<View key={index} style={{ width: 100, height: 100, backgroundColor: COLORS[index % COLORS.length] }} />
1414
))}

packages/babel-preset/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"babel-plugin-istanbul": "^7.0.1"
2020
},
2121
"peerDependencies": {
22-
"@babel/core": "^7.22.0"
22+
"@babel/core": "^7.22.0",
23+
"@babel/plugin-transform-react-jsx": "*"
2324
},
2425
"devDependencies": {
2526
"@babel/core": "^7.22.0",

packages/babel-preset/src/collapsible-plugin.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/babel-preset/src/preset.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import resolveWeakPlugin from './resolve-weak-plugin';
2-
import collapsiblePlugin from './collapsible-plugin';
32
import path from 'path';
43

54
const getIstanbulPlugin = (): string | [string, object] | null => {
@@ -21,8 +20,14 @@ const getIstanbulPlugin = (): string | [string, object] | null => {
2120
export const rnHarnessPlugins = [
2221
'@babel/plugin-transform-class-static-block',
2322
resolveWeakPlugin,
24-
process.env.RN_HARNESS_VIEW_FLATTENING === 'false' ? collapsiblePlugin : null,
2523
getIstanbulPlugin(),
24+
[
25+
'@babel/plugin-transform-react-jsx',
26+
{
27+
runtime: 'automatic',
28+
importSource: '@react-native-harness/runtime',
29+
},
30+
],
2631
].filter((plugin) => plugin !== null);
2732

2833
export const rnHarnessPreset = () => {

packages/metro/src/manifest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Config as HarnessConfig } from '@react-native-harness/config';
55
const getManifestContent = (harnessConfig: HarnessConfig): string => {
66
return `global.RN_HARNESS = {
77
appRegistryComponentName: '${harnessConfig.appRegistryComponentName}',
8-
webSocketPort: ${harnessConfig.webSocketPort}
8+
webSocketPort: ${harnessConfig.webSocketPort},
9+
disableViewFlattening: ${harnessConfig.disableViewFlattening},
910
};`;
1011
};
1112

packages/metro/src/resolver.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@ export const getHarnessResolver = (
1414
paths: [process.cwd()],
1515
});
1616

17+
const resolvedJsxRuntimePath = require.resolve('@react-native-harness/runtime/jsx-runtime');
18+
const resolvedJsxDevRuntimePath = require.resolve('@react-native-harness/runtime/jsx-dev-runtime');
19+
1720
return (context, moduleName, platform) => {
21+
if (moduleName === '@react-native-harness/runtime/jsx-runtime') {
22+
return {
23+
type: 'sourceFile',
24+
filePath: resolvedJsxRuntimePath,
25+
};
26+
}
27+
28+
if (moduleName === '@react-native-harness/runtime/jsx-dev-runtime') {
29+
return {
30+
type: 'sourceFile',
31+
filePath: resolvedJsxDevRuntimePath,
32+
};
33+
}
34+
1835
const existingResolver =
1936
metroConfig.resolver?.resolveRequest ?? context.resolveRequest;
2037
const resolvedModule = existingResolver(context, moduleName, platform);

packages/runtime/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
"types": "./dist/entry-point.d.ts",
2424
"import": "./dist/entry-point.js",
2525
"default": "./dist/entry-point.js"
26+
},
27+
"./jsx-runtime": {
28+
"development": "./src/jsx/jsx-runtime.ts",
29+
"import": "./dist/jsx/jsx-runtime.js",
30+
"default": "./dist/jsx/jsx-runtime.js"
31+
},
32+
"./jsx-dev-runtime": {
33+
"development": "./src/jsx/jsx-dev-runtime.ts",
34+
"import": "./dist/jsx/jsx-dev-runtime.js",
35+
"default": "./dist/jsx/jsx-dev-runtime.js"
2636
}
2737
},
2838
"peerDependencies": {

packages/runtime/src/globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ImageSnapshotOptions } from '@react-native-harness/bridge';
33
export type HarnessGlobal = {
44
appRegistryComponentName: string;
55
webSocketPort?: number;
6+
disableViewFlattening?: boolean;
67
};
78

89
declare global {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as ReactJSXRuntimeDev from 'react/jsx-dev-runtime';
2+
3+
export const Fragment = ReactJSXRuntimeDev.Fragment;
4+
5+
export function jsxDEV(
6+
type: any,
7+
props: any,
8+
key: any,
9+
isStaticChildren: any,
10+
source: any,
11+
self: any
12+
) {
13+
if (
14+
type &&
15+
(type.displayName === 'View' || type.name === 'View') &&
16+
props &&
17+
props.collapsable === undefined
18+
) {
19+
props = { ...props, collapsable: true };
20+
}
21+
return ReactJSXRuntimeDev.jsxDEV(
22+
type,
23+
props,
24+
key,
25+
isStaticChildren,
26+
source,
27+
self
28+
);
29+
}

0 commit comments

Comments
 (0)