Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Tree shaking not working #6702

Closed
nguyenbatranvan opened this issue Jun 2, 2024 · 9 comments
Closed

[Bug]: Tree shaking not working #6702

nguyenbatranvan opened this issue Jun 2, 2024 · 9 comments
Assignees
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@nguyenbatranvan
Copy link

nguyenbatranvan commented Jun 2, 2024

System Info

MAC OS - M1

Details

I have files:

// test1.ts
export const Test1=()=>{
    console.log('sdh')
}

// test2.ts
import dayjs from "dayjs";

export const Test2=()=>{
    console.log(dayjs())
}

// index.ts
export {Test1} from "./test1";
export {Test2} from "./test2";

In App.tsx i used:

import {Test1} from "./utils";
import {useEffect} from "react";


const App = () => {
    useEffect(() => {
        Test1();
    }, []);
    return (
        <div className="content">
            <h1>Rsbuild with React</h1>
        </div>
    );
};

export default App;
  • obviously I'm not using Test2 with dayjs, but when building, it still appears and is not tree-shaking
  • I'm currently using rsbuild, but I don't see any documentation about treeshaking. While going through rspack I found this, despite following it, it still doesn't work.

In rsbuild.config.ts

tools: {
                rspack: {
                    module:{
                        rules:[{
                            test: [/\/index.ts/i], // or /index\.ts/ => all not working
                            sideEffects: false
                        }]
                    },
                   ...
            }

What configuration am I missing? Thanks for help

Reproduce link

No response

Reproduce Steps

  1. No
@nguyenbatranvan nguyenbatranvan added bug Something isn't working pending triage The issue/PR is currently untouched. labels Jun 2, 2024
@mrakobes27
Copy link

Hi! add the following settings to the config:
{ optimization: { usedExports: true, providedExports: true, } }
for enable tree-shaking

@nguyenbatranvan
Copy link
Author

optimization: { usedExports: true, providedExports: true, }

image
Not working bro!!

@nguyenbatranvan
Copy link
Author

any solutions for this?

@JSerFeng JSerFeng self-assigned this Jun 6, 2024
@JSerFeng
Copy link
Collaborator

JSerFeng commented Jun 6, 2024

I suppose the ./utils is your ./index.ts right ?

Here is my demo for you: https://github.com/JSerFeng/rspack-repro-treeshaking

If you have any questions, feel free to ask, it would be better if you can upload your reproduce demo

@JSerFeng JSerFeng closed this as completed Jun 6, 2024
@nguyenbatranvan
Copy link
Author

I suppose the ./utils is your ./index.ts right ?

Here is my demo for you: https://github.com/JSerFeng/rspack-repro-treeshaking

If you have any questions, feel free to ask, it would be better if you can upload your reproduce demo

why comment console.log(dayjs()), The problem I encountered was running that command line
image

@JSerFeng
Copy link
Collaborator

JSerFeng commented Jun 6, 2024

You can uncomment this, the result is the same

The point is set utils.js as sideEffect free

@ljianc
Copy link

ljianc commented Jun 11, 2024

官方文档的例子也不生效!设置成 minimize: true才生效。官网还特意设置成 minimize: false来演示,严重误导啊。应该改成minimize: true也生效吧?方便调试。@JSerFeng

@JSerFeng
Copy link
Collaborator

官方文档的例子也不生效!设置成 minimize: true才生效。官网还特意设置成 minimize: false来演示,严重误导啊。应该改成minimize: true也生效吧?方便调试。@JSerFeng

官网例子可能有误导。你说的不生效可能是发现函数仍然存在于产物中,但其实那些未使用函数的引用已经没有了,这是符合预期的,webpack 去除掉的是未使用函数的导出,依赖 minimizer 去掉死代码。

@ljianc
Copy link

ljianc commented Jun 11, 2024

官方文档的例子也不生效!设置成 minimize: true才生效。官网还特意设置成 minimize: false来演示,严重误导啊。应该改成minimize: true也生效吧?方便调试。@JSerFeng

官网例子可能有误导。你说的不生效可能是发现函数仍然存在于产物中,但其实那些未使用函数的引用已经没有了,这是符合预期的,webpack 去除掉的是未使用函数的导出,依赖 minimizer 去掉死代码。

对,官网确实误导。应该改一下文档,不能特意拿minimize: false来演示

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

4 participants