热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Treeshakingproblem(ReactApplicationandLibrary)

higuysihaveproblemwithtreeshakinginnx,problemwithassetslibrary

hi guys i have problem with tree shaking in nx,

problem with assets library

so i create an application let`s says its (main app) and i create a library called (assets)

index.js (assets library)

1
2
export { default as Logo} from './lib/Logo/';

export { default as Loading } from './lib/Loading/';

main.js (main apps)

1
2
3
4
5
6
7
8
9
10
11
import { Logo} from '-test/assets';



export const Main= () => {

    return (

        <>

     

       

    );

};



export default Main;

nx build main --prod --buildLibsFromSource

when i bundle my main apps in dist/apps/main
i have loading.(hash number).svg so why this loading get bundle?

and when i delete this code

1
export { default as Loading } from &#039;./lib/Loading/&#039;;

on my assets index.js
loading.(hash number).svg not get bundle on dist folder

this problem also happen on my ui library and increase a lot of my bundle size

problem with ui library

index.js (ui library )

1
2
export { default as MyButton} from &#039;./lib/Button/Button&#039;;

export { default as MyModal } from &#039;./lib/Modal/Modal&#039;;

Button.js

1
2
3
4
5
6
7
import {Button} from &#039;antd&#039;



export const MyButton &#061;() &#061;>{

    return (

       

    }

};

Modal.js

1
2
3
4
5
6
7
import {Modal} from &#039;antd&#039;



export const MyModal &#061;() &#061;>{

    return (

       

    }

};

Main.js

1
2
3
4
5
6
7
8
9
10
11
import {MyButton} from &#039;-test/ui&#039;;



export const Main&#061; () &#061;> {

    return (

        <>

     

       

    );

};



export default Main;

as you can see its only import button on main app, but when i remove export Modal on my index.js (entry file ui library) and now its only import button

1
export { default as MyButton} from &#039;./lib/Button/Button&#039;;

bundle size decrease 30kb

so why its happen? its like nx keep bundle all the third party (ant design modal) not what im using which is only button

该提问来源于开源项目:nrwl/nx

We have the same problem in our nx monorepo. So for example we have an UI lib with many components. Each component has it’s own module file. And all modules are exported through the index.ts file of the lib.



When I now have an app and only import one module from the UI lib, all modules from the lib are included in the bundle after the build, even for the production build.

So one solution is to add path mapping in the root tsconfig.base.json for each module from the lib. Then only the need module is included in the build.



But maybe there is a nicer way? My colleague told me for publishable libs ng-packagr would do the magic. But since we have a monorepo we do not have any publishable lib. It would be nice if there would be some mechanism like for instance in the Angular Material library, where you can also import each module on it’s own to keep the bundle size low.


   



推荐阅读
author-avatar
小鱼2502907687
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有