@@ -45,10 +45,6 @@ module.exports = function(source) {
4545 // 获取vue组件示例的代码
4646 const nextIndex = tokens [ index + 1 ] ;
4747 let content = nextIndex . type === "fence" ? nextIndex . content : "" ;
48- if ( ! / ^ < t e m p l a t e > / . test ( content ) ) {
49- content = `<template><div>${ content } </div></template>` ;
50- }
51-
5248 // 将content解析为vue组件基本属性对象;
5349 let { template, script, styles } = parse ( {
5450 source : content ,
@@ -57,10 +53,15 @@ module.exports = function(source) {
5753 } ) ;
5854 styleCodeList = styleCodeList . concat ( styles ) ;
5955 // 将template的转为render函数
60- const { code } = compileTemplate ( {
61- source : template . content ,
62- compiler : VueTemplateComplier
63- } ) ;
56+ let templateCodeRender = "" ;
57+ if ( template && template . content ) {
58+ const { code } = compileTemplate ( {
59+ source : template . content ,
60+ compiler : VueTemplateComplier
61+ } ) ;
62+ templateCodeRender = code ;
63+ }
64+
6465 // 获取script的代码
6566 script = script ? script . content : "" ;
6667 if ( script ) {
@@ -74,12 +75,12 @@ module.exports = function(source) {
7475 const name = `vc-snippent-${ componentCodeList . length } ` ;
7576 // 渲染组件代码添加到数据集合
7677 componentCodeList . push ( `"${ name } ":(function () {
77- ${ code }
78+ ${ templateCodeRender }
7879 ${ script }
7980 return {
8081 ...exportJavaScript,
81- render,
82- staticRenderFns
82+ ${ templateCodeRender ? " render," : "" }
83+ ${ templateCodeRender ? " staticRenderFns," : "" }
8384 }
8485 })()` ) ;
8586 // 将需要渲染的示例用vc-snippet组件包裹替换插槽显示示例效果
0 commit comments