-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
How to split code? #271
Conversation
- pksjce | ||
--- | ||
|
||
In `webpack`, when you use the css loader and import css into your javascript files, the css is bundled along with your javascript. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 }) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two spaces before webpack
.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/s/parallely/parallel
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Can you rebase? I'll merge after that. |
Done! |
Trying to tackle #3 with this.