@@ -8,6 +8,7 @@ const compileCoffeeScript = require('./compilers/coffee-compiler')
88const extractPropsFromFunctionalTemplate = require ( './extract-props' )
99const fs = require ( 'fs' )
1010const join = require ( 'path' ) . join
11+ const cssExtract = require ( 'extract-from-css' )
1112
1213const splitRE = / \r ? \n / g
1314
@@ -27,6 +28,25 @@ function processScript (scriptPart) {
2728 return compileBabel ( scriptPart . content )
2829}
2930
31+ function processStyle ( stylePart ) {
32+ if ( ! stylePart ) return { }
33+
34+ let cssCode = stylePart . content
35+ if ( / ^ s t y l | s t y l u s $ / . test ( stylePart . lang ) ) {
36+ const stylus = require ( 'stylus' )
37+ cssCode = stylus . render ( stylePart . content )
38+ }
39+
40+ const cssNames = cssExtract . extractClasses ( cssCode )
41+ const obj = { }
42+
43+ for ( let i = 0 , l = cssNames . length ; i < l ; i ++ ) {
44+ obj [ cssNames [ i ] ] = cssNames [ i ]
45+ }
46+
47+ return obj
48+ }
49+
3050function changePartsIfFunctional ( parts ) {
3151 const isFunctional = parts . template && parts . template . attrs && parts . template . attrs . functional
3252 if ( isFunctional ) {
@@ -78,5 +98,20 @@ module.exports = function (src, path) {
7898 }
7999 }
80100
101+ if ( Array . isArray ( parts . styles ) && parts . styles . length > 0 ) {
102+ const styleStr = parts . styles . map ( ast => {
103+ const moduleName = ast . module || '$style'
104+ const styleObj = processStyle ( ast )
105+
106+ return '\n this[\'' + moduleName + '\'] = ' + JSON . stringify ( styleObj )
107+ } )
108+
109+ output += '\n;(function() {' +
110+ '\nvar render = __vue__options__.render' +
111+ '\n__vue__options__.render = function(h) {' + styleStr +
112+ '\n return render.call(this, h)' +
113+ '\n}})()'
114+ }
115+
81116 return { code : output , map }
82117}
0 commit comments