Update asset utils

This commit is contained in:
Michael Jackson
2016-06-12 23:19:38 -07:00
parent 0b2906979d
commit 2b9ffd90f9
4 changed files with 17459 additions and 16 deletions

View File

@ -32,7 +32,7 @@ const createBundle = (webpackStats) => {
}
/**
* An express middleware that sets req.assets from the build
* An express middleware that sets req.bundle from the build
* info in the given stats file. Should be used in production.
*/
export const staticAssets = (webpackStatsFile) => {
@ -48,33 +48,33 @@ export const staticAssets = (webpackStatsFile) => {
)
}
const assets = createBundle(stats)
const bundle = createBundle(stats)
return (req, res, next) => {
req.assets = assets
req.bundle = bundle
next()
}
}
/**
* An express middleware that sets req.assets from the
* An express middleware that sets req.bundle from the
* latest result from a running webpack compiler (i.e. using
* webpack-dev-middleware). Should only be used in dev.
*/
export const assetsCompiler = (webpackCompiler) => {
let assets
export const devAssets = (webpackCompiler) => {
let bundle
webpackCompiler.plugin('done', (stats) => {
assets = createBundle(stats.toJson())
bundle = createBundle(stats.toJson())
})
return (req, res, next) => {
invariant(
assets != null,
'assetsCompiler middleware needs a running compiler; ' +
'use webpack-dev-middleware in front of assetsCompiler'
bundle != null,
'devAssets middleware needs a running compiler; ' +
'use webpack-dev-middleware in front of devAssets'
)
req.assets = assets
req.bundle = bundle
next()
}
}