11var path = require ( 'path' ) ;
22var chalk = require ( 'chalk' )
3- var parse5 = require ( 'parse5' ) ;
4- var deindent = require ( 'de-indent' ) ;
53var objectAssign = require ( 'object-assign' ) ;
64var hashSum = require ( 'hash-sum' ) ;
75var compiler = require ( 'vue-template-compiler' ) ;
86var transpile = require ( 'vue-template-es2015-compiler' ) ;
9- var validateTemplate = require ( 'vue-template-validator' ) ;
10-
11- function getAttribute ( node , name ) {
12- if ( node . attrs ) {
13- var i = node . attrs . length ;
14- var attr ;
15- while ( i -- ) {
16- attr = node . attrs [ i ] ;
17- if ( attr . name === name ) {
18- return attr . value ;
19- }
20- }
21- }
22- }
237
248// exports
259module . exports = function ( content , file , conf ) {
@@ -38,6 +22,15 @@ module.exports = function(content, file, conf) {
3822 runtimeOnly : false ,
3923 } , conf ) ;
4024
25+ // replace scoped flag
26+ function replaceScopedFlag ( str ) {
27+ var reg = new RegExp ( '([^a-zA-Z0-9\-_])(' + configs . cssScopedFlag + ')([^a-zA-Z0-9\-_])' , 'g' ) ;
28+ str = str . replace ( reg , function ( $0 , $1 , $2 , $3 ) {
29+ return $1 + vuecId + $3 ;
30+ } ) ;
31+ return str ;
32+ }
33+
4134 // 兼容content为buffer的情况
4235 content = content . toString ( ) ;
4336
@@ -49,72 +42,13 @@ module.exports = function(content, file, conf) {
4942 }
5043 content = replaceScopedFlag ( content ) ;
5144
52- // replace scoped flag
53- function replaceScopedFlag ( str ) {
54- var reg = new RegExp ( '([^a-zA-Z0-9\-_])(' + configs . cssScopedFlag + ')([^a-zA-Z0-9\-_])' , 'g' ) ;
55- str = str . replace ( reg , function ( $0 , $1 , $2 , $3 ) {
56- return $1 + vuecId + $3 ;
57- } ) ;
58- return str ;
59- }
60-
6145 // parse
62- fragment = parse5 . parseFragment ( content . toString ( ) , {
63- locationInfo : true
64- } ) ;
65-
66- output = {
67- template : [ ] ,
68- style : [ ] ,
69- script : [ ]
70- } ;
71-
72- fragment . childNodes . forEach ( function ( node ) {
73- var type = node . tagName ;
74- var lang = getAttribute ( node , 'lang' ) ;
75- var src = getAttribute ( node , 'src' ) ;
76- var warnings = null ;
77- var content ;
78-
79- if ( ! output [ type ] ) return ;
80-
81- if ( type == 'style' && ( ! node . childNodes || ! node . childNodes . length ) ) {
82- return ;
83- }
84-
85- if ( ! lang ) {
86- if ( type == 'script' ) lang = 'js' ;
87- if ( type == 'style' ) lang = 'css' ;
88- if ( type == 'template' ) lang = 'html' ;
89- }
90-
91- if ( type == 'template' ) {
92- content = parse5 . serialize ( node . content ) ;
93- } else {
94- content = parse5 . serialize ( node ) ;
95- }
96-
97- content = deindent ( content ) ;
98- content = content . replace ( / ( ^ [ \r \n ] * ) | ( [ \r \n ] * $ ) / g, '' ) ;
99-
100- // node count check
101- if ( ( type === 'script' || type === 'template' ) && output [ type ] . length > 0 ) {
102- throw new Error (
103- '[fis3-parser-vue-component] Only one <script> or <template> tag is ' +
104- 'allowed inside a Vue component.'
105- )
106- } else {
107- output [ type ] . push ( {
108- content : content ,
109- lang : lang
110- } ) ;
111- }
112- } ) ;
46+ var output = compiler . parseComponent ( content . toString ( ) , { pad : true } ) ;
11347
11448 // script
115- if ( output [ 'script' ] . length ) {
116- scriptStr = output [ ' script' ] [ 0 ] . content ;
117- jsLang = output [ ' script' ] [ 0 ] . lang ;
49+ if ( output . script ) {
50+ scriptStr = output . script . content ;
51+ jsLang = output . script . lang || 'js' ;
11852 } else {
11953 scriptStr += 'module.exports = {}' ;
12054 jsLang = 'js' ;
@@ -129,9 +63,9 @@ module.exports = function(content, file, conf) {
12963 return transpile ( 'function render () {' + code + '}' )
13064 }
13165
132- if ( output [ 'template' ] . length ) {
133- templateContent = fis . compile . partial ( output [ ' template' ] [ 0 ] . content , file , {
134- ext : output [ ' template' ] [ 0 ] . lang ,
66+ if ( output . template ) {
67+ templateContent = fis . compile . partial ( output . template . content , file , {
68+ ext : output . template . lang || 'html' ,
13569 isHtmlLike : true
13670 } ) ;
13771
@@ -158,16 +92,12 @@ module.exports = function(content, file, conf) {
15892 scriptStr += '\n})(' + renderFun + ',' + staticRenderFns + ');\n' ;
15993 } else {
16094 // template
161- if ( output [ 'template' ] . length ) {
162- templateContent = fis . compile . partial ( output [ ' template' ] [ 0 ] . content , file , {
163- ext : output [ ' template' ] [ 0 ] . lang ,
95+ if ( output . template ) {
96+ templateContent = fis . compile . partial ( output . template . content , file , {
97+ ext : output . template . lang || 'html' ,
16498 isHtmlLike : true
16599 } ) ;
166100
167- validateTemplate ( output [ 'template' ] [ 0 ] . content ) . forEach ( function ( msg ) {
168- console . log ( msg ) ;
169- } )
170-
171101 scriptStr += '\n;\n(function(template){\n'
172102 scriptStr += '\nmodule && module.exports && (module.exports.template = template);\n' ;
173103 scriptStr += '\nexports && exports.default && (exports.default.template = template);\n' ;
@@ -188,11 +118,11 @@ module.exports = function(content, file, conf) {
188118 } ) ;
189119
190120 // style
191- output [ 'style ' ] . forEach ( function ( item , index ) {
121+ output [ 'styles ' ] . forEach ( function ( item , index ) {
192122 if ( item . content ) {
193123 var styleFileName , styleFile , styleContent ;
194124
195- if ( output [ 'style ' ] . length == 1 ) {
125+ if ( output [ 'styles ' ] . length == 1 ) {
196126 styleFileName = file . realpathNoExt + configs . styleNameJoin + '.css' ;
197127 } else {
198128 styleFileName = file . realpathNoExt + configs . styleNameJoin + '-' + index + '.css' ;
@@ -202,7 +132,7 @@ module.exports = function(content, file, conf) {
202132
203133 // css也采用片段编译,更好的支持less、sass等其他语言
204134 styleContent = fis . compile . partial ( item . content , file , {
205- ext : item . lang ,
135+ ext : item . lang || 'css' ,
206136 isCssLike : true
207137 } ) ;
208138
0 commit comments