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()
}
}

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ const DOCTYPE = '<!DOCTYPE html>'
const fetchStats = (callback) => {
if (process.env.NODE_ENV === 'development') {
callback(null, require('./stats.json'))
callback(null, require('./CloudFlareStats.json'))
} else {
getZones('npmcdn.com')
.then(zones => {
@ -27,8 +27,8 @@ const fetchStats = (callback) => {
export const sendHomePage = (req, res, next) => {
const props = {
styles: req.assets.getStyleAssets([ 'vendor', 'home' ]),
scripts: req.assets.getScriptAssets([ 'vendor', 'home' ])
styles: req.bundle.getStyleAssets([ 'vendor', 'home' ]),
scripts: req.bundle.getScriptAssets([ 'vendor', 'home' ])
}
fetchStats((error, stats) => {

View File

@ -7,7 +7,7 @@ import express from 'express'
import devErrorHandler from 'errorhandler'
import WebpackDevServer from 'webpack-dev-server'
import { createRequestHandler } from 'npm-http-server'
import { staticAssets, assetsCompiler, createDevCompiler } from './AssetsUtils'
import { staticAssets, devAssets, createDevCompiler } from './AssetsUtils'
import webpackConfig from '../../webpack.config'
import { sendHomePage } from './MainController'
import { logStats } from './StatsUtils'
@ -78,7 +78,7 @@ export const createDevServer = (config) => {
// This runs after webpack-dev-middleware.
server.use(devErrorHandler())
server.use(express.static(config.publicDir))
server.use(assetsCompiler(compiler))
server.use(devAssets(compiler))
server.use(createRouter(config))
return server