[英]Does ReactJS reuse imported packages?
Does ReactJS reuse imported packages? ReactJS是否会重用导入的包?
Let's say I have a file called DisplayItems.js and EditItem.js. 假设我有一个名为DisplayItems.js和EditItem.js的文件。 They are both imported into App.js.
它们都被导入到App.js中。
If I import a package (like axios) at the top of my DisplayItem.js file ( import axios from 'axios';
), and I also import it in my EditItem.js file, does my Application grow by 13kb or 26kb (assuming axios is 13kb)? 如果我在DisplayItem.js文件的顶部导入了一个包(如axios)(
import axios from 'axios';
),并且也将其导入了EditItem.js文件,那么我的应用程序会增加13kb还是26kb(假设axios是13kb)?
This behavior is controlled not by React, but by whatever build tool compiles and bundles your import
statements into a JavaScript file for the browser. 这种行为不是由React控制的,而是由任何构建工具来编译的,并将您的
import
语句捆绑并捆绑到浏览器的JavaScript文件中。
The Create React App template currently uses Webpack as its build tool. Create React App模板当前使用Webpack作为其构建工具。 Webpack avoids duplicating code that is imported multiple times;
Webpack避免了重复导入多次的代码; it only writes the definitions once.
它只写一次定义。 If you are using a different project setup for your React app, your project may use a different build tool.
如果您为React应用程序使用其他项目设置,那么您的项目可能会使用其他构建工具。
In response to jhpratt, I did think of testing it on my own, but I knew it would take some time (about 34 mins). 作为对jhpratt的回应,我确实考虑过自己进行测试,但是我知道这将需要一些时间(大约34分钟)。
Here's the test results. 这是测试结果。
Importing jQuery 导入jQuery
2742120 (the control) - with jquery imported once 2742120(控件)-一次导入jquery
2742353 - with jquery imported twice (233 byte difference, .2kb) 2742353-导入了两次jQuery(差异为233字节,.2kb)
2741887 - with jquery not being imported (233 byte difference, .2kb) 2741887-未导入jquery(233字节差,.2kb)
Importing modal-vanilla 进口莫代尔香草
2742120 (the control) - with modal-vanilla being imported once 2742120(控件)-一次导入了模态香草
2742406 - with modal-vanilla being import twice (286 byte difference, .3kb) 2742406-两次导入了模态香草(差异为286字节,.3kb)
2712501 - with modal-vanilla not being imported (29386 byte difference, 29.3kb) 2712501-未导入模态香草(29386字节差,29.3kb)
I'm not sure what was going on with jQuery (maybe I added it somewhere else in my project?), but it does look like packages are reused (at least in this instance). 我不确定jQuery发生了什么(也许我在项目中的其他地方添加了它?),但是它确实看起来像包被重用了(至少在这种情况下)。
Just in case anyone's wondering, I am using Laravel's React setup. 万一有人想知道,我正在使用Laravel的React设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.