Skip to content

Commit e814d9d

Browse files
committed
fix($compliation): make work when chunk names not used + add better example to readme
1 parent 36d0462 commit e814d9d

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</p>
1818

1919
# extract-css-chunks-webpack-plugin
20-
> **UPDATE (July 7th):** [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import) is now required to asynchronously import both css + js. *Much Faster Builds!* You likely want to read its intro article: https://medium.com/@faceyspacey/webpacks-import-will-soon-fetch-js-css-here-s-how-you-do-it-today-4eb5b4929852
20+
> **UPDATE (July 7th):** [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import) is now required to asynchronously import both css + js. *Much Faster Builds!* You likely want to read [its intro article](https://medium.com/@faceyspacey/webpacks-import-will-soon-fetch-js-css-here-s-how-you-do-it-today-4eb5b4929852). Note: with *webpack-flush-chunks* you will have to use the `chunkNames` option instead of the `moduleIds` option to use it.
2121
2222
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.
2323

@@ -123,6 +123,47 @@ Keep in mind, by default `[name].css` is used when `process.env.NODE_ENV === 'de
123123

124124
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` 🤓
125125

126+
## Usage with [react-universal-component](https://github.com/faceyspacey/react-universal-component), [webpack-flush-chunks](https://github.com/faceyspacey/webpack-flush-chunks) and [babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import)
127+
128+
When using `webpack-flush-chunks` you will have to supply the `chunkNames` option, not the `moduleIds` option since the babel plugin is based on chunk names. Here's an example:
129+
130+
*src/components/App.js:*
131+
```js
132+
const UniversalComponent = universal(() => import('./Foo'), {
133+
resolve: () => require.resolveWeak('./Foo'),
134+
chunkName: 'Foo'
135+
})
136+
137+
const UniversalDynamicComponent = universal(() => import(`./base/${page}`), {
138+
resolve: ({ page }) => require.resolveWeak(`./base/${page}`),
139+
chunkName: ({ page }) => `base/${page}`
140+
})
141+
142+
```
143+
*server/render.js:*
144+
```js
145+
import { flushChunkNames } from 'react-universal-component/server'
146+
import flushChunks from 'webpack-flush-chunks'
147+
148+
const app = ReactDOMServer.renderToString(<App />)
149+
const { js, styles, cssHash } = flushChunks(webpackStats, {
150+
chunkNames: flushChunkNames()
151+
})
152+
153+
res.send(`
154+
<!doctype html>
155+
<html>
156+
<head>
157+
${styles}
158+
</head>
159+
<body>
160+
<div id="root">${app}</div>
161+
${js}
162+
${cssHash}
163+
</body>
164+
</html>
165+
`)
166+
```
126167

127168
## What about Aphrodite, Glamor, StyleTron, Styled-Components, Styled-Jsx, etc?
128169

index.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,21 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
293293
// HMR: inject file name into corresponding javascript modules in order to trigger
294294
// appropriate hot module reloading of CSS
295295
if (DEV) {
296-
compilation.plugin("optimize-module-ids", function(modules){
296+
compilation.plugin("before-chunk-assets", function() {
297297
extractedChunks.forEach(function(extractedChunk) {
298-
extractedChunk.modules.forEach(function (module) {
299-
if(module.__fileInjected) {
300-
return;
301-
}
298+
extractedChunk.modules.forEach(function(module) {
299+
if(module.__fileInjected) return;
302300
module.__fileInjected = true;
303301

304-
extractedChunk.modules.forEach(function(module){
305-
var originalModule = module.getOriginalModule();
306-
var file = getFile(compilation, filename, module, extractedChunk);
307-
originalModule._source._value = originalModule._source._value.replace('%%extracted-file%%', file);
308-
});
302+
var originalModule = module.getOriginalModule();
303+
var file = getFile(compilation, filename, module, extractedChunk.originalChunk);
304+
originalModule._source._value = originalModule._source._value.replace('%%extracted-file%%', file);
309305
});
310306
});
311307
});
312308
}
309+
310+
// add the css files to assets and the files array corresponding to its chunks
313311
compilation.plugin("additional-assets", function(callback) {
314312
extractedChunks.forEach(function(extractedChunk) {
315313
if(extractedChunk.modules.length) {
@@ -321,12 +319,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
321319
return getOrder(a, b);
322320
});
323321

322+
var stylesheet = this.renderExtractedChunk(extractedChunk);
324323
var chunk = extractedChunk.originalChunk;
325-
var module = this.renderExtractedChunk(extractedChunk);
326-
var file = getFile(compilation, filename, module, extractedChunk)
324+
var file = getFile(compilation, filename, stylesheet, chunk)
327325

328-
// add the css files to assets and the files array corresponding to its chunk
329-
compilation.assets[file] = module;
326+
compilation.assets[file] = stylesheet;
330327
chunk.files.push(file);
331328
}
332329
}, this);

0 commit comments

Comments
 (0)