-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
144 lines (120 loc) · 3.6 KB
/
gulpfile.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
var gulp = require('gulp')
var path = require('path')
var tap = require('gulp-tap')
var fs = require('fs')
var jsx2wxml = require('./_scripts/jsx2wxml')
var watch = require('gulp-watch');
var prettier = require('prettier')
var colors = require('colors');
var less = require('less');
function buildComponent(code) {
return `
class WeElement {
render () {
return ${code.trim()}
}
}`}
// var code = require("babel-core").transform("<div dd='1'>aaa</div>",{
// "plugins": [
// ["transform-react-jsx", {
// "pragma": "global.__h"
// }]
// ]
// }).code;
// console.log(code)
var baseOptions = {
isRoot: false,
isApp: false,
sourcePath: __dirname,
outputPath: __dirname,
code: '',
isTyped: false
}
gulp.task('watch', () => {
watch(['./**/*.jsx', '!./node_modules/**', '!./_scripts/**'], { events: ['add', 'change'] }, (evt, type) => {
var contents = fs.readFileSync(evt.path)
compile({
path: evt.path,
contents: contents.toString()
}, true)
})
})
gulp.task('watchLess', () => {
watch(['./**/*.less', '!./node_modules/**', '!./_scripts/**', '!./common-less/**'], { events: ['add', 'change'] }, (evt, type) => {
var contents = fs.readFileSync(evt.path)
compileLess({
path: evt.path,
contents: contents.toString()
}, true)
})
})
gulp.task('watchCommonLess', () => {
watch('./common-less/**', { events: ['add', 'change'] }, (evt, type) => {
gulp.start('compileLess')
})
})
//加 cache?同样的字符串返回同样的结果
gulp.task('compile', () => {
return gulp
.src(['./**/*.jsx', '!./node_modules/**', '!./_scripts/**'])
.pipe(
tap(file => {
compile({
path: file.path,
contents: file.contents.toString()
})
})
)
})
function compile(file, watch) {
var dir = path.dirname(file.path)
var name = path.basename(file.path, '.jsx')
console.log('[编译文件]'.green , file.path)
var template = jsx2wxml.default({
...baseOptions,
code: buildComponent(file.contents)
}).template.replace(/<block>/,'').replace(/([\s\S]*)<\/block>/,'$1')
console.log('[编译完成]'.green , file.path)
const res = prettier.format(template, { parser: "angular" })
console.log('[代码美化]'.green , name + '.qml' )
fs.writeFileSync(dir + '/' + name + '.qml', res)
console.log('[写入文件]' .green , name + '.qml')
if(watch){
console.log('[编译完成]'.green , name + '.qml' )
console.log('[监听更改]'.green, '...' )
}
}
function compileLess(file, watch) {
var dir = path.dirname(file.path)
var name = path.basename(file.path, '.less')
console.log('[编译文件]'.green, file.path)
less.render(file.contents, {
paths: ['.', './common-less'],
}, function (e, output) {
console.log('[编译完成]'.green, file.path)
fs.writeFileSync(dir + '/' + name + '.qss', output.css)
console.log('[写入文件]'.green, name + '.qss')
if (watch) {
console.log('[编译完成]'.green, name + '.qss')
console.log('[监听更改]'.green, '...')
}
});
}
gulp.task('compileLess', () => {
return gulp
.src(['./**/*.less', '!./node_modules/**', '!./_scripts/**', '!./common-less/**'])
.pipe(
tap(file => {
compileLess({
path: file.path,
contents: file.contents.toString()
})
})
)
})
gulp.task('default', ['compile', 'compileLess', 'watch', 'watchLess', 'watchCommonLess'])
console.log('[开始编译]'.green ,'...')
gulp.start('default',function(){
console.log('[编译完成]'.green , '恭喜你全部文件编译完成。' )
console.log('[监听更改]'.green, '...' )
})