Skip to content

How to split code? #271

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 14 commits into from
Oct 26, 2016
Merged

How to split code? #271

merged 14 commits into from
Oct 26, 2016

Conversation

pksjce
Copy link

@pksjce pksjce commented Oct 23, 2016

Trying to tackle #3 with this.

  • Intro to code splitting
  • On demand code splitting
    • With require.ensure()
  • Resource splitting
    • CSS splitting
    • Vendor chunk splitting (Common chunks and manifest)

@pksjce pksjce changed the title Feat/code splitting How to split code? Oct 23, 2016
- pksjce
---

In `webpack`, when you use the css loader and import css into your javascript files, the css is bundled along with your javascript.
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Just webpack to be consistent with other docs.
  • javascript -> JavaScript
  • css -> CSS


## Using `css-loader`

To import css into your javascript code like [any other module](concept/modules), you will have to use the [css-loader](https://github.com/webpack/css-loader)
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/javascript/JavaScript


```javascript
...
loader: ExtractPlugin.extract('css-loader?sourceMap') //Can be used without sourcemaps too.
Copy link
Contributor

Choose a reason for hiding this comment

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

The syntax has changed in webpack 2.

Copy link
Author

Choose a reason for hiding this comment

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

Need to put these changes in the upgrade document too right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup. It's a breaking change.

### In the plugin

```javascript
new ExtractPlugin({ filename: 'bundle.css', disable: false, allChunks: true })
Copy link
Contributor

Choose a reason for hiding this comment

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

Why ExtractPlugin instead of ExtractTextPlugin?

Copy link
Author

Choose a reason for hiding this comment

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

No reason particularly. Changed it.


In webpack, when you use the css-loader and import CSS into your Javascript files, the CSS is bundled along with your Javascript.
This has the disadvantage that, you will not be able to utilize the browser's ability to load CSS asynchronously and parallely. Instead, your page will have to wait until your whole Javascript bundle is loaded, to style itself.
webpack can help with this problem by bundling the CSS separately using [Extract-Text-Webpack-Plugin](https://github.com/webpack/extract-text-webpack-plugin) and the [css-loader](https://github.com/webpack/css-loader)
Copy link
Member

Choose a reason for hiding this comment

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

Forgot a dot in the end.

> see also [[Output]]
> see also [[dynamic dependencies]]

Code splitting is the most compelling feature for `webpack` usage. `webpack`, with some configuration, can tweak your app such that, you can split your code into various bundles and load them on demand.
Copy link
Member

Choose a reason for hiding this comment

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

Two spaces before webpack.

Copy link
Member

@SpaceK33z SpaceK33z Oct 23, 2016

Choose a reason for hiding this comment

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

Can you take another look at the second sentence? It has too many comma's in it, making it a bit hard to read.


#### CSS splitting

An application owner would want to split all the css into a separate bundle. This enhances caheability of the resource bundle and also allows the browser to parallely load the bundle which makes for a solid performance improvement.
Copy link
Member

Choose a reason for hiding this comment

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

caheability -> cacheability

- pksjce
---

In webpack, when you use the css-loader and import CSS into your Javascript files, the CSS is bundled along with your Javascript.
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/Javascript/JavaScript

---

In webpack, when you use the css-loader and import CSS into your Javascript files, the CSS is bundled along with your Javascript.
This has the disadvantage that, you will not be able to utilize the browser's ability to load CSS asynchronously and parallely. Instead, your page will have to wait until your whole Javascript bundle is loaded, to style itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/parallely/parallel

Copy link
Contributor

Choose a reason for hiding this comment

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

/s/Javascript/JavaScript


In webpack, when you use the css-loader and import CSS into your Javascript files, the CSS is bundled along with your Javascript.
This has the disadvantage that, you will not be able to utilize the browser's ability to load CSS asynchronously and parallely. Instead, your page will have to wait until your whole Javascript bundle is loaded, to style itself.
webpack can help with this problem by bundling the CSS separately using [Extract-Text-Webpack-Plugin](https://github.com/webpack/extract-text-webpack-plugin) and the [css-loader](https://github.com/webpack/css-loader).
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe lower case extract-text-webpack-plugin.


## Using `css-loader`

To import css into your Javascript code like [any other module](concept/modules), you will have to use the [css-loader](https://github.com/webpack/css-loader)
Copy link
Contributor

Choose a reason for hiding this comment

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

JavaScript

## Using `css-loader`

To import css into your Javascript code like [any other module](concept/modules), you will have to use the [css-loader](https://github.com/webpack/css-loader)
The webpack config with `css-loader` will look somewhat like
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rephrase this and drop "somewhat".


It is for this reason, that we will need to use the [CommonsChunkPlugin](https://github.com/webpack/webpack/blob/master/lib/optimize/CommonsChunkPlugin.js).

### CommonsChunkPlugin
Copy link
Contributor

Choose a reason for hiding this comment

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

Header level + missing formatting.


On running `webpack` now, we see that two bundles have been created. If you inspect these though, you will find that the code for `moment` is present in both the files!

It is for this reason, that we will need to use the [CommonsChunkPlugin](https://github.com/webpack/webpack/blob/master/lib/optimize/CommonsChunkPlugin.js).
Copy link
Contributor

Choose a reason for hiding this comment

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

This link can break if the source changes. Maybe link to a hash (specific commit). Not sure.

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 what exactly to do. Shall I remove the link?

Copy link
Contributor

Choose a reason for hiding this comment

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

You could add plugins/commons-chunk-plugin.md stub and link to it instead.

```
Now run `webpack` on your application. Bundle inspection shows that `moment` code is present only in the vendor bundle.

### Manifest file
Copy link
Contributor

Choose a reason for hiding this comment

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

Manifest File. See also the rest.

To prevent this, we need extract out the runtime into a separate manifest file. Even though we are creating another bundle, the overhead is offset by the long term caching benefits that we obtain on the `vendor` file.

```javascript
\\ webpack.config.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Push filenames outside of the examples so you get something like this:

webpack.config.js

demo goes here


`require.ensure()` is the CommonJS way of including assets asynchronously. By adding `require.ensure([<fileurl>])`, we can define a split point in the code. webpack can then create a separate bundle of all the code inside this split point.
Learn [how to split your code using `require.ensure()`](/how-to/code-splitting/splitting-require)
### Code splitting with System.import
Copy link
Contributor

Choose a reason for hiding this comment

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

Add TODOs (?>) here so we won't forget to document these.

@bebraw
Copy link
Contributor

bebraw commented Oct 26, 2016

Can you rebase? I'll merge after that.

@pksjce
Copy link
Author

pksjce commented Oct 26, 2016

Done!

@bebraw bebraw merged commit db78115 into webpack:develop Oct 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants