Skip to content

Commit c81bf01

Browse files
docs(README): Adding API, PERKS, and examples
Mergeing the new documentation with the old for a more concise read. Updating the example code and the perks of our plugin
1 parent 50cd8a7 commit c81bf01

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

README.md

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ Its got all the goodness of `mini-css-extract-plugin` but with 2 gleaming, sough
2929

3030
Compared to the existing loaders, we are offering a single solution as opposed to needing to depend on multiple loaders to cater for different features:
3131

32+
## Perks
33+
* **HMR:** It also has first-class support for **Hot Module Replacement** across ALL those css files/chunks!!!
34+
* cacheable stylesheets
35+
* smallest total bytes sent compared to "render-path" css-in-js solutions that include your CSS definitions in JS
36+
* Faster than the V2!
3237
* Async loading
3338
* No duplicate compilation (performance)
3439
* Easier to use
3540
* Specific to CSS
36-
* True **Hot Module Reloading** - that means no `style-loader`
3741
* SSR Friendly development build, focused on frontend DX
3842
* Works seamlessly with the Universal family
3943
* Works fantastically as a standalone style loader (You can use it for any webpack project! with no extra dependencies!)
@@ -49,7 +53,7 @@ If you want to test this alpha branch, which is currently not published to the N
4953

5054
Add the following to your package.json file, then
5155

52-
npm i extract-css-chunks-webpack-plugin@next
56+
yarn add extract-css-chunks-webpack-plugin@next
5357

5458

5559
## Webpack 4 Standalone Installation:
@@ -112,10 +116,6 @@ If you do need Webpack 3, make sure to stick with the latest `v2.x.x` release. `
112116
- [react-universal-component](https://github.com/faceyspacey/react-universal-component)
113117
- [babel-plugin-universal-import](https://github.com/faceyspacey/babel-plugin-universal-import) ***or*** [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import)
114118

115-
<details><summary>See Old Docs</summary>
116-
Like `extract-text-webpack-plugin`, but creates multiple css files (one per chunk). Then, as part of server side rendering, you can deliver just the css chunks needed by the current request. The result is the most minimal CSS initially served compared to emerging "render path" solutions.
117-
118-
For a demo, `git clone`: [universal-demo](https://github.com/faceyspacey/universal-demo)
119119

120120
## Recommended Installation
121121
```
@@ -139,22 +139,23 @@ module.exports = {
139139
rules: [
140140
{
141141
test: /\.css$/,
142-
use: ExtractCssChunks.extract({
143-
use: {
142+
use: [
143+
ExtractCssChunks.loader,
144+
{
144145
loader: 'css-loader',
145146
options: {
146147
modules: true,
147-
localIdentName: '[name]__[local]--[hash:base64:5]'
148-
}
149-
}
150-
})
151-
}
152-
]
148+
localIdentName: '[name]__[local]--[hash:base64:5]',
149+
},
150+
},
151+
],
152+
},
153+
],
153154
},
154155
plugins: [
155-
new ExtractCssChunks,
156+
new ExtractCssChunks(),
156157
]
157-
}
158+
};
158159
```
159160

160161
## Desired Output
@@ -174,21 +175,16 @@ Here's the sort of CSS you can expect to serve:
174175
<script type='text/javascript' src='/static/0.js'></script>
175176
<script type='text/javascript' src='/static/7.js'></script>
176177
<script type='text/javascript' src='/static/main.js'></script>
177-
178-
<!-- stylsheets that will be requested when import() on user navigation is called -->
179-
<script>
180-
window.__CSS_CHUNKS__ = {
181-
Foo: '/static/Foo.css',
182-
Bar: '/static/Bar.css'
183-
}
184-
</script>
185178
</body>
186179
```
187180

188-
[webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks) will scoop up the exact stylesheets to embed in your response. It essentially automates producing the above.
181+
If you need to resolve your stylesheets on the client side, for whatever reason.
182+
183+
[webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks) will scoop up the exact stylesheets to embed in your response. It will give you an object which can be embedded on page
189184

190185
Here's how you do it:
191186

187+
192188
*src/components/App.js:*
193189
```js
194190
const UniversalComponent = universal(props => import(`./${props.page}`))
@@ -214,7 +210,9 @@ res.send(`
214210
</head>
215211
<body>
216212
<div id="root">${app}</div>
213+
<!-- not needed unless you want to access css chunks urls manually -->
217214
${cssHash}
215+
<!-- extract-css-chunks takes care of loading the css assets automatically -->
218216
${js}
219217
</body>
220218
</html>
@@ -223,30 +221,21 @@ res.send(`
223221

224222
***As for asynchronous calls to `import()` on user navigation,*** [babel-plugin-universal-import](https://github.com/faceyspacey/babel-plugin-universal-import) is required if you're using [react-universal-component](https://github.com/faceyspacey/react-universal-component). And if you aren't, you must use: [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import).
225223

226-
These babel plugins request both your js + your css. *Very Nice!* This is the new feature of the 2.0. Read *Sokra's* (author of webpack) article on how [on how this is the future of CSS for webpack](https://medium.com/webpack/the-new-css-workflow-step-1-79583bd107d7). Use this and be in the future today.
227-
228-
## Perks
229-
- **HMR:** It also has first-class support for **Hot Module Replacement** across ALL those css files/chunks!!!
230-
- cacheable stylesheets
231-
- smallest total bytes sent compared to "render-path" css-in-js solutions that include your CSS definitions in JS
232-
- Faster than the V1!
233-
234224

225+
These babel plugins request both your js + your css. *Very Nice!* This is the new feature of the 2.0. Read *Sokra's* (author of webpack) article on how [on how this is the future of CSS for webpack](https://medium.com/webpack/the-new-css-workflow-step-1-79583bd107d7). Use this and be in the future today.
235226

236227

237228
## API
238-
You can pass the same options as `extract-text-webpack-plugin` to `new ExtractCssChunks`, such as:
229+
You can pass the same options as `mini-css-extract` to `new ExtractCssChunks`, such as:
239230

240231
```javascript
241232
new ExtractCssChunk({
242-
filename: '[name].[contenthash].css'
233+
filename: devMode ? '[name].css' : '[name].[hash].css',
234+
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
243235
})
244236
```
245237

246-
Keep in mind, by default `[name].css` is used when `process.env.NODE_ENV === 'development'` and `[name].[contenthash].css` during production, so you can likely forget about having to pass anything.
247-
248-
The 2 exceptions are: `allChunks` will no longer do anything, and `fallback` will no longer do anything when passed to to `extract`. Basically just worry about passing your `css-loader` string and `localIdentName` 🤓
249-
238+
>Keep in mind, by default `[name].css` is used when `process.env.NODE_ENV === 'development'` and `[name].[contenthash].css` during production, so you can likely forget about having to pass anything.
250239
251240
### HMR Pitfall
252241

@@ -274,8 +263,12 @@ For example, when running the build using some form of npm script:
274263
```
275264
[cross-env](https://www.npmjs.com/package/cross-env) is optional but recommended.
276265

277-
## What about Glamorous, Styled Components, Styled-Jsx, Aphrodite, etc?
278266

267+
What about Glamorous, Styled Components, Styled-Jsx, Aphrodite, etc?
268+
<details>
269+
<summary>
270+
<span style="color:blue">Click to Read</span>.
271+
</summary>
279272
If you effectively use code-splitting, **Exract Css Chunks** can be a far better option than using emerging solutions like *Glamorous*, *Styled Components*, and slightly older tools like *Aphrodite*, *Glamor*, etc. We aren't fans of either rounds of tools because of several issues, but particularly because they all have a runtime overhead. Every time your React component is rendered with those, CSS is generated and updated within the DOM. On the server, you're going to also see unnecessary cycles for flushing the CSS along the critical render path. *Next.js's* `styled-jsx`, by the way, doesn't even work on the server--*not so good when it comes to flash of unstyled content (FOUC).*
280273

281274
The reason *Extract CSS Chunk* can be a better option is because *we also generate multiple sets of CSS based on what is actually "used",* ***but without the runtime overhead***. The difference is our definition of "used" is modules determined *statically* (which may not in fact be rendered) vs. what is in the "critical render path" (as is the case with the other tools).
@@ -285,6 +278,12 @@ So yes, our CSS may be mildly larger and include unnecessary css, but our `no_cs
285278
On top of that, those are extra packages all with a huge number of issues in their Github repos corresponding to various limitations in the CSS they generate--something that is prevented when your definition for "CSS-in-JS" is simply importing CSS files compiled as normal by powerful proven CSS-specific processors.
286279

287280
Lastly, those solutions don't provide cacheable stylesheets. They do a lot of work--but they will *continue* doing it for you when you could have been done in one go long ago. Cloudflare is free--serve them through their CDN and you're winning. I love true javascript in css--don't get me wrong--but first I'd have to see they generate cacheable stylesheets. In my opinion, for now, it's best for environments that natively support it such as React Native.
281+
<details>
282+
<details><summary>See Old Docs</summary>
283+
284+
For a demo, `git clone`: [universal-demo](https://github.com/faceyspacey/universal-demo)
285+
286+
288287

289288
#### Next:
290289

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "extract-css-chunks-webpack-plugin",
3-
"version": "3.0.0-beta.1",
3+
"version": "3.0.1-beta.1",
44
"author": "James Gillmore <james@faceyspacey.com>",
55
"contributors": [
66
"Zack Jackson <zackary.l.jackson@gmail.com> (https://github.com/zackljackson)"

0 commit comments

Comments
 (0)