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
21 changes: 21 additions & 0 deletions .changeset/eager-bats-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@lynx-js/react-webpack-plugin": patch
---

Shake `useImperativeHandle` on the main-thread by default.

```js
import { forwardRef, useImperativeHandle } from '@lynx-js/react';

export default forwardRef(function App(_, ref) {
useImperativeHandle(ref, () => {
// This should be considered as background only
return {
name() {
// This should be considered as background only
console.info('This should not exist in main-thread');
},
};
});
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
'useLayoutEffect',
'__runInJS',
'useLynxGlobalEventListener',
'useImperativeHandle',

Check warning on line 247 in packages/webpack/react-webpack-plugin/src/loaders/options.ts

View check run for this annotation

Codecov / codecov/patch

packages/webpack/react-webpack-plugin/src/loaders/options.ts#L247

Added line #L247 was not covered by tests
...(shake?.removeCallParams ?? []),
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference types="vitest/globals" />

import fs from 'node:fs/promises';

import './react.js';
import './lynx-js-react.js';

it('should have useEffect func in background thread script', async () => {
if (__LEPUS__) {
return;
}
const expected = eval(
`['This', 'should', 'not', 'exist', 'in', 'main-thread'].join(' ', )`,
);
const content = await fs.readFile(__filename, 'utf-8');

expect(content).toContain(expected);
});

it('should not have useEffect func in main thread script', async () => {
if (__JS__) {
return;
}
const expected = eval(
`['This', 'should', 'not', 'exist', 'in', 'main-thread'].join(' ', )`,
);

const content = await fs.readFile(
__filename,
'utf-8',
);

expect(content).not.toContain(expected);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { forwardRef, useImperativeHandle } from '@lynx-js/react';
import { useEffect } from '@lynx-js/react';
import { useEffect as useMyEffect } from '@lynx-js/react';

export default forwardRef(function App(_, ref) {
function bar() {
console.info('This should not exist in main-thread');
}
useImperativeHandle(ref, () => {
console.info('This should not exist in main-thread');
return {
name() {
console.info('This should not exist in main-thread');
},
bar,
baz: 'This should not exist in main-thread',
};
});
useEffect(() => {
console.info('This should not exist in main-thread');
}, []);

useMyEffect(() => {
// TODO: import alias is not removed
});

React.useEffect(() => {
// TODO: default import is not removed
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { forwardRef, useImperativeHandle } from 'react';
import { useEffect } from 'react';
import { useEffect as useMyEffect } from 'react';

export default forwardRef(function App(_, ref) {
function bar() {
console.info('This should not exist in main-thread');
}
useImperativeHandle(ref, () => {
console.info('This should not exist in main-thread');
return {
name() {
console.info('This should not exist in main-thread');
},
bar,
baz: 'This should not exist in main-thread',
};
});
useEffect(() => {
console.info('This should not exist in main-thread');
}, []);

useMyEffect(() => {
// TODO: import alias is not removed
});

React.useEffect(() => {
// TODO: default import is not removed
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createConfig } from '../../../create-react-config.js';

const defaultConfig = createConfig();

/** @type {import('@rspack/core').Configuration} */
export default {
context: __dirname,
...defaultConfig,
resolve: {
...defaultConfig.resolve,
alias: {
'react': '@lynx-js/react',
'@lynx-js/react-runtime': '@lynx-js/react',
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import("@lynx-js/test-tools").TConfigCaseConfig} */
module.exports = {
bundlePath: [
'main:main-thread.js',
'main:background.js',
],
};
Loading