|
| 1 | +import { IContext, TPlugin, Result as R, Uma, TPluginConfig } from '@umajs/core'; |
| 2 | +import * as engineSource from 'consolidate'; |
| 3 | +import * as getStream from 'get-stream'; |
| 4 | +import Srejs from '@srejs/vue'; |
| 5 | + |
| 6 | +interface TviewOptions{ |
| 7 | + ssr?: boolean, // 全局开启服务端渲染 |
| 8 | + cache?: boolean, // 全局使用服务端渲染缓存 |
| 9 | + useEngine?:boolean // 渲染自定义html的页面组件时,选择性开启使用模板引擎 |
| 10 | +} |
| 11 | + |
| 12 | +export interface TssrPluginOptions extends TviewOptions { |
| 13 | + rootDir?:string, // 客户端页面组件根文件夹 |
| 14 | + rootNode?:string, // 客户端页面挂载根元素ID |
| 15 | + defaultRouter?:boolean, // 开启默认文件路由 |
| 16 | + prefixCDN?:string, // 构建后静态资源CDN地址前缀 |
| 17 | + prefixRouter?:string // 默认页面路由前缀(在defaultRouter设置为true时有效) |
| 18 | +} |
| 19 | + |
| 20 | +interface IvueViewParms{ |
| 21 | + viewName :string; |
| 22 | + initProps : any, |
| 23 | + options:TviewOptions |
| 24 | +} |
| 25 | +export class Result<T> extends R<T> { |
| 26 | + /** |
| 27 | + * |
| 28 | + * @param viewName 页面标识 |
| 29 | + * @param initProps 初始化数据上下文(模板和组件props可直接接收),如果传递state则自动注入到vuex初始化state中 |
| 30 | + * @param options 页面组件标识 |
| 31 | + * @returns |
| 32 | + */ |
| 33 | + static vue(viewName: string, initProps?: any, options?:TviewOptions) { |
| 34 | + return new Result<IvueViewParms>({ |
| 35 | + type: 'vueView', |
| 36 | + data: { |
| 37 | + viewName, |
| 38 | + initProps, |
| 39 | + options, |
| 40 | + }, |
| 41 | + }); |
| 42 | + } |
| 43 | +} |
| 44 | +const NODE_ENV = (process.env && process.env.NODE_ENV) || 'development'; |
| 45 | +let SrejsInstance; |
| 46 | + |
| 47 | +/** 插件配置读取放到了@srejs/vue框架中进行兼容,在生产环境部署前构建阶段不会执行插件 */ |
| 48 | +let opt:TssrPluginOptions = Uma.config?.ssr || {}; // ssr.config.ts |
| 49 | +const vueSsrPlugin = <TPluginConfig>Uma.config?.plugin?.vue || <TPluginConfig>Uma.config?.plugin['vue-ssr']; |
| 50 | + |
| 51 | +if (vueSsrPlugin?.options) { |
| 52 | + opt = vueSsrPlugin.options; |
| 53 | +} |
| 54 | + |
| 55 | +let defaultRouter = false; |
| 56 | + |
| 57 | +// eslint-disable-next-line no-prototype-builtins |
| 58 | +if (opt.hasOwnProperty('defaultRouter')) { |
| 59 | + defaultRouter = opt.defaultRouter; |
| 60 | +} |
| 61 | + |
| 62 | +try { |
| 63 | + SrejsInstance = new Srejs(Uma.app, NODE_ENV === 'development', defaultRouter, opt); |
| 64 | +} catch (error) { |
| 65 | + console.error(error); |
| 66 | +} |
| 67 | + |
| 68 | +const renderView = async (ctx:IContext, viewName:string, initProps?:any, options?:TviewOptions) => { |
| 69 | + let html = await SrejsInstance.render(ctx, viewName, initProps, options); |
| 70 | + |
| 71 | + const viewPlugin = <TPluginConfig>Uma.config?.plugin.views; // use @umajs/plugin-views |
| 72 | + |
| 73 | + if (viewPlugin?.enable && options.useEngine) { |
| 74 | + const { opts } = viewPlugin.options; |
| 75 | + const { map } = opts; |
| 76 | + const engineName = map?.html; |
| 77 | + |
| 78 | + console.assert(engineName, '@umajs/plugin-views must be setting; eg====> map:{html:"nunjucks"}'); |
| 79 | + const engine = engineSource[engineName]; |
| 80 | + const state = { ...options, ...ctx.state || {}, ...initProps }; |
| 81 | + |
| 82 | + if (typeof html === 'object' && html.readable && options.cache) { |
| 83 | + // when cache model ,html return a file stream |
| 84 | + html = await getStream(html); |
| 85 | + } |
| 86 | + |
| 87 | + html = await engine.render(html, state); |
| 88 | + } |
| 89 | + |
| 90 | + ctx.type = 'text/html'; |
| 91 | + ctx.body = html; |
| 92 | +}; |
| 93 | + |
| 94 | +export default (): TPlugin => ({ |
| 95 | + results: { |
| 96 | + async vueView(ctx: IContext, data: IvueViewParms) { |
| 97 | + const { |
| 98 | + viewName, |
| 99 | + initProps = {}, |
| 100 | + options = {}, |
| 101 | + } = data; |
| 102 | + |
| 103 | + await renderView(ctx, viewName, initProps, options); |
| 104 | + }, |
| 105 | + }, |
| 106 | + context: { |
| 107 | + async vue(viewName:string, initProps?:any, options?:TviewOptions) { |
| 108 | + await renderView(this, viewName, initProps, options); |
| 109 | + }, |
| 110 | + }, |
| 111 | + |
| 112 | +}); |
0 commit comments