-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
144 lines (119 loc) · 3.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var path = require('path');
var objectAssign = require('object-assign');
var hashSum = require('hash-sum');
var compiler = require('vue-template-compiler');
var genId = require('./lib/gen-id');
var rewriteStyle = require('./lib/style-rewriter');
var compileTemplate = require('./lib/template-compiler');
var insertCSS = require('./lib/insert-css');
var replaceScopedFlag = require('./lib/replace-scoped-flag');
// exports
module.exports = function(content, file, conf) {
var scriptStr = '';
var output, configs, jsLang;
// configs
configs = objectAssign({
cssScopedFlag: '__vuec__',
extractCSS: true,
cssScopedIdPrefix: '_v-',
cssScopedHashType: 'sum',
cssScopedHashLength: 8,
styleNameJoin: '',
runtimeOnly: false,
}, conf);
// 兼容content为buffer的情况
content = content.toString();
// generate css scope id
var id = configs.cssScopedIdPrefix + genId(file, configs);
// 兼容旧的cssScopedFlag
if (configs.cssScopedFlag) {
content = replaceScopedFlag(content, configs.cssScopedFlag, id);
}
// parse
var output = compiler.parseComponent(content.toString(), { pad: true });
// check for scoped style nodes
var hasScopedStyle = output.styles.some(function (style) {
return style.scoped
});
// script
if (output.script) {
scriptStr = output.script.content;
jsLang = output.script.lang || 'js';
} else {
scriptStr += 'module.exports = {}';
jsLang = 'js';
}
// 部分内容以 js 的方式编译一次。如果要支持 cooffe 需要这么配置。
// 只针对预编译语言
// fis.match('*.vue:cooffe', {
// parser: fis.plugin('cooffe')
// })
scriptStr = fis.compile.partial(scriptStr, file, {
ext: jsLang,
isJsLike: true
});
scriptStr += '\nvar __vue__options__;\n';
scriptStr += 'if(exports && exports.__esModule && exports.default){\n';
scriptStr += ' __vue__options__ = exports.default;\n';
scriptStr += '}else{\n';
scriptStr += ' __vue__options__ = module.exports;\n';
scriptStr += '}\n';
if(output.template){
var templateContent = fis.compile.partial(output.template.content, file, {
ext: output.template.lang || 'html',
isHtmlLike: true
});
// runtimeOnly
if(configs.runtimeOnly){
var result = compileTemplate(templateContent);
if(result){
scriptStr += '__vue__options__.render =' + result.render + '\n';
scriptStr += '__vue__options__.staticRenderFns =' + result.staticRenderFns + '\n';
}
}else{
// template
scriptStr += '__vue__options__.template = ' + JSON.stringify(templateContent) + '\n';
}
}
if(hasScopedStyle){
// template
scriptStr += '__vue__options__._scopeId = ' + JSON.stringify(id) + '\n';
}
// style
output.styles.forEach(function(item, index) {
if(!item.content){
return;
}
// empty string, or all space line
if(/^\s*$/.test(item.content)){
return;
}
// css也采用片段编译,更好的支持less、sass等其他语言
var styleContent = fis.compile.partial(item.content, file, {
ext: item.lang || 'css',
isCssLike: true
});
styleContent = rewriteStyle(id, styleContent, item.scoped, {})
if(!configs.extractCSS){
scriptStr += '\n;(' + insertCSS + ')(' + JSON.stringify(styleContent) + ');\n';
return;
}
var styleFileName, styleFile;
if (output['styles'].length == 1) {
styleFileName = file.realpathNoExt + configs.styleNameJoin + '.css';
} else {
styleFileName = file.realpathNoExt + configs.styleNameJoin + '-' + index + '.css';
}
styleFile = fis.file.wrap(styleFileName);
styleFile.cache = file.cache;
styleFile.isCssLike = true;
styleFile.setContent(styleContent);
fis.compile.process(styleFile);
styleFile.links.forEach(function(derived) {
file.addLink(derived);
});
file.derived.push(styleFile);
file.addRequire(styleFile.getId());
});
return scriptStr;
};