-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
169 lines (145 loc) · 6.7 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
var fs = require('fs');
var less = require('less');
var parse5 = require('parse5');
var sass = require('sass');
var through = require('through2');
var PLUGIN_NAME = 'gulp-vue-single-file-component';
var LOG_PREFIX = '[' + PLUGIN_NAME + ']';
function getAttribute(node, name) {
var attr = node.attrs.filter(function(attr) {
return attr.name == name;
});
return attr.length ? attr[0].value : null;
}
function minify(input) {
return input.split('\n').map(function(line) {
return line.trim();
}).filter(function(line) {
return line != '';
}).join(' ') // Make sure it keeps a space (fixes https://github.com/nfplee/gulp-vue-single-file-component/issues/6).
.replace(/"/g, '\\"')
.trim();
}
/**
* This plugin compiles Vue single file components to plain Javascript.
*
* ## Usage:
* ```js
* gulp.task('vue', function() {
* return gulp.src('./js/components/*.vue')
* .pipe(vueComponent({ ...options })
* .pipe(gulp.dest('./dist/'));
* ```
* ## Options
* @param debug Display verbose output when building components.
* @param loadCssMethod Method used to load the component's CSS within the application.
* @param lessOptions Options passed to `less`. See the [`less` docs](https://lesscss.org/usage/#less-options).
* @param sassOptions Options passed to `sass`. See the [`sass` docs](https://sass-lang.com/documentation/js-api/interfaces/options/).
*/
module.exports = function(options) {
var defaults = {
debug: false,
loadCssMethod: 'require.loadCss',
lessOptions: {},
sassOptions: {},
};
var settings = Object.assign({}, defaults, options);
return through.obj(function(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
console.log(LOG_PREFIX, 'Cannot use streamed files');
return callback();
}
if (settings.debug) {
console.log(LOG_PREFIX, 'File =>', file.path);
}
// Parse the the file content and get the tag content.
var contents = file.contents.toString(encoding),
fragment = parse5.parseFragment(contents, {
locationInfo: true
}),
tagContent = [];
// Work out whether the component is a petite-vue component (since petite-vue exports a function instead of an object).
var isPetiteVue = contents.includes('export default function');
fragment.childNodes.forEach(function(node) {
if (node.tagName == 'style') {
var style = parse5.serialize(node),
lang = getAttribute(node, 'lang'),
href = getAttribute(node, 'href');
if (lang == 'less') {
var options = Object.assign({
compress: true
}, settings.lessOptions);
less.render(style, options, function(e, result) {
tagContent['style'] = '{ content: "' + minify(result.css) + '" }';
});
} else if (lang == 'sass' || lang == 'scss') {
var options = Object.assign({
style: 'compressed',
syntax: lang == 'sass' ? 'indented' : 'scss',
}, settings.sassOptions);
var result;
if (href) {
result = sass.compile(href, options);
} else {
result = sass.compileString(style, options);
}
tagContent['style'] = '{ content: "' + minify(result.css) + '" }';
} else {
if (href) {
tagContent['style'] = '{ url: \'' + href + '\' }';
} else {
tagContent['style'] = '{ content: "' + minify(style) + '" }';
}
}
} else if (node.tagName == 'template') {
var template,
include = getAttribute(node, 'include');
if (include) {
template = fs.readFileSync(include, 'utf-8');
} else {
//var treeAdapter = parse5.treeAdapters.default,
// docFragment = treeAdapter.createDocumentFragment();
//treeAdapter.appendChild(docFragment, node);
//template = parse5.serialize(docFragment);
//template = template.replace('<template>', '');
//template = template.substring(0, template.lastIndexOf('</template>'));
// parse5 lowercases any attributes in the template which causes problems when using scoped slots within the template.
template = contents.substring(contents.indexOf('<template>') + 10, contents.lastIndexOf('</template>'));
}
tagContent['template'] = minify(template);
} else if (node.tagName === 'script') {
tagContent['script'] = parse5.serialize(node);
}
});
// Build up the file content.
var content = tagContent['script'];
// Add a beforeCreate event to load the CSS (if applicable).
if (tagContent['style']) {
if (!isPetiteVue) {
if (!content.includes('beforeCreate()')) {
content = content.replace(/(export default [^{]*{)/, '$1\n beforeCreate() {\n ' + settings.loadCssMethod + '(' + tagContent['style'] + ');\n },');
}
} else {
content = content.replace(/export default(.*?)return {/s, 'export default$1' + settings.loadCssMethod + '(' + tagContent['style'] + ');\n return {');
}
}
// Add the template (if applicable).
if (tagContent['template']) {
// Note: Using " intead of ` allows us to use template literals e.g. <button :id="`my-dynamic-id-${id}`">Text</button>. However we must make sure the template has removed all line breaks.
if (!isPetiteVue) {
if (!content.includes('template:')) {
content = content.replace(/(export default [^{]*{)/, '$1\n template: "' + tagContent['template'] + '",');
}
} else {
if (!content.includes('$template:')) {
content = content.replace(/(export default.*?return {)/s, '$1\n $template: "' + tagContent['template'] + '",');
}
}
}
file.contents = new Buffer(content);
callback(null, file);
});
}