refactor(ssr): path => context

This commit is contained in:
qingwei.li 2017-05-30 04:27:49 +08:00 • committed by cinwell.li
commit d4f6072d10
5 changed files with 88 additions and 39 deletions

View file

@ -28,12 +28,12 @@ function mainTpl (config) {
export default class Renderer {
constructor ({
template,
path,
context,
config,
cache
}) {
this.html = template
this.path = cwd(path || './')
this.context = cwd(context || './')
this.config = config = Object.assign({}, config, {
routerMode: 'history'
})
@ -54,7 +54,7 @@ export default class Renderer {
return isAbsolutePath(file)
? file
: cwd(this.path, `./${file}`)
: cwd(this.context, `./${file}`)
}
async renderToString (url) {
@ -114,6 +114,12 @@ export default class Renderer {
async _loadFile (filePath) {
try {
this.lock = this.lock || 0
if (++this.lock > 10) {
this.lock = 0
return
}
if (isAbsolutePath(filePath)) {
const res = await fetch(filePath)
return await res.text()
@ -124,7 +130,7 @@ export default class Renderer {
const fileName = basename(filePath)
const parentPath = cwd(filePath, '../..')
if (this.path.length < parentPath.length) {
if (this.context.length < parentPath.length) {
throw Error(`Not found file ${fileName}`)
}