Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-rstest-jsx-runtime-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@lynx-js/react-alias-rsbuild-plugin': patch
---

fix(rstest): add global fallback aliases for `@lynx-js/react/jsx-runtime` and `@lynx-js/react/jsx-dev-runtime`

`pluginReactAlias` only aliased these entries inside layer-specific rules (`issuerLayer: BACKGROUND/MAIN_THREAD`). In rstest mode there are no layers, so JSX transformed by the testing loader—which emits `import { jsx } from '@lynx-js/react/jsx-runtime'`—could not be resolved, causing a `Cannot find module '@lynx-js/react/jsx-runtime'` error. Added global (non-layer-specific) fallback aliases pointing to the background jsx-runtime.
7 changes: 7 additions & 0 deletions .changeset/fix-rstest-testing-loader-compat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@lynx-js/react-webpack-plugin': patch
---

fix(rstest): normalize partial `compat` options in the testing loader

The testing loader forwards `compat` directly to `transformReactLynxSync` without normalization. When `compat` is supplied as a partial object, the required `target` field is absent and the WASM transform throws `Error: Missing field 'target'`. Added the same normalization already present in `getCommonOptions` for the background/main-thread loaders: fills in `target: 'MIXED'` and all other required fields with sensible defaults.
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ describe('pluginReactLynx', () => {
"@lynx-js/react/debug$": false,
"@lynx-js/react/experimental/lazy/import$": "<ROOT>/packages/react/runtime/lazy/import.js",
"@lynx-js/react/internal$": "<ROOT>/packages/react/runtime/lib/internal.js",
"@lynx-js/react/jsx-dev-runtime": "<ROOT>/packages/react/runtime/jsx-dev-runtime/index.js",
"@lynx-js/react/jsx-runtime": "<ROOT>/packages/react/runtime/jsx-runtime/index.js",
"@lynx-js/react/legacy-react-runtime$": "<ROOT>/packages/react/runtime/lib/legacy-react-runtime/index.js",
"@lynx-js/react/runtime-components$": "<ROOT>/packages/react/components/lib/index.js",
"@lynx-js/react/worklet-runtime/bindings$": "<ROOT>/packages/react/runtime/lib/worklet-runtime/bindings/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/rspeedy/plugin-react-alias/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export function pluginReactAlias(options: Options): RsbuildPlugin {
'@lynx-js/react$',
reactLepus.background,
)
.set('@lynx-js/react/jsx-runtime', jsxRuntime.background)
.set('@lynx-js/react/jsx-dev-runtime', jsxDevRuntime.background)

if (reactCompat) {
chain
Expand Down
20 changes: 20 additions & 0 deletions packages/rspeedy/plugin-react-alias/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ describe('React - alias', () => {
),
)

expect(config.resolve.alias).toHaveProperty(
'@lynx-js/react/jsx-runtime',
expect.stringContaining(
'/packages/react/runtime/jsx-runtime/index.js'.replaceAll(
'/',
path.sep,
),
),
)

expect(config.resolve.alias).toHaveProperty(
'@lynx-js/react/jsx-dev-runtime',
expect.stringContaining(
'/packages/react/runtime/jsx-dev-runtime/index.js'.replaceAll(
'/',
path.sep,
),
),
)

expect(config.resolve.alias).toHaveProperty(
'@lynx-js/react/internal$',
expect.stringContaining(
Expand Down
18 changes: 17 additions & 1 deletion packages/webpack/react-webpack-plugin/src/loaders/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ function testingLoader(
this.resourcePath,
),
);
const normalizedCompat = typeof compat === 'object'
? {
target: 'MIXED' as const,
addComponentElement: compat.addComponentElement ?? false,
additionalComponentAttributes: compat.additionalComponentAttributes ?? [],
componentsPkg: compat.componentsPkg ?? ['@lynx-js/react-components'],
disableDeprecatedWarning: compat.disableDeprecatedWarning ?? false,
newRuntimePkg: compat.newRuntimePkg ?? '@lynx-js/react',
oldRuntimePkg: compat.oldRuntimePkg ?? ['@lynx-js/react-runtime'],
simplifyCtorLikeReactLynx2: compat.simplifyCtorLikeReactLynx2 ?? false,
...(typeof compat.removeComponentAttrRegex === 'string' && {
removeComponentAttrRegex: compat.removeComponentAttrRegex,
}),
darkMode: compat.darkMode ?? false,
}
: (compat ?? false);
const result = transformReactLynxSync(
content,
{
Expand All @@ -52,7 +68,7 @@ function testingLoader(
directiveDCE: false,
defineDCE,
shake,
compat,
compat: normalizedCompat,
engineVersion,
worklet: {
filename,
Expand Down
109 changes: 109 additions & 0 deletions packages/webpack/react-webpack-plugin/test/testing-loader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { describe, expect, it } from 'vitest';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* Invoke the testing loader synchronously by wrapping the callback pattern.
*/
function runTestingLoader(
content: string,
options: Record<string, unknown>,
): Promise<{ code: string; map?: string }> {
return new Promise((resolve, reject) => {
// Dynamically import the built loader
const loaderPath = path.resolve(
__dirname,
'../lib/loaders/testing.js',
);
import(loaderPath).then(
(
mod: {
default: (this: Record<string, unknown>, content: string) => void;
},
) => {
const loader = mod.default;

const ctx: Record<string, unknown> = {
getOptions: () => options,
resourcePath: path.resolve(__dirname, 'fixture.tsx'),
rootContext: __dirname,
sourceMap: false,
hot: false,
experiments: undefined,
emitError: (err: Error) => reject(err),
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
emitWarning: () => {},
callback: (
err: Error | null,
code?: string,
map?: string,
) => {
if (err) reject(err);
else resolve({ code: code!, map });
},
};

loader.call(ctx, content);
},
).catch(reject);
});
}

describe('testing loader', () => {
it('handles partial compat object (missing target) without throwing', async () => {
const jsxContent = `
import { View } from '@lynx-js/react';
export function App() {
return <view />;
}
`;

// Partial compat without required `target` field — this was the bug
const result = await runTestingLoader(jsxContent, {
compat: {
componentsPkg: ['@byted-lynx/react-components'],
newRuntimePkg: '@lynx-js/react',
oldRuntimePkg: ['@byted-lynx/react-runtime'],
// intentionally missing: target, addComponentElement, etc.
},
engineVersion: '3.2',
});

expect(result.code).toBeTruthy();
});

it('handles compat: false without throwing', async () => {
const jsxContent = `
export function App() {
return <view />;
}
`;

const result = await runTestingLoader(jsxContent, {
compat: false,
engineVersion: '3.2',
});

expect(result.code).toBeTruthy();
});

it('handles missing compat (defaults to false) without throwing', async () => {
const jsxContent = `
export function App() {
return <view />;
}
`;

const result = await runTestingLoader(jsxContent, {
engineVersion: '3.2',
});

expect(result.code).toBeTruthy();
});
});
Loading