Skip to content

Commit

Permalink
feat: support asset
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Dec 12, 2018
1 parent 0141763 commit 816dc8f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
'use strict';
module.exports = {
renderClient(name, locals, options) {
return this.renderVueClient(name, locals, options);
return this.renderVueClient(name, locals, options).then(html => {
this.body = html;
});
},
renderVueClient(name, locals, options = {}) {
locals = this.app.vue.normalizeLocals(this, locals, options, false);
return this.app.vue.renderClient(name, locals, options).then(html => {
renderAsset(name, locals, options) {
return this.renderVueAsset(name, locals, options).then(html => {
this.body = html;
});
},
renderVueAsset(name, locals, options) {
return this.app.vue.renderAsset(this, name, locals, options);
},
renderVueClient(name, locals, options = {}) {
locals = this.app.vue.normalizeLocals(this, locals, options, false);
return this.app.vue.renderClient(name, locals, options);
},
};

23 changes: 23 additions & 0 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const fs = require('fs');
const Vue = require('vue');
const LRU = require('lru-cache');
const serialize = require('serialize-javascript');
const vueServerRenderer = require('vue-server-renderer');
const Resource = require('server-side-render-resource');
const VUE_RESOURCE = Symbol('Application#VueResource');
Expand Down Expand Up @@ -56,6 +57,17 @@ class Engine {
return options.renderOptions && options.renderOptions.template || this.template;
}

getAsset(name, state) {
const manifest = this.resource && this.resource.manifest || {};
const deps = manifest.deps || {};
const res = deps[name] || {};
return {
js: res.js || [],
css: res.css || [],
state: serialize(state || {}, { isJSON: true })
};
}

normalizeLocals(ctx, locals = {}, options = {}, engine = true) {
// egg-view engine mode, the locals had merged
if (engine) {
Expand Down Expand Up @@ -142,6 +154,17 @@ class Engine {
return this.resource ? this.resource.inject(html, name, locals, options) : html;
});
}

renderAsset(ctx, name, locals, options = {}) {
const viewEngine = options.viewEngine || this.config.viewEngine || 'nunjucks';
// 输出到页面的 state 数据
const state = Object.assign({}, ctx.locals, locals);
const asset = this.getAsset(name, state);
const template = this.getTemplate(options);
const context = Object.assign({}, locals, { asset });
// egg-view 自动合并 ctx, request, response, helper
return ctx.renderString(template, context, { viewEngine });
}
}

module.exports = Engine;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"dependencies": {
"lru-cache": "^4.0.1",
"serialize-javascript": "^1.5.0",
"server-side-render-resource": "^1.0.3",
"vue-server-renderer": "^2.5.13"
},
Expand Down

0 comments on commit 816dc8f

Please sign in to comment.