Webpack import * messes tree shaking?
up vote
0
down vote
favorite
I read this here - https://www.thedevelobear.com/post/5-things-to-improve-performance/ - that doing importing all things from a library will not allow tree shaking to remove it, even if it is not used. I don't believe this, is it really true? I would think that tree shaking would identify that none of the functions except a couple were used, and then remove those.
There is a really easy way to reduce bundle size by just checking your imports. When performing methods or components from 3rd party libraries, make sure you import only the things you need, not the whole library itself. For instance, if you’re using lodash and need the fill method, import it directly instead of calling it on lodash object:
// Instead of this
import _ from ‘lodash’
let array = [1, 2, 3];
_.fill(array, '🐻');
// Do this
import { fill } from ‘lodash’
let array = [1, 2, 3];
fill(array, '🐻');
webpack tree-shaking
add a comment |
up vote
0
down vote
favorite
I read this here - https://www.thedevelobear.com/post/5-things-to-improve-performance/ - that doing importing all things from a library will not allow tree shaking to remove it, even if it is not used. I don't believe this, is it really true? I would think that tree shaking would identify that none of the functions except a couple were used, and then remove those.
There is a really easy way to reduce bundle size by just checking your imports. When performing methods or components from 3rd party libraries, make sure you import only the things you need, not the whole library itself. For instance, if you’re using lodash and need the fill method, import it directly instead of calling it on lodash object:
// Instead of this
import _ from ‘lodash’
let array = [1, 2, 3];
_.fill(array, '🐻');
// Do this
import { fill } from ‘lodash’
let array = [1, 2, 3];
fill(array, '🐻');
webpack tree-shaking
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I read this here - https://www.thedevelobear.com/post/5-things-to-improve-performance/ - that doing importing all things from a library will not allow tree shaking to remove it, even if it is not used. I don't believe this, is it really true? I would think that tree shaking would identify that none of the functions except a couple were used, and then remove those.
There is a really easy way to reduce bundle size by just checking your imports. When performing methods or components from 3rd party libraries, make sure you import only the things you need, not the whole library itself. For instance, if you’re using lodash and need the fill method, import it directly instead of calling it on lodash object:
// Instead of this
import _ from ‘lodash’
let array = [1, 2, 3];
_.fill(array, '🐻');
// Do this
import { fill } from ‘lodash’
let array = [1, 2, 3];
fill(array, '🐻');
webpack tree-shaking
I read this here - https://www.thedevelobear.com/post/5-things-to-improve-performance/ - that doing importing all things from a library will not allow tree shaking to remove it, even if it is not used. I don't believe this, is it really true? I would think that tree shaking would identify that none of the functions except a couple were used, and then remove those.
There is a really easy way to reduce bundle size by just checking your imports. When performing methods or components from 3rd party libraries, make sure you import only the things you need, not the whole library itself. For instance, if you’re using lodash and need the fill method, import it directly instead of calling it on lodash object:
// Instead of this
import _ from ‘lodash’
let array = [1, 2, 3];
_.fill(array, '🐻');
// Do this
import { fill } from ‘lodash’
let array = [1, 2, 3];
fill(array, '🐻');
webpack tree-shaking
webpack tree-shaking
edited Nov 21 at 17:07
asked Nov 21 at 15:37
Noitidart
17.3k1360149
17.3k1360149
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Yes it is true. This is done via static analysis on the named imports on the es modules.
The tool is going to statically analyze your imports and just copy from the source those that you had stated. If it were to run throughout all your code, identify all the functions that you called from that file, go back, remove those that aren't used, it would be costly and would take much more time.
if you have:
import {a} from 'filea'
but you have
export const a = 'a';
export const b = 'b';
The bundler/tool can go to your file, see:
"oh, one imported just
afromfilea, let me pull just it."
https://webpack.js.org/guides/tree-shaking/
https://medium.com/webpack/better-tree-shaking-with-deep-scope-analysis-a0b788c0ce77
https://medium.com/@zwegrzyniak/webpack-4-1-and-es-modules-esm-dd0bd7dca4da
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Yes it is true. This is done via static analysis on the named imports on the es modules.
The tool is going to statically analyze your imports and just copy from the source those that you had stated. If it were to run throughout all your code, identify all the functions that you called from that file, go back, remove those that aren't used, it would be costly and would take much more time.
if you have:
import {a} from 'filea'
but you have
export const a = 'a';
export const b = 'b';
The bundler/tool can go to your file, see:
"oh, one imported just
afromfilea, let me pull just it."
https://webpack.js.org/guides/tree-shaking/
https://medium.com/webpack/better-tree-shaking-with-deep-scope-analysis-a0b788c0ce77
https://medium.com/@zwegrzyniak/webpack-4-1-and-es-modules-esm-dd0bd7dca4da
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
add a comment |
up vote
1
down vote
accepted
Yes it is true. This is done via static analysis on the named imports on the es modules.
The tool is going to statically analyze your imports and just copy from the source those that you had stated. If it were to run throughout all your code, identify all the functions that you called from that file, go back, remove those that aren't used, it would be costly and would take much more time.
if you have:
import {a} from 'filea'
but you have
export const a = 'a';
export const b = 'b';
The bundler/tool can go to your file, see:
"oh, one imported just
afromfilea, let me pull just it."
https://webpack.js.org/guides/tree-shaking/
https://medium.com/webpack/better-tree-shaking-with-deep-scope-analysis-a0b788c0ce77
https://medium.com/@zwegrzyniak/webpack-4-1-and-es-modules-esm-dd0bd7dca4da
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Yes it is true. This is done via static analysis on the named imports on the es modules.
The tool is going to statically analyze your imports and just copy from the source those that you had stated. If it were to run throughout all your code, identify all the functions that you called from that file, go back, remove those that aren't used, it would be costly and would take much more time.
if you have:
import {a} from 'filea'
but you have
export const a = 'a';
export const b = 'b';
The bundler/tool can go to your file, see:
"oh, one imported just
afromfilea, let me pull just it."
https://webpack.js.org/guides/tree-shaking/
https://medium.com/webpack/better-tree-shaking-with-deep-scope-analysis-a0b788c0ce77
https://medium.com/@zwegrzyniak/webpack-4-1-and-es-modules-esm-dd0bd7dca4da
Yes it is true. This is done via static analysis on the named imports on the es modules.
The tool is going to statically analyze your imports and just copy from the source those that you had stated. If it were to run throughout all your code, identify all the functions that you called from that file, go back, remove those that aren't used, it would be costly and would take much more time.
if you have:
import {a} from 'filea'
but you have
export const a = 'a';
export const b = 'b';
The bundler/tool can go to your file, see:
"oh, one imported just
afromfilea, let me pull just it."
https://webpack.js.org/guides/tree-shaking/
https://medium.com/webpack/better-tree-shaking-with-deep-scope-analysis-a0b788c0ce77
https://medium.com/@zwegrzyniak/webpack-4-1-and-es-modules-esm-dd0bd7dca4da
edited Nov 21 at 16:49
answered Nov 21 at 16:45
PlayMa256
3,11911133
3,11911133
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
add a comment |
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
Oh wow thank you very much!!! Do you have any sources/references that elaborate more on this?
– Noitidart
Nov 21 at 16:46
1
1
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
added! Most of them are webpack related, but still
– PlayMa256
Nov 21 at 16:49
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
Thank you once again very much brother!
– Noitidart
Nov 21 at 16:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415558%2fwebpack-import-messes-tree-shaking%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown