Skip to content

Commit

Permalink
feat($cssChunksMap): add support for sending map of css chunk names t…
Browse files Browse the repository at this point in the history
…o files
  • Loading branch information
faceyspacey committed Jul 4, 2017
1 parent 5a36b1e commit 2c86d12
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
54 changes: 54 additions & 0 deletions __tests__/__snapshots__/flushChunks.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
exports[`flushChunks() called as pure function babel - chunkNames 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand All @@ -34,11 +43,20 @@ Object {
exports[`flushChunks() called as pure function babel: uses default entries when no named chunks provided via opts.before/after 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand Down Expand Up @@ -67,11 +85,20 @@ Object {
exports[`flushChunks() called as pure function babel: uses entries provided by opts.before/after 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand Down Expand Up @@ -99,11 +126,20 @@ Object {
exports[`flushChunks() called as pure function webpack - chunkNames 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand All @@ -130,11 +166,20 @@ Object {
exports[`flushChunks() called as pure function webpack: uses default entries when no named chunks provided via opts.before/after 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand Down Expand Up @@ -163,11 +208,20 @@ Object {
exports[`flushChunks() called as pure function webpack: uses entries provided by opts.before/after 1`] = `
Object {
"Css": [Function],
"CssChunks": [Function],
"Js": [Function],
"Styles": [Function],
"css": Object {
"toString": [Function],
},
"cssChunks": Object {
"toString": [Function],
},
"cssChunksHash": Object {
"chunk1": "/static/0.css",
"chunk2": "/static/1.css",
"main": "/static/main.css",
},
"js": Object {
"toString": [Function],
},
Expand Down
22 changes: 20 additions & 2 deletions src/createApiWithCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type StatelessComponent = () => React.Element<*>
type ObjectString = {
toString: () => string
}
export type CssChunksHash = {
[key: string]: string
}

export type Api = {
Js: StatelessComponent,
Styles: StatelessComponent,
Expand All @@ -30,7 +34,8 @@ export default (
files: Array<string>,
filesOrderedForCss: Array<string>,
publicPath: string,
outputPath: ?string
outputPath: ?string,
cssChunksHash: CssChunksHash
): Api => {
const regex = getJsFileRegex(files)
const scripts = files.filter(file => isJs(regex, file))
Expand Down Expand Up @@ -101,7 +106,20 @@ export default (

// 5) for completeness provide the paths even though they were inputs:
publicPath,
outputPath
outputPath,

// 6) special goodness for dual-file import()
cssChunksHash,
CssChunks: () => (
<script
type='text/javascript'
dangerouslySetInnerHTML={{ __html: JSON.stringify(cssChunksHash) }}
/>
),
cssChunks: {
toString: () =>
`<script type='text/javascript'>window.__CSS_CHUNKS__= ${JSON.stringify(cssChunksHash)}</script>`
}
}

return api
Expand Down
15 changes: 13 additions & 2 deletions src/flushChunks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import type { Api } from './createApiWithCss'
import type { Api, CssChunksHash } from './createApiWithCss'
import createApiWithCss from './createApiWithCss'

declare function __webpack_require__(id: string): any
Expand Down Expand Up @@ -78,7 +78,8 @@ const flushChunks = (stats: Stats, isWebpack: boolean, opts: Options = {}) => {
...files // correct incrementing order already
],
stats.publicPath,
opts.outputPath
opts.outputPath,
createCssHash(stats.assetsByChunkName, stats.publicPath)
)
}

Expand Down Expand Up @@ -199,6 +200,16 @@ const filesFromChunks = (
return [].concat(...chunkNames.filter(hasChunk).map(entryToFiles))
}

const createCssHash = (
assetsByChunkName: FilesMap,
publicPath: string
): CssChunksHash =>
Object.keys(assetsByChunkName).reduce((hash, name) => {
const file = assetsByChunkName[name].find(file => file.endsWith('.css'))
if (file) hash[name] = `${publicPath}${file}`
return hash
}, {})

/** EXPORTS FOR TESTS */

export {
Expand Down

0 comments on commit 2c86d12

Please sign in to comment.