This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
234 lines (188 loc) · 9.38 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var fs = require('fs');
var path = require("path");
var parse5 = require('parse5');
var gutil = require('gulp-util');
var through = require("through2");
var sass = require("node-sass");
var PLUGIN_NAME = 'gulp-vue-module';
var LOG_PREFIX = '[' + PLUGIN_NAME + '] ';
function getAttribute (node, name) {
if (node.attrs) {
var i = node.attrs.length, attr;
while (i--) {
attr = node.attrs[i];
if (attr.name === name) {
return attr.value;
}
}
}
}
module.exports = function(options) {
var defaults = {
debug : false, // Debug mode
amd : false, // AMD style, Define module name and deps
define : true, // Using define() wrapper the module, false for Node.js (CommonJS style)
defineName : false, // Define the module name
indent : ' ', // Indent whitespace
headerComment : true, // Using <header-comment> Insert the header comments
templateReplaceTag : '__template__', // vue component template replace tag
loadCSSMethod : 'require.loadCSS', // define the load css method for require
externalRequire : false // don't pass require as a parameter
};
var settings = Object.assign({}, defaults, options),
debug = settings.debug,
indent = settings.indent,
templateReplaceTag = settings.templateReplaceTag;
return through.obj(function (file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Cannot use streamed files'));
return callback();
}
if (file.isBuffer()) {
if (debug) {
console.log(LOG_PREFIX, "target =>", file.path);
}
var content = file.contents.toString(encoding),
fragment = parse5.parseFragment(content, {
locationInfo: true
});
var tags = {},
contents = {
script : [],
style : [],
template : []
},
moduleName = '',
moduleDeps = '',
headerComment = '',
includeFileName,
scriptEmpty = false,
componentTags = ['template', 'style', 'script'];
fragment.childNodes.forEach(function (node) {
var type = node.tagName;
var lang = getAttribute(node, 'lang');
var href = getAttribute(node, 'href');
var src = getAttribute(node, 'src');
if (type === "header-comment") {
headerComment = parse5.serialize(node);
}
if (componentTags.indexOf(type) >= 0) {
tags[type] = true;
if (type === "style") {
var style = parse5.serialize(node);
if (!lang || lang === "css") {
style.split("\n").forEach(function(line){
if (line) contents.style.push(line.trim());
});
style = contents.style.join("");
if (style != "") {
contents.style = '{content : "' + style + '"}';
}
if (href && href !== "") {
contents.style = '{url : "' + href + '"}';
}
}
else if (lang && (lang === "sass" || lang === "scss")) {
contents.style = [];
style.split("\n").forEach(function(line){
if (line) contents.style.push(line);
});
var result,
sassRenderOptions = {
outputStyle : "compressed",
indentedSyntax : (lang === "sass") ? true : false,
};
if (href) {
sassRenderOptions.file = href;
} else {
sassRenderOptions.data = contents.style.join("\n");
}
result = sass.renderSync(sassRenderOptions);
result = result.css.toString().replace("\n", "");
if (result !== "") {
contents.style = '{content : "' + result + '"}';
}
}
}
if (type === "template") {
includeFileName = getAttribute(node, 'include');
if (includeFileName) {
var tpl = fs.readFileSync(includeFileName, 'utf-8');
if (!tpl) {
console.error(LOG_PREFIX, "read template file error =>", includeFileName);
}
} else {
var treeAdapter = parse5.treeAdapters.default,
docFragment = treeAdapter.createDocumentFragment();
treeAdapter.appendChild(docFragment, node);
var tpl = parse5.serialize(docFragment);
tpl = tpl.replace('<template>', '').replace('</template>', '');
}
tpl.split("\n").forEach(function(line){
if (line) contents.template.push(line.trim());
});
contents.template = contents.template.join("").toString().replace(/'/g, "'");
}
if (type === "script") {
moduleName = getAttribute(node, 'module-name');
moduleDeps = getAttribute(node, 'module-deps');
var script = parse5.serialize(node);
if (script.split("\n").length <= 2) {
scriptEmpty = true;
script = indent + "module.exports = {\n" + indent + indent + "template : '" + templateReplaceTag + "'\n" + indent + "};\n";
}
script.split("\n").forEach(function(line){
if (line.trim() != "") {
line = line.replace(new RegExp(templateReplaceTag, "gi"), contents.template);
contents.script.push(line);
}
});
}
}
});
if (settings.headerComment) {
headerComment = headerComment.replace("\n", '');
} else {
headerComment = '';
}
var script = contents.script.join("\n"),
deps = '',
loadCSS = '',
defineName = '',
moduleContent = '';
if (typeof contents.style === "string" && contents.style != "") {
loadCSS = indent + settings.loadCSSMethod + '('+contents.style+');\n\n';
}
if (settings.defineName && moduleName) {
defineName = '"' + moduleName + '", ';
}
if (settings.amd && moduleDeps) {
deps = [];
moduleDeps.split(/\s*,\s*/).forEach(function(dep){
deps.push('"' + dep + '"');
});
deps = "[" + deps.join(", ") + "], ";
}
if (settings.define) {
if (settings.externalRequire) {
moduleContent = 'define(' + defineName + deps + 'function('+ moduleDeps +') {\n' + loadCSS + script+'\n});';
} else {
moduleContent = 'define(' + defineName + deps + 'function(require, exports, module) {\n' + loadCSS + script+'\n});';
}
} else {
moduleContent = script;
}
script = headerComment + moduleContent;
content = script;
if (!tags.script || !tags.template) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, file.path + ' not vue component file, not have script and template tag'));
return callback();
}
file.contents = new Buffer(content);
}
callback(null, file);
});
}