-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (38 loc) · 1.31 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
const through = require('through2')
const PluginError = require('plugin-error')
const replaceExt = require('replace-ext')
const VueCompiler = require('@vialer/vue-compiler')
const PLUGIN_NAME = 'vue-compiler-gulp'
function vueCompilerGulp(options) {
let defaults = {
namespace: 'window.templates',
commonjs: false,
pathfilter: [],
vue: {
preserveWhitespace: false,
},
}
options = Object.assign(defaults, options)
const vueCompiler = new VueCompiler(Object.assign(defaults, options))
return through.obj(function(file, encode, callback) {
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported'))
callback()
return
}
let target = file.path.replace(`${process.cwd()}/`, '')
vueCompiler.processFile(file.contents.toString(), target).then((result) => {
file.path = replaceExt(file.path, '.js')
file.contents = Buffer.from(result.data)
callback(null, file)
})
.catch((err) => {
this.emit('error', new PluginError(PLUGIN_NAME, `${target}: ${err}`))
})
})
}
module.exports = vueCompilerGulp