This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
54 lines (44 loc) · 1.61 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
var through = require('through2')
var path = require('path')
var weexTransformer = require('weex-transformer')
var gutil = require('gulp-util')
var printf = require('printf')
var chalk = require('chalk')
var pluginName = require('./package.json').name
module.exports = function weex(options) {
options = options || {}
options.oldFormat = options.oldFormat === true ? true : false
options.isEntry = options.isEntry !== false ? true : false
/**
* @this {Transform}
*/
var transform = function transform(file, encoding, callback) {
/* istanbul ignore if */
if (file.isNull()) {
return callback(null, file)
}
/* istanbul ignore if */
if (file.isStream()) {
return callback(new gutil.PluginError(pluginName, 'Streaming not supported'))
}
if (file.isBuffer()) {
var filename = path.basename(file.path).replace(/\..+/, '')
var dir = path.relative(file.cwd, file.base)
var content = file._contents.toString()
var ret = weexTransformer.transform(filename, content, dir, null, options)
file.path = file.path.replace(/\..+/, '.js')
file.contents = new Buffer(ret.result)
// print logs
ret.logs && ret.logs.forEach(function (log) {
var reason = log.reason.toUpperCase()
var formattedLog = printf('%-10s %4d:%-4d %s', log.name, log.line, log.column, log.reason)
if (reason.indexOf('NOTE') !== 0) {
formattedLog = chalk[reason.indexOf('ERROR') === 0 ? 'red' : 'yellow'](formattedLog)
}
gutil.log(formattedLog)
})
}
return callback(null, file)
}
return through.obj(transform)
}