mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
26 lines
481 B
JavaScript
26 lines
481 B
JavaScript
|
import fs from 'mz/fs'
|
||
|
import resolve from './resolve'
|
||
|
|
||
|
/**
|
||
|
* resolve a JSON page like `require.resolve`,
|
||
|
* and read and cache the file content
|
||
|
*/
|
||
|
|
||
|
async function readPage (path) {
|
||
|
const f = await resolve(path)
|
||
|
if (cache.hasOwnProperty(f)) {
|
||
|
return cache[f]
|
||
|
}
|
||
|
|
||
|
const source = await fs.readFile(f, 'utf8')
|
||
|
const { component } = JSON.parse(source)
|
||
|
|
||
|
cache[f] = component
|
||
|
return component
|
||
|
}
|
||
|
|
||
|
export default readPage
|
||
|
export const cache = {}
|
||
|
|
||
|
readPage.cache = cache
|