isv-page-json-to-xtpl
Version:
abs 将页面 json 转换成 html
73 lines (69 loc) • 2.84 kB
JavaScript
var _ = require('lodash');
var util = require('../../util');
var pageAssets = require('../page-assets');
module.exports = {
replaceSolutionAssets: function(xtpl, options) {
options = _.extend({ isDev: false }, options);
var framework = options.framework;
var formattedAssets = util.formatAssets(framework);
xtpl = xtpl.replace('{{frameworkCss}}', formattedAssets.css[0] || '');
if (formattedAssets.js.length > 0) {
// 用无版本号的js
var js = formattedAssets.js[0];
if (options.isDev) {
//框架调试,解除 combo
var jsUrls = js.split('??');
var jsNames = jsUrls[1].split(',');
var scripts = [];
jsNames.forEach(function(name) {
scripts.push('<script src="' + jsUrls[0] + name + '"></script>');
});
xtpl = xtpl.replace('<script src="{{frameworkJs}}"></script>', scripts.join('\n'));
} else {
xtpl = xtpl.replace('{{frameworkJs}}', js);
}
}
//种子资源的特殊处理
var seed = options.seed;
if(seed){
var seedContent = util.comboAssets(['require','babel-helper','appear','lib'],seed);
//向下兼容
if(/kg\/online-pi\/0\.0/.test(JSON.stringify(framework)) || /kg\/pi\/0\.9/.test(JSON.stringify(framework))){
seedContent = util.comboAssets(['kimi','require','kissy-mod-define','appear'],seed);
}
if(seedContent.js['g.alicdn.com']){
xtpl = xtpl.replace('{{frameworkSeedJs}}', seedContent.js['g.alicdn.com']);
}
//pc 页面特殊的注入
if(options.terminalType === 'pc'){
var fizAssetsContent = util.comboAssets(['fiz-assets'],seed);
if(fizAssetsContent.js['g.alicdn.com']){
xtpl = xtpl.replace('{{fizAssetsJs}}', fizAssetsContent.js['g.alicdn.com']);
}
if(fizAssetsContent.css['g.alicdn.com']){
xtpl = xtpl.replace('{{fizAssetsCss}}', fizAssetsContent.css['g.alicdn.com']);
}
}
}
return xtpl;
},
/**
* air 渲染后执行的方法
* 在线上不起作用,主要用于dev与预览环境
*/
afterAirRenderContentReplace: function*(content,airParams,option){
//调试信息
var seedInfo = '\n;console.log("seed info:");console.log(JSON.parse(\'' + JSON.stringify(airParams.page.seed) + '\'));';
if(!option.wh_chunk && !option.wh_block){
content +='<script>' + seedInfo +'</script>';
}else{
content += seedInfo;
}
var noExistAssetPaths = yield pageAssets.noExistAssetPaths(content);
//如果存在依赖的组件不存在的情况,抛出异常
if(noExistAssetPaths.length){
content = '<html><head><meta charset="utf8"></head><body><h3>页面找不到如下依赖资源,请检查依赖的组件:</h3><p>'+noExistAssetPaths+'</p></body></html>';
}
return content;
}
};