简体   繁体   English

使用Webpack2时,下面两条import语句有区别吗?

[英]When using Webpack2, is there a difference between the following two import statements?

I was taking a look at the documentation for an npm package and saw the following:我正在查看 npm 包的文档并看到以下内容:

Notice that in the above example, we used:请注意,在上面的示例中,我们使用了:

import RaisedButton from 'material-ui/RaisedButton';

instead of代替

import {RaisedButton} from 'material-ui';

This will make your build process faster and your build output smaller.这将使您的构建过程更快,构建输出更小。

When using Webpack2, is there a different between the two imports with regards to build speed and bundle size?使用 Webpack2 时,这两个导入在构建速度和包大小方面是否存在差异?

Yes.是的。 Both imports are different and they do affect the build time and build output.两种导入都不同,它们确实会影响构建时间和构建输出。

When you are using import {RaisedButton} from 'material-ui';当您使用import {RaisedButton} from 'material-ui'; , you are actually importing it from main index.js file, which is also exporting the other components. ,您实际上是从主index.js文件中导入它,该文件也在导出其他组件。 Therefore webpack endup bundling all the other components, which are exported in this file, in the bundle which increases bundle size and bundling time both.因此,webpack 最终将所有其他组件(在此文件中导出)捆绑在捆绑包中,这会增加捆绑包的大小和捆绑时间。

But if you use import RaisedButton from 'material-ui/RaisedButton';但是如果你使用import RaisedButton from 'material-ui/RaisedButton'; , then you are importing raised button from raised button's index.js , which is exporting raised button only. ,然后您从凸起按钮的index.js导入凸起按钮,它仅导出凸起按钮。 Therefore webpack will bundle only raised button and nothing else result in less bundle size and time.因此 webpack 只会捆绑凸起的按钮,其他任何东西都不会减少捆绑包的大小和时间。

Reading the docs for React Router, I've found the following:阅读 React Router 的文档,我发现了以下内容:

If you're going for a really minimal bundle sizes on the web you can import modules directly.如果您要在网络上使用真正最小的包大小,则可以直接导入模块。 Theoretically a tree-shaking bundler like Webpack makes this unnecessary but we haven't tested it yet.从理论上讲,像 Webpack 这样的摇树捆绑器使这成为不必要的,但我们还没有对其进行测试。 We welcome you to!我们欢迎您!

 import Router from 'react-router-dom/BrowserRouter' import Route from 'react-router-dom/Route'

// etc. // 等等。

So I think the answer is that when using Webpack, the two imports should result in the same bundle size in production, but doing something like import {RaisedButton} from 'material-ui';所以我认为答案是,当使用 Webpack 时,两个导入应该在生产中产生相同的包大小,但是做一些类似import {RaisedButton} from 'material-ui';事情import {RaisedButton} from 'material-ui'; may cause webpack to take a bit longer to bundle.可能会导致 webpack 需要更长的时间来打包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过TypeScript导入和Webpack2使用DatePicker - Using DatePicker via TypeScript import and Webpack2 使用 querySelectorAll() 的以下两个语句之间有什么区别? - What is the difference between the following two statements using the querySelectorAll()? 在react-router-dom / webpack2中使用import() - using import() in react-router-dom/webpack2 使用以下两种方法定义的方法之间的区别 - The difference between the method defined using the following two webpack中样式@import和js import之间的区别 - Difference between style @import and js import in webpack Webpack2不理解我的SASS文件中的@import语句(如何使用webpack2编译SASS?) - Webpack2 not understanding @import statement in my SASS files (How to compile SASS with webpack2?) 以下两个声明之间的区别是什么以及何时应该使用它们 - What is the difference between the following two declarations and when should they be used 模块构建失败:SyntaxError:使用webpack2时缺少类属性转换 - Module build failed: SyntaxError: Missing class properties transform when using webpack2 这些 ES6 导入语句之间有什么区别? - What is the difference between these ES6 import statements? Webpack2 + Typescript2无法动态导入json - Webpack2 + Typescript2 fails to dynamic import json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM