Skip to content

Commit ada438e

Browse files
author
season
committed
使用vue-template-compiler 替换parse5
1 parent cb80698 commit ada438e

File tree

7 files changed

+77
-99
lines changed

7 files changed

+77
-99
lines changed

index.js

Lines changed: 22 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
var path = require('path');
22
var chalk = require('chalk')
3-
var parse5 = require('parse5');
4-
var deindent = require('de-indent');
53
var objectAssign = require('object-assign');
64
var hashSum = require('hash-sum');
75
var compiler = require('vue-template-compiler');
86
var 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
259
module.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

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fis3-parser-vue-component",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"description": "A parser plugin for fis3 to parser vue component.",
55
"keywords": [
66
"fis3",
@@ -12,13 +12,10 @@
1212
"main": "index.js",
1313
"dependencies": {
1414
"chalk": "^1.1.3",
15-
"de-indent": "^1.0.2",
1615
"hash-sum": "^1.0.2",
1716
"object-assign": "^4.1.0",
18-
"parse5": "^2.1.5",
1917
"vue-template-compiler": "^2.1.6",
20-
"vue-template-es2015-compiler": "^1.4.0",
21-
"vue-template-validator": "^1.1.5"
18+
"vue-template-es2015-compiler": "^1.4.0"
2219
},
2320
"devDependencies": {},
2421
"scripts": {

test2/src/component/a.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<template>
1+
<template lang="html">
22
<div class="component-a" __vuec__>
33
Component A
44
</div>
55
</template>
66

7-
<script>
7+
<script lang="js">
88
module.exports = {
99
created() {
1010
console.log('component a created !');

test2/src/component/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ $blue : #1875e7; 
2929
<component-b></component-b>
3030
<component-c></component-c>
3131
<component-jade></component-jade>
32+
<test-bug></test-bug>
3233
</div>
3334
</template>
3435

3536
<script>
3637
import ComponentA from './a';
3738
import ComponentB from './b';
3839
import ComponentJade from './jade';
40+
import TestBug from './test-bug';
3941
4042
// @require '../less/other2.less';
4143
@@ -44,6 +46,7 @@ $blue : #1875e7; 
4446
ComponentA,
4547
ComponentB,
4648
ComponentJade,
49+
TestBug,
4750
ComponentC: require('./c')
4851
},
4952
created() {

test2/src/component/test-bug.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<my-table>
3+
<my-td></my-td>
4+
</my-table>
5+
</template>
6+
7+
<script>
8+
import MyTable from './test-table';
9+
import MyTd from './test-td';
10+
export default {
11+
components: {
12+
MyTable,
13+
MyTd
14+
}
15+
}
16+
</script>
17+
18+
<style lang="css">
19+
</style>

test2/src/component/test-table.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template lang="html">
2+
<table>
3+
<thead>
4+
<tr>
5+
<slot></slot>
6+
</tr>
7+
</thead>
8+
</table>
9+
</template>
10+
11+
<script>
12+
export default {
13+
14+
}
15+
</script>
16+
17+
<style lang="css">
18+
</style>

test2/src/component/test-td.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template lang="html">
2+
<td>ttttest-td</td>
3+
</template>
4+
5+
<script>
6+
export default {
7+
}
8+
</script>
9+
10+
<style lang="css">
11+
</style>

0 commit comments

Comments
 (0)