11var path = require ( 'path' ) ;
2+ var chalk = require ( 'chalk' )
23var parse5 = require ( 'parse5' ) ;
3- var validateTemplate = require ( 'vue-template-validator' ) ;
44var deindent = require ( 'de-indent' ) ;
55var objectAssign = require ( 'object-assign' ) ;
66var hashSum = require ( 'hash-sum' ) ;
7+ var compiler = require ( 'vue-template-compiler' ) ;
8+ var transpile = require ( 'vue-template-es2015-compiler' ) ;
9+ var validateTemplate = require ( 'vue-template-validator' ) ;
710
811function getAttribute ( node , name ) {
912 if ( node . attrs ) {
@@ -30,7 +33,9 @@ module.exports = function(content, file, conf) {
3033 cssScopedIdPrefix : '_v-' ,
3134 cssScopedHashType : 'sum' ,
3235 cssScopedHashLength : 8 ,
33- styleNameJoin : ''
36+ styleNameJoin : '' ,
37+
38+ runtimeOnly : false ,
3439 } , conf ) ;
3540
3641 // 兼容content为buffer的情况
@@ -116,23 +121,61 @@ module.exports = function(content, file, conf) {
116121 }
117122
118123 // template
119- if ( output [ 'template' ] . length ) {
120- templateContent = fis . compile . partial ( output [ 'template' ] [ 0 ] . content , file , {
121- ext : output [ 'template' ] [ 0 ] . lang ,
122- isHtmlLike : true
123- } ) ;
124+ if ( configs . runtimeOnly ) {
125+ // runtimeOnly
126+
127+ function toFunction ( code ) {
128+ // console.log(code);
129+ return transpile ( 'function render () {' + code + '}' )
130+ }
131+
132+ if ( output [ 'template' ] . length ) {
133+ templateContent = fis . compile . partial ( output [ 'template' ] [ 0 ] . content , file , {
134+ ext : output [ 'template' ] [ 0 ] . lang ,
135+ isHtmlLike : true
136+ } ) ;
137+
138+ var compiled = compiler . compile ( templateContent ) ;
139+ var renderFun , staticRenderFns ;
124140
125- validateTemplate ( output [ 'template' ] [ 0 ] . content ) . forEach ( function ( msg ) {
126- console . log ( msg )
127- } )
141+ if ( compiled . errors . length ) {
142+ compiled . errors . forEach ( function ( err ) {
143+ console . error ( '\n' + chalk . red ( err ) + '\n' )
144+ } ) ;
145+ throw new Error ( 'Vue template compilation failed' ) ;
146+ } else {
147+ renderFun = toFunction ( compiled . render ) ;
148+ staticRenderFns = '[' + compiled . staticRenderFns . map ( toFunction ) . join ( ',' ) + ']' ;
149+ }
150+ } else {
151+ renderFun = 'function(){}' ;
152+ staticRenderFns = '[]' ;
153+ }
128154
129- scriptStr += '\n;\n(function(template ){\n'
130- scriptStr += '\nmodule && module.exports && ( module.exports.template = template); \n' ;
131- scriptStr += '\nexports && exports.default && ( exports.default.template = template); \n' ;
132- scriptStr += '\n})(' + JSON . stringify ( templateContent ) + ');\n' ;
155+ scriptStr += '\n;\n(function(renderFun, staticRenderFns ){\n'
156+ scriptStr += '\nif(module && module.exports){ module.exports.render=renderFun; module.exports.staticRenderFns=staticRenderFns;} \n' ;
157+ scriptStr += '\nif(exports && exports.default){ exports.default.render=renderFun; exports.default.staticRenderFns=staticRenderFns;} \n' ;
158+ scriptStr += '\n})(' + renderFun + ',' + staticRenderFns + ');\n' ;
133159 } else {
134- scriptStr += '\nmodule && module.exports && (module.exports.template = "");\n' ;
135- scriptStr += '\nexports && exports.default && (exports.default.template = "");\n' ;
160+ // template
161+ if ( output [ 'template' ] . length ) {
162+ templateContent = fis . compile . partial ( output [ 'template' ] [ 0 ] . content , file , {
163+ ext : output [ 'template' ] [ 0 ] . lang ,
164+ isHtmlLike : true
165+ } ) ;
166+
167+ validateTemplate ( output [ 'template' ] [ 0 ] . content ) . forEach ( function ( msg ) {
168+ console . log ( msg ) ;
169+ } )
170+
171+ scriptStr += '\n;\n(function(template){\n'
172+ scriptStr += '\nmodule && module.exports && (module.exports.template = template);\n' ;
173+ scriptStr += '\nexports && exports.default && (exports.default.template = template);\n' ;
174+ scriptStr += '\n})(' + JSON . stringify ( templateContent ) + ');\n' ;
175+ } else {
176+ scriptStr += '\nmodule && module.exports && (module.exports.template = "");\n' ;
177+ scriptStr += '\nexports && exports.default && (exports.default.template = "");\n' ;
178+ }
136179 }
137180
138181 // 部分内容以 js 的方式编译一次。如果要支持 es6 需要这么配置。
0 commit comments