@umajs/plugin-vue-ssr
Version:
In umajs, Vue is used to develop the plug-in of spa and MPa, which supports server-side rendering and client-side rendering
77 lines (76 loc) • 3.27 kB
JavaScript
;
var _a, _b, _c, _d;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Result = void 0;
const core_1 = require("@umajs/core");
const engineSource = require("consolidate");
const getStream = require("get-stream");
const vue_1 = require("@srejs/vue");
class Result extends core_1.Result {
static vue(viewName, initProps, options) {
return new Result({
type: 'vueView',
data: {
viewName,
initProps,
options,
},
});
}
}
exports.Result = Result;
const NODE_ENV = (process.env && process.env.NODE_ENV) || 'development';
let SrejsInstance;
let opt = ((_a = core_1.Uma.config) === null || _a === void 0 ? void 0 : _a.ssr) || {};
const vueSsrPlugin = ((_c = (_b = core_1.Uma.config) === null || _b === void 0 ? void 0 : _b.plugin) === null || _c === void 0 ? void 0 : _c.vue) || ((_d = core_1.Uma.config) === null || _d === void 0 ? void 0 : _d.plugin['vue-ssr']);
if (vueSsrPlugin === null || vueSsrPlugin === void 0 ? void 0 : vueSsrPlugin.options) {
opt = vueSsrPlugin.options;
}
let defaultRouter = false;
if (opt.hasOwnProperty('defaultRouter')) {
defaultRouter = opt.defaultRouter;
}
try {
SrejsInstance = new vue_1.default(core_1.Uma.app, NODE_ENV === 'development', defaultRouter, opt);
}
catch (error) {
console.error(error);
}
const renderView = async (ctx, viewName, initProps, options) => {
var _a, _b, _c;
const mergeProps = Object.assign(ctx.state || {}, initProps);
let html = await SrejsInstance.render(ctx, viewName, mergeProps, options);
const viewPlugin = (_a = core_1.Uma.config) === null || _a === void 0 ? void 0 : _a.plugin.views;
if ((viewPlugin === null || viewPlugin === void 0 ? void 0 : viewPlugin.enable) && options.useEngine) {
const { opts } = viewPlugin.options;
const { map } = opts;
const engineName = map === null || map === void 0 ? void 0 : map.html;
console.assert(engineName, '@umajs/plugin-views must be setting; eg====> map:{html:"nunjucks"}');
const engine = engineSource[engineName];
const state = Object.assign(Object.assign({}, options), mergeProps);
if (typeof html === 'object' && html.readable && options.cache) {
html = await getStream(html);
}
const ssrReg = new RegExp(/<script[^>]*>window.__SSR_DATA__=([\s\S]*?)<\/script>/);
const placeholderStr = '<!--plAcehoLder-->';
const ssrScriptStr = ((_c = (_b = html.match) === null || _b === void 0 ? void 0 : _b.call(html, ssrReg)) === null || _c === void 0 ? void 0 : _c[0]) || '';
const renderHtml = html.replace(ssrReg, placeholderStr);
html = await engine.render(renderHtml, state);
html = html.replace(placeholderStr, ssrScriptStr);
}
ctx.type = 'text/html';
ctx.body = html;
};
exports.default = () => ({
results: {
async vueView(ctx, data) {
const { viewName, initProps = {}, options = {}, } = data;
await renderView(ctx, viewName, initProps, options);
},
},
context: {
async vue(viewName, initProps, options) {
await renderView(this, viewName, initProps, options);
},
},
});