Update asset utils
This commit is contained in:
@ -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.
|
* info in the given stats file. Should be used in production.
|
||||||
*/
|
*/
|
||||||
export const staticAssets = (webpackStatsFile) => {
|
export const staticAssets = (webpackStatsFile) => {
|
||||||
@ -48,33 +48,33 @@ export const staticAssets = (webpackStatsFile) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const assets = createBundle(stats)
|
const bundle = createBundle(stats)
|
||||||
|
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
req.assets = assets
|
req.bundle = bundle
|
||||||
next()
|
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
|
* latest result from a running webpack compiler (i.e. using
|
||||||
* webpack-dev-middleware). Should only be used in dev.
|
* webpack-dev-middleware). Should only be used in dev.
|
||||||
*/
|
*/
|
||||||
export const assetsCompiler = (webpackCompiler) => {
|
export const devAssets = (webpackCompiler) => {
|
||||||
let assets
|
let bundle
|
||||||
webpackCompiler.plugin('done', (stats) => {
|
webpackCompiler.plugin('done', (stats) => {
|
||||||
assets = createBundle(stats.toJson())
|
bundle = createBundle(stats.toJson())
|
||||||
})
|
})
|
||||||
|
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
invariant(
|
invariant(
|
||||||
assets != null,
|
bundle != null,
|
||||||
'assetsCompiler middleware needs a running compiler; ' +
|
'devAssets middleware needs a running compiler; ' +
|
||||||
'use webpack-dev-middleware in front of assetsCompiler'
|
'use webpack-dev-middleware in front of devAssets'
|
||||||
)
|
)
|
||||||
|
|
||||||
req.assets = assets
|
req.bundle = bundle
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17443
modules/server/CloudFlareStats.json
Normal file
17443
modules/server/CloudFlareStats.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,7 @@ const DOCTYPE = '<!DOCTYPE html>'
|
|||||||
|
|
||||||
const fetchStats = (callback) => {
|
const fetchStats = (callback) => {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
callback(null, require('./stats.json'))
|
callback(null, require('./CloudFlareStats.json'))
|
||||||
} else {
|
} else {
|
||||||
getZones('npmcdn.com')
|
getZones('npmcdn.com')
|
||||||
.then(zones => {
|
.then(zones => {
|
||||||
@ -27,8 +27,8 @@ const fetchStats = (callback) => {
|
|||||||
|
|
||||||
export const sendHomePage = (req, res, next) => {
|
export const sendHomePage = (req, res, next) => {
|
||||||
const props = {
|
const props = {
|
||||||
styles: req.assets.getStyleAssets([ 'vendor', 'home' ]),
|
styles: req.bundle.getStyleAssets([ 'vendor', 'home' ]),
|
||||||
scripts: req.assets.getScriptAssets([ 'vendor', 'home' ])
|
scripts: req.bundle.getScriptAssets([ 'vendor', 'home' ])
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchStats((error, stats) => {
|
fetchStats((error, stats) => {
|
||||||
|
@ -7,7 +7,7 @@ import express from 'express'
|
|||||||
import devErrorHandler from 'errorhandler'
|
import devErrorHandler from 'errorhandler'
|
||||||
import WebpackDevServer from 'webpack-dev-server'
|
import WebpackDevServer from 'webpack-dev-server'
|
||||||
import { createRequestHandler } from 'npm-http-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 webpackConfig from '../../webpack.config'
|
||||||
import { sendHomePage } from './MainController'
|
import { sendHomePage } from './MainController'
|
||||||
import { logStats } from './StatsUtils'
|
import { logStats } from './StatsUtils'
|
||||||
@ -78,7 +78,7 @@ export const createDevServer = (config) => {
|
|||||||
// This runs after webpack-dev-middleware.
|
// This runs after webpack-dev-middleware.
|
||||||
server.use(devErrorHandler())
|
server.use(devErrorHandler())
|
||||||
server.use(express.static(config.publicDir))
|
server.use(express.static(config.publicDir))
|
||||||
server.use(assetsCompiler(compiler))
|
server.use(devAssets(compiler))
|
||||||
server.use(createRouter(config))
|
server.use(createRouter(config))
|
||||||
|
|
||||||
return server
|
return server
|
||||||
|
Reference in New Issue
Block a user