-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
69 lines (55 loc) · 2.22 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
(function() {
'use strict';
var through = require('through2');
var PluginError = require('plugin-error')
var replaceExtension = require('replace-ext');
var compiler = require('vue-component-compiler');
var babel = require('babel-core');
var path = require('path');
var PLUGIN_NAME = 'gulp-vue-compiler';
function gulpVueCompiler (options) {
options = Object.assign({ esModule: true }, options || {});
var parserConfig = Object.assign({ needMap: false }, options.parserConfig || {});
var templateCompilerConfig = Object.assign({}, options.templateCompilerConfig || {}, { esModule: options.esModule });
return through.obj(function (file, encode, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported'));
return callback();
}
if (options.newExtension) {
file.path = replaceExtension(file.path, '.' + options.newExtension);
}
try {
var descriptor = compiler.parse(file.contents.toString(), file.path, parserConfig);
var script = descriptor.script.content;
var template = compiler.compileTemplate({
code: descriptor.template.content,
descriptor: descriptor.template
}, file.path, templateCompilerConfig).code;
// Assemble ESM / CJS
var replaceExport, exportDefault;
if (options.esModule) {
replaceExport = /^export\s+default/m;
exportDefault = '\n\nexport default';
} else {
replaceExport = /^module\.exports =/m;
exportDefault = '\n\nmodule.exports =';
}
script = script.replace(replaceExport, 'var __script__ =');
template = template.replace(replaceExport, 'var __template__ =');
var component = [script, template, exportDefault + ' Object.assign({}, __script__, __template__);'].join('\n\n');
component = babel.transform(component, options.babel).code;
file.contents = new Buffer(component);
callback(null, file);
} catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME,
'In file ' + path.relative(process.cwd(), file.path) + ':\n' + err.message));
return callback();
}
});
}
module.exports = gulpVueCompiler;
})()