From 74c81733dab9a029588b8892d69c0fbcc7078082 Mon Sep 17 00:00:00 2001 From: dmbch Date: Tue, 30 Jan 2018 22:40:28 +0100 Subject: [PATCH] feat(express): expose stats/asset paths using express add res.locals.hops.{stats,assets} to help with SSR --- packages/express/lib/utils.js | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/express/lib/utils.js b/packages/express/lib/utils.js index 9a9222815..2281530b2 100644 --- a/packages/express/lib/utils.js +++ b/packages/express/lib/utils.js @@ -8,6 +8,17 @@ var https = require('https'); var hopsConfig = require('hops-config'); +var stats; +function getStatsFromFile() { + if (!stats) { + var statsFilename = path.join(hopsConfig.buildDir, 'stats.json'); + if (fs.existsSync(statsFilename)) { + stats = require(statsFilename); + } + } + return stats || {}; +} + function defaultCallback(error) { if (error) { console.error(error.stack.toString()); @@ -64,6 +75,7 @@ exports.rewritePath = function rewritePath(req, res, next) { }; exports.registerMiddleware = function registerMiddleware(app, middleware) { + app.use(exports.assetsMiddleware); if ( process.env.HOPS_MODE === 'static' && Array.isArray(hopsConfig.locations) @@ -76,6 +88,32 @@ exports.registerMiddleware = function registerMiddleware(app, middleware) { } }; +exports.assetsMiddleware = function assetsMiddleware(req, res, next) { + res.locals.hops = { + stats: + res.locals && res.locals.webpackStats + ? res.locals.webpackStats.toJson() + : getStatsFromFile(), + }; + res.locals.hops.assets = { js: [], css: [] }; + ['vendor', 'main'].forEach(function(key) { + var asset = res.locals.hops.stats.assetsByChunkName[key]; + if (Array.isArray(asset)) { + var js = asset.find(function(item) { + return item.indexOf('.js') === item.length - 3; + }); + js && res.locals.hops.assets.js.push('/' + js); + var css = asset.find(function(item) { + return item.indexOf('.css') === item.length - 4; + }); + css && res.locals.hops.assets.css.push('/' + css); + } else if (asset) { + res.locals.hops.assets.js.push('/' + asset); + } + }); + next(); +}; + exports.bootstrap = hopsConfig.bootstrapServer || function() {}; exports.teardown = hopsConfig.teardownServer || function() {};