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

43
build/release.sh Normal file
View file

@ -0,0 +1,43 @@
set -e
if [[ -z $1 ]]; then
echo "Enter new version: "
read VERSION
else
VERSION=$1
fi
read -p "Releasing $VERSION $RELEASE_TAG - are you sure? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Releasing $VERSION ..."
npm run lint
# build
VERSION=$VERSION npm run build
# update packages
cd packages/docsify-server-renderer
npm version $VERSION
if [[ -z $RELEASE_TAG ]]; then
npm publish
else
npm publish --tag $RELEASE_TAG
fi
cd -
# commit
git add -A
git commit -m "[build] $VERSION"
npm version $VERSION --message "[release] $VERSION"
# publish
git push origin refs/tags/v$VERSION
git push
if [[ -z $RELEASE_TAG ]]; then
npm publish
else
npm publish --tag $RELEASE_TAG
fi
fi

View file

@ -1,4 +1,5 @@
module.exports = { module.exports = {
config: {
alias: { alias: {
'/de-de/changelog': '/changelog', '/de-de/changelog': '/changelog',
'/zh-cn/changelog': '/changelog', '/zh-cn/changelog': '/changelog',
@ -26,4 +27,7 @@ module.exports = {
'/': 'Search' '/': 'Search'
} }
} }
},
context: './docs',
template: './ssr.html'
} }

View file

@ -20,15 +20,13 @@
"themes" "themes"
], ],
"scripts": { "scripts": {
"build": "rm -rf lib themes && node build/build && mkdir lib/themes && mkdir themes && node build/build-css", "build": "rm -rf lib themes && node build/build && mkdir lib/themes && mkdir themes && node build/build-css && npm run build:ssr",
"dev:build": "rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev", "dev:build": "rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev",
"dev": "node app & nodemon -w src -e js,css --exec 'npm run dev:build'", "dev": "node app & nodemon -w src -e js,css --exec 'npm run dev:build'",
"build:ssr": "node build/build-ssr", "build:ssr": "node build/build-ssr",
"test": "eslint {src,packages} --fix" "test": "eslint {src,packages} --fix",
}, "pub:next": "RELEASE_TAG=next sh build/release",
"docsify": { "pub": "sh build/release"
"path": "./",
"template": "./ssr.html"
}, },
"dependencies": { "dependencies": {
"marked": "^0.3.6", "marked": "^0.3.6",

View file

@ -11,17 +11,15 @@ yarn add docsify-server-render
```js ```js
var Renderer = require('docsify-server-renderer') var Renderer = require('docsify-server-renderer')
var readFileSync = require('fs').readFileSync var readFileSync = require('fs').readFileSync
var resolve = require('path').resolve
// init // init
var renderer = new Renderer({ var renderer = new Renderer({
template: readFileSync('./index.template.html', 'utf-8')., template: readFileSync('./docs/index.template.html', 'utf-8').,
path: resolve(_dirname, './docs'), context: './docs',
config: { config: {
name: 'docsify', name: 'docsify',
repo: 'qingwei-li/docsify' repo: 'qingwei-li/docsify'
} }
//,cache: () => {}
}) })
renderer.renderToString({ url }) renderer.renderToString({ url })

View file

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