[英]Why is my ES6 webpack bundle larger when using default imports instead of named imports?
I am using OpenLayers 6 and I import parts of the library using this notation:我正在使用 OpenLayers 6,并使用以下符号导入库的部分内容:
import { Map, View } from 'ol';
import { Vector as VectorSource } from 'ol/source';
import { Vector as VectorLayer } from 'ol/layer';
// More in other files [...]
When running npm run dev
I get a 9MB file for my project.运行
npm run dev
时,我的项目得到了一个9MB的文件。
For testing purpose, I tried to replace these named imports by default imports:出于测试目的,我尝试使用默认导入替换这些命名导入:
import Map from 'ol/Map';
import View from 'ol/View';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
Surprisingly, it reduced my bundled file to 6MB , It's 33% lighter?令人惊讶的是,它把我的捆绑文件减少到了6MB ,轻了 33%? why is that?
这是为什么? Shouldn't named imports be importing only required parts of modules?
命名导入不应该只导入模块的必需部分吗?
EDIT 1编辑 1
Following @Bergi comment, the library is available here .在@Bergi 评论之后,该库在此处可用。 I use the last version which is installed through
npm
: v6.4.2
我使用通过
npm
安装的最新版本: v6.4.2
EDIT 2编辑 2
As pointed out by @felixmosh answer, running npm run prod
seems to reduce the size difference.正如@felixmosh 回答所指出的,运行
npm run prod
似乎可以减少尺寸差异。 I get a difference of 1KB
from 886KB to 885KB .我从886KB到885KB得到
1KB
的差异。
Tree shaking, is part of the minification process.摇树是缩小过程的一部分。 In dev bundles this process is not applied.
在开发包中,不应用此过程。
Try to run in "production" mode, and compare the results.尝试在“生产”模式下运行,并比较结果。
No, it shouldn't.不,不应该。 It very depends on how internal libs are organized.
这很大程度上取决于内部库的组织方式。
require
inside, the lib will not be shakedrequire
,lib不会被晃动import *
inside and uses this package, the whole *
will be includedimport *
inside 并使用这个 package,整个*
将被包括在内import {name} from './names'
it still might not be tree-shakedimport {name} from './names'
它仍然可能不是 tree-shaked There is a good starting article about how tree shaking works and how to help it: https://webpack.js.org/guides/tree-shaking/有一篇很好的关于摇树如何工作以及如何帮助它的入门文章: https://webpack.js.org/guides/tree-shaking/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.