Skip to content

update tree shaking guide for webpack 4 #1865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2018

Conversation

quantizor
Copy link

updated the treeshaking documentation for webpack 4

@quantizor
Copy link
Author

cc @sokra

I think uglify isn't technically needed anymore to do tree shaking right? But seems like a good recommendation to enable it anyway.

@jeremenichelli
Copy link
Member

Thanks @probablyup, can you change the base from masterto next we are mergin everything there first. Thanks!

@quantizor quantizor changed the base branch from master to next February 28, 2018 19:28
@quantizor
Copy link
Author

@jeremenichelli done

@@ -144,7 +144,7 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds
With that said, let's run `npx webpack` with our script as the [entry point](/concepts/entry-points) and `bundle.js` as the [output](/concepts/output). The `npx` command, which ships with Node 8.2 or higher, runs the webpack binary (`./node_modules/.bin/webpack`) of the webpack package we installed in the beginning:

``` bash
npx webpack src/index.js dist/bundle.js
npx webpack src/index.js --output dist/bundle.js
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure where this change came from...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the next rebase maybe.

## Minify the Output
## Mark the file as side-effect-free

In a 100% ESM module world, identifying side effects is straightforward. However, we don't live in that reality just yet, so it's necessary to provide hints to webpack's compiler on the "pureness" of your code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rephrase we don't live in that reality just yet, so to we aren't there just yet, in the mean time


Let's start by installing it:
T> A "side effect" is defined as code that performs a special behavior when imported, other than simply exposing one or more exports. For instance: a polyfill which adjusts the global scope but does not provide an export or has one that you are not using in your codebase.
Copy link
Member

@jeremenichelli jeremenichelli Feb 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rephrase this as:

A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports like a polyfill, which might affect the global scope and might not provide an export or has one that you are not using in your codebase.

## Minify the Output

So we've cued up our "dead code" to be dropped by using the `import` and `export` syntax, but we still need to drop it from the bundle. To do that, we'll use the `-p` (production) webpack compilation flag to enable the uglifyjs minification plugin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheLarkInn @sokra is the -p flag still recommended in webpack 4? Or should we clarify that is for versions older than v4?

## Conclusion

So, what we've learned is that in order to take advantage of _tree shaking_, you must...

- Use ES2015 module syntax (i.e. `import` and `export`).
- Add a "sideEffects" entry to your project's `package.json` file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be if the code is not affecting outside scope, not always so we should add it to here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, you should always add a sideEffects entry or treeshaking won't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, but if your librari does produce side effects you shouldn't, right? I don't want to give that message to package owners.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, but in that case set it to true, or provide an array with the specific files that include side effects as described in the guide.

It's better to explicitly have the sideEffects known to webpack than to try and infer it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is redundant since you already define what sideEffects means. I'm OK with it. Sorry for the noise.

@jeremenichelli
Copy link
Member

There are some rephrasing that I think is necessary, the rest looks good @probablyup

@quantizor
Copy link
Author

@jeremenichelli updated

@edmorley
Copy link
Contributor

Do you think it's worth also linking to the debugging optimization bailouts section from this page?

@edmorley
Copy link
Contributor

I think this should also include:

@quantizor
Copy link
Author

@edmorley updated

@quantizor
Copy link
Author

@jeremenichelli could this be merged? I think many people would find it helpful

@jeremenichelli jeremenichelli merged commit c1318e1 into webpack:next Mar 15, 2018
@edmorley
Copy link
Contributor

@probablyup this is great - thank you for the changes :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants