This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): get webpack stats using
fs
feat(server): get webpack stats using `fs` and `setTimeout` (on first run)
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |