|
1 | 1 | var path = require('path'); |
2 | | -var chalk = require('chalk') |
3 | 2 | var objectAssign = require('object-assign'); |
4 | 3 | var hashSum = require('hash-sum'); |
5 | 4 | var compiler = require('vue-template-compiler'); |
6 | | -var transpile = require('vue-template-es2015-compiler'); |
| 5 | + |
| 6 | +var compileTemplate = require('./lib/template-compiler'); |
| 7 | +var insertCSS = require('./lib/insert-css'); |
7 | 8 |
|
8 | 9 | // exports |
9 | 10 | module.exports = function(content, file, conf) { |
10 | 11 | var scriptStr = ''; |
11 | | - var templateFileName, templateFile, templateContent; |
12 | | - var fragment, output, configs, vuecId, jsLang; |
| 12 | + var output, configs, vuecId, jsLang; |
13 | 13 |
|
14 | 14 | // configs |
15 | 15 | configs = objectAssign({ |
| 16 | + extractCSS: true, |
16 | 17 | cssScopedFlag: '__vuec__', |
17 | 18 | cssScopedIdPrefix: '_v-', |
18 | 19 | cssScopedHashType: 'sum', |
@@ -64,89 +65,70 @@ module.exports = function(content, file, conf) { |
64 | 65 | isJsLike: true |
65 | 66 | }); |
66 | 67 |
|
67 | | - // template |
68 | | - if (configs.runtimeOnly) { |
| 68 | + if(output.template){ |
| 69 | + var templateContent = fis.compile.partial(output.template.content, file, { |
| 70 | + ext: output.template.lang || 'html', |
| 71 | + isHtmlLike: true |
| 72 | + }); |
69 | 73 | // runtimeOnly |
70 | | - |
71 | | - function toFunction (code) { |
72 | | - // console.log(code); |
73 | | - return transpile('function render () {' + code + '}') |
74 | | - } |
75 | | - |
76 | | - if (output.template) { |
77 | | - templateContent = fis.compile.partial(output.template.content, file, { |
78 | | - ext: output.template.lang || 'html', |
79 | | - isHtmlLike: true |
80 | | - }); |
81 | | - |
82 | | - var compiled = compiler.compile(templateContent); |
83 | | - var renderFun, staticRenderFns; |
84 | | - |
85 | | - if (compiled.errors.length) { |
86 | | - compiled.errors.forEach(function (err) { |
87 | | - console.error('\n' + chalk.red(err) + '\n') |
88 | | - }); |
89 | | - throw new Error('Vue template compilation failed'); |
90 | | - } else { |
91 | | - renderFun = toFunction(compiled.render); |
92 | | - staticRenderFns = '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'; |
| 74 | + if(configs.runtimeOnly){ |
| 75 | + var result = compileTemplate(templateContent); |
| 76 | + 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'; |
93 | 81 | } |
94 | | - } else { |
95 | | - renderFun = 'function(){}'; |
96 | | - staticRenderFns = '[]'; |
97 | | - } |
98 | | - |
99 | | - scriptStr += '\n;\n(function(renderFun, staticRenderFns){\n' |
100 | | - scriptStr += '\nif(module && module.exports){ module.exports.render=renderFun; module.exports.staticRenderFns=staticRenderFns;}\n'; |
101 | | - scriptStr += '\nif(exports && exports.default){ exports.default.render=renderFun; exports.default.staticRenderFns=staticRenderFns;}\n'; |
102 | | - scriptStr += '\n})(' + renderFun + ',' + staticRenderFns + ');\n'; |
103 | | - } else { |
104 | | - // template |
105 | | - if (output.template) { |
106 | | - templateContent = fis.compile.partial(output.template.content, file, { |
107 | | - ext: output.template.lang || 'html', |
108 | | - isHtmlLike: true |
109 | | - }); |
110 | | - |
| 82 | + }else{ |
| 83 | + // template |
111 | 84 | scriptStr += '\n;\n(function(template){\n' |
112 | 85 | scriptStr += '\nmodule && module.exports && (module.exports.template = template);\n'; |
113 | 86 | scriptStr += '\nexports && exports.default && (exports.default.template = template);\n'; |
114 | 87 | scriptStr += '\n})(' + JSON.stringify(templateContent) + ');\n'; |
115 | | - } else { |
116 | | - scriptStr += '\nmodule && module.exports && (module.exports.template = "");\n'; |
117 | | - scriptStr += '\nexports && exports.default && (exports.default.template = "");\n'; |
118 | 88 | } |
119 | 89 | } |
120 | 90 |
|
121 | 91 | // style |
122 | 92 | output['styles'].forEach(function(item, index) { |
123 | | - if (item.content) { |
124 | | - var styleFileName, styleFile, styleContent; |
| 93 | + if(!item.content){ |
| 94 | + return; |
| 95 | + } |
125 | 96 |
|
126 | | - if (output['styles'].length == 1) { |
127 | | - styleFileName = file.realpathNoExt + configs.styleNameJoin + '.css'; |
128 | | - } else { |
129 | | - styleFileName = file.realpathNoExt + configs.styleNameJoin + '-' + index + '.css'; |
130 | | - } |
| 97 | + // empty string, or all space line |
| 98 | + if(/^\s*$/.test(item.content)){ |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // css也采用片段编译,更好的支持less、sass等其他语言 |
| 103 | + var styleContent = fis.compile.partial(item.content, file, { |
| 104 | + ext: item.lang || 'css', |
| 105 | + isCssLike: true |
| 106 | + }); |
131 | 107 |
|
132 | | - styleFile = fis.file.wrap(styleFileName); |
133 | | - |
134 | | - // css也采用片段编译,更好的支持less、sass等其他语言 |
135 | | - styleContent = fis.compile.partial(item.content, file, { |
136 | | - ext: item.lang || 'css', |
137 | | - isCssLike: true |
138 | | - }); |
139 | | - |
140 | | - styleFile.cache = file.cache; |
141 | | - styleFile.isCssLike = true; |
142 | | - styleFile.setContent(styleContent); |
143 | | - fis.compile.process(styleFile); |
144 | | - styleFile.links.forEach(function(derived) { |
145 | | - file.addLink(derived); |
146 | | - }); |
147 | | - file.derived.push(styleFile); |
148 | | - file.addRequire(styleFile.getId()); |
| 108 | + if(!configs.extractCSS){ |
| 109 | + scriptStr += '\n;(' + insertCSS + ')(' + JSON.stringify(styleContent) + ');\n'; |
| 110 | + return; |
149 | 111 | } |
| 112 | + |
| 113 | + var styleFileName, styleFile; |
| 114 | + |
| 115 | + if (output['styles'].length == 1) { |
| 116 | + styleFileName = file.realpathNoExt + configs.styleNameJoin + '.css'; |
| 117 | + } else { |
| 118 | + styleFileName = file.realpathNoExt + configs.styleNameJoin + '-' + index + '.css'; |
| 119 | + } |
| 120 | + |
| 121 | + styleFile = fis.file.wrap(styleFileName); |
| 122 | + |
| 123 | + styleFile.cache = file.cache; |
| 124 | + styleFile.isCssLike = true; |
| 125 | + styleFile.setContent(styleContent); |
| 126 | + fis.compile.process(styleFile); |
| 127 | + styleFile.links.forEach(function(derived) { |
| 128 | + file.addLink(derived); |
| 129 | + }); |
| 130 | + file.derived.push(styleFile); |
| 131 | + file.addRequire(styleFile.getId()); |
150 | 132 | }); |
151 | 133 |
|
152 | 134 | // 处理一遍scoped css |
|
0 commit comments