Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(server): get webpack stats using fs
Browse files Browse the repository at this point in the history
feat(server): get webpack stats using `fs` and `setTimeout` (on first
run)
  • Loading branch information
Metnew committed Nov 6, 2017
1 parent ca8ae99 commit 5221391
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/server/ssr/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow
import fs from 'fs'

let cache = {}

async function getFile (path) {
// Cache file forever
if (cache[path]) {
console.log('from cache')
return Promise.resolve(cache[path])
}

return new Promise((resolve, reject) => {
// Give Webpack some time on first stats generation
setTimeout(() => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
throw err
}
const json = JSON.parse(data)
cache[path] = json
resolve(json)
})
}, 4000)
})
}

export default async function () {
const basePath = process.env.CLIENT_DIST_PATH
const assets = await getFile(`${basePath}/webpack-assets.json`)
const faviconsAssets = await getFile(`${basePath}/favicons-stats.json`)

return {
assets,
faviconsAssets
}
}

0 comments on commit 5221391

Please sign in to comment.