@@ -3,18 +3,19 @@ var objectAssign = require('object-assign');
33var hashSum = require ( 'hash-sum' ) ;
44var compiler = require ( 'vue-template-compiler' ) ;
55
6+ var genId = require ( './lib/gen-id' ) ;
7+ var rewriteStyle = require ( './lib/style-rewriter' ) ;
68var compileTemplate = require ( './lib/template-compiler' ) ;
79var insertCSS = require ( './lib/insert-css' ) ;
810
911// exports
1012module . exports = function ( content , file , conf ) {
1113 var scriptStr = '' ;
12- var output , configs , vuecId , jsLang ;
14+ var output , configs , jsLang ;
1315
1416 // configs
1517 configs = objectAssign ( {
1618 extractCSS : true ,
17- cssScopedFlag : '__vuec__' ,
1819 cssScopedIdPrefix : '_v-' ,
1920 cssScopedHashType : 'sum' ,
2021 cssScopedHashLength : 8 ,
@@ -23,29 +24,19 @@ module.exports = function(content, file, conf) {
2324 runtimeOnly : false ,
2425 } , conf ) ;
2526
26- // replace scoped flag
27- function replaceScopedFlag ( str ) {
28- var reg = new RegExp ( '([^a-zA-Z0-9\-_])(' + configs . cssScopedFlag + ')([^a-zA-Z0-9\-_])' , 'g' ) ;
29- str = str . replace ( reg , function ( $0 , $1 , $2 , $3 ) {
30- return $1 + vuecId + $3 ;
31- } ) ;
32- return str ;
33- }
34-
3527 // 兼容content为buffer的情况
3628 content = content . toString ( ) ;
3729
38- // scope replace
39- if ( configs . cssScopedType == 'sum' ) {
40- vuecId = configs . cssScopedIdPrefix + hashSum ( file . subpath ) ;
41- } else {
42- vuecId = configs . cssScopedIdPrefix + fis . util . md5 ( file . subpath , configs . cssScopedHashLength ) ;
43- }
44- content = replaceScopedFlag ( content ) ;
45-
30+ // generate css scope id
31+ var id = configs . cssScopedIdPrefix + genId ( file , configs ) ;
4632 // parse
4733 var output = compiler . parseComponent ( content . toString ( ) , { pad : true } ) ;
4834
35+ // check for scoped style nodes
36+ var hasScopedStyle = output . styles . some ( function ( style ) {
37+ return style . scoped
38+ } ) ;
39+
4940 // script
5041 if ( output . script ) {
5142 scriptStr = output . script . content ;
@@ -65,6 +56,13 @@ module.exports = function(content, file, conf) {
6556 isJsLike : true
6657 } ) ;
6758
59+ scriptStr += '\nvar __vue__options__;\n' ;
60+ scriptStr += 'if(module.exports.__esModule && module.exports.default){\n' ;
61+ scriptStr += ' __vue__options__ = module.exports.default;\n' ;
62+ scriptStr += '}else{\n' ;
63+ scriptStr += ' __vue__options__ = module.exports;\n' ;
64+ scriptStr += '}\n' ;
65+
6866 if ( output . template ) {
6967 var templateContent = fis . compile . partial ( output . template . content , file , {
7068 ext : output . template . lang || 'html' ,
@@ -74,22 +72,22 @@ module.exports = function(content, file, conf) {
7472 if ( configs . runtimeOnly ) {
7573 var result = compileTemplate ( templateContent ) ;
7674 if ( result ) {
77- scriptStr += '\n;\n(function(renderFun, staticRenderFns){\n'
78- scriptStr += '\nif(module && module.exports){ module.exports.render=renderFun; module.exports.staticRenderFns=staticRenderFns;}\n' ;
79- scriptStr += '\nif(exports && exports.default){ exports.default.render=renderFun; exports.default.staticRenderFns=staticRenderFns;}\n' ;
80- scriptStr += '\n})(' + result . render + ',' + result . staticRenderFns + ');\n' ;
75+ scriptStr += '__vue__options__.render =' + result . render + '\n' ;
76+ scriptStr += '__vue__options__.staticRenderFns =' + result . staticRenderFns + '\n' ;
8177 }
8278 } else {
8379 // template
84- scriptStr += '\n;\n(function(template){\n'
85- scriptStr += '\nmodule && module.exports && (module.exports.template = template);\n' ;
86- scriptStr += '\nexports && exports.default && (exports.default.template = template);\n' ;
87- scriptStr += '\n})(' + JSON . stringify ( templateContent ) + ');\n' ;
80+ scriptStr += '__vue__options__.template = ' + JSON . stringify ( templateContent ) + '\n' ;
8881 }
8982 }
9083
84+ if ( hasScopedStyle ) {
85+ // template
86+ scriptStr += '__vue__options__._scopeId = ' + JSON . stringify ( id ) + '\n' ;
87+ }
88+
9189 // style
92- output [ ' styles' ] . forEach ( function ( item , index ) {
90+ output . styles . forEach ( function ( item , index ) {
9391 if ( ! item . content ) {
9492 return ;
9593 }
@@ -105,6 +103,8 @@ module.exports = function(content, file, conf) {
105103 isCssLike : true
106104 } ) ;
107105
106+ styleContent = rewriteStyle ( id , styleContent , item . scoped , { } )
107+
108108 if ( ! configs . extractCSS ) {
109109 scriptStr += '\n;(' + insertCSS + ')(' + JSON . stringify ( styleContent ) + ');\n' ;
110110 return ;
@@ -131,8 +131,5 @@ module.exports = function(content, file, conf) {
131131 file . addRequire ( styleFile . getId ( ) ) ;
132132 } ) ;
133133
134- // 处理一遍scoped css
135- scriptStr = replaceScopedFlag ( scriptStr ) ;
136-
137134 return scriptStr ;
138135} ;
0 commit comments