[英]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.