From 816dc8f1cc483d356b826b9b7539ebfe5779697f Mon Sep 17 00:00:00 2001 From: hubcarl Date: Wed, 12 Dec 2018 20:52:21 +0800 Subject: [PATCH] feat: support asset --- app/extend/context.js | 17 +++++++++++++---- lib/engine.js | 23 +++++++++++++++++++++++ package.json | 1 + 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/extend/context.js b/app/extend/context.js index 49798dd..3ed25ef 100644 --- a/app/extend/context.js +++ b/app/extend/context.js @@ -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); + }, }; + diff --git a/lib/engine.js b/lib/engine.js index 659ed82..534a55a 100644 --- a/lib/engine.js +++ b/lib/engine.js @@ -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'); @@ -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) { @@ -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; diff --git a/package.json b/package.json index bf09a1a..b08c2f8 100644 --- a/package.json +++ b/package.json @@ -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" },