-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (170 loc) · 5.65 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
/**
* gulp-plugin-vue
* Gulp plugin for compiling Vue single file components
*
* @see https://github.com/vuejs/vue-loader
* @see https://github.com/vuejs/component-compiler-utils
* @see https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler
*
* sobird<[email protected]> at 2020/10/10 13:12:44 created.
*/
const through = require('through2');
const hash = require('hash-sum');
const log = require('loglevel');
const { parse, compileTemplate, compileStyle } = require('@vue/component-compiler-utils');
const compiler = require('vue-template-compiler');
const vfs = require('vinyl-fs');
module.exports = function (options) {
options = Object.assign({
runtime: true,
outputSourceRange: false
}, options);
return through.obj(async function (file, encoding, callback) {
// ignore empty files
if (file.isNull()) {
callback();
return;
}
// we don't do streams (yet)
if (file.isStream()) {
this.emit('error', new Error('gulp-plugin-vue: Streaming not supported'));
callback();
return;
}
let contents = file.contents.toString();
let filename = file.relative;
let sourceRoot = file.base;
let descriptor = parse({
source: contents,
filename,
compiler,
// https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilerparsecomponentfile-options
compilerParseOptions: { pad: 'line' },
sourceRoot,
needMap: true
});
const scopeId = `data-v-${hash(filename)}`;
const scoped = descriptor.styles.some(s => s.scoped);
const isFunctional = descriptor.template && descriptor.template.attrs.functional;
let script = [];
let styles = [];
// script
if (descriptor.script) {
// todo 需要babel处理
script.push(descriptor.script.content);
} else {
script.push('module.exports = {};');
}
script.push('var __vue__options__;');
script.push('if (exports && exports.__esModule && exports.default) {');
script.push(' __vue__options__ = exports.default;');
script.push('} else {');
script.push(' __vue__options__ = module.exports;');
script.push('}');
script.push(`__vue__options__.__file = ${JSON.stringify(filename)};`);
// template
if (descriptor.template) {
if (options.runtime) {
/**
* Compiles a template string and returns compiled JavaScript code.
* The returned result is an object of the following format:
* {
* ast: ?ASTElement, // parsed template elements to AST
* render: string, // main render function code
* staticRenderFns: Array<string>, // render code for static sub trees, if any
* errors: Array<string> // template syntax errors, if any
* tips: Array<string>
* }
*/
let compiled = compileTemplate({
source: descriptor.template.content,
filename,
compiler,
isFunctional
});
// tips
if (compiled.tips && compiled.tips.length) {
compiled.tips.forEach(tip => {
log.warn(typeof tip === 'object' ? tip.msg : tip);
})
}
// errors
if (compiled.errors && compiled.errors.length) {
// 2.6 compiler outputs errors as objects with range
if (compiler.generateCodeFrame && options.outputSourceRange) {
// TODO account for line offset in case template isn't placed at top
// of the file
log.error(
`\n\n Errors compiling template:\n\n` +
compiled.errors.map(({ msg, start, end }) => {
const frame = compiler.generateCodeFrame(source, start, end)
return ` ${msg}\n\n${pad(frame)}`
}).join(`\n\n`) +
'\n'
)
} else {
log.error(
`\n Error compiling template:\n${pad(compiled.source)}\n` +
compiled.errors.map(e => ` - ${e}`).join('\n') +
'\n'
)
}
}
script.push(compiled.code);
script.push('__vue__options__.render = render;');
script.push('__vue__options__.staticRenderFns = staticRenderFns;');
script.push('__vue__options__._compiled = true;');
} else {
script.push('__vue__options__.template = ' + JSON.stringify(descriptor.template.content) + ';');
}
}
if (isFunctional) {
script.push('__vue__options__.functional = true;');
}
if(scoped) {
script.push('__vue__options__._scopeId = ' + JSON.stringify(scopeId) + ';');
}
// style
for (let style of descriptor.styles) {
if (style.content && !/^\s*$/.test(style.content)) {
const styled = compileStyle({
source: style.content,
filename,
id: scopeId,
//map: inMap,
scoped,
trim: true,
preprocessLang: style.lang || "css"
});
if (styled.errors && styled.errors.length) {
log.error(
`\n Error compiling style:\n` +
styled.errors.map(e => ` - ${e}`).join('\n') +
'\n'
)
}
styles.push(styled.code);
}
}
// 同名样式文件
if (styles.length) {
let styleFile = file.clone();
styleFile.contents = Buffer.from(styles.join('\n'));
styleFile.extname = '.css';
this.push(styleFile);
}
// 同名脚本文件
file.contents = Buffer.from(script.join('\n'));
file.extname = '.js';
callback(null, file);
}, function(callback) {
// todo
callback();
});
}
function pad(source) {
return source
.split(/\r?\n/)
.map(line => ` ${line}`)
.join('\n')
}