-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
186 lines (153 loc) · 4.72 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
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
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 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)
//注释掉以修复 import 的问题
//const res = prettier.format(template, { parser: "angular" })
//console.log('[代码美化]'.green, name + '.wxml')
fs.writeFileSync(dir + '/' + name + '.wxml', template)
console.log('[写入文件]'.green, name + '.wxml')
if (watch) {
console.log('[编译完成]'.green, name + '.wxml')
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 + '.wxss', output.css)
console.log('[写入文件]'.green, name + '.wxss')
if (watch) {
console.log('[编译完成]'.green, name + '.wxss')
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('compileSVG', () => {
return gulp
.src(['./**/*.svg', '!./node_modules/**', '!./_scripts/**'])
.pipe(
tap(file => {
compileSVG({
path: file.path,
contents: file.contents.toString()
})
})
)
})
gulp.task('watchSVG', () => {
watch(['./**/*.svg', '!./node_modules/**', '!./_scripts/**'], { events: ['add', 'change'] }, (evt, type) => {
var contents = fs.readFileSync(evt.path)
compileSVG({
path: evt.path,
contents: contents.toString()
}, true)
})
})
function compileSVG(file, watch) {
var dir = path.dirname(file.path)
var name = path.basename(file.path, '.svg')
console.log('[编译文件]'.green, file.path)
var code = require("babel-core").transform(file.contents, {
"plugins": [
["transform-react-jsx", {
"pragma": "h"
}]
]
}).code;
console.log('[编译完成]'.green, file.path)
fs.writeFileSync(dir + '/' + name + '.js', prettier.format(`const h = (type, props, ...children)=>({ type, props, children });export default ${code}`, { parser: "babel" }))
console.log('[写入文件]'.green, name + '.js')
if (watch) {
console.log('[编译完成]'.green, name + '.js')
console.log('[监听更改]'.green, '...')
}
}
gulp.task('default', ['compile', 'compileLess', 'compileSVG', 'watch', 'watchLess', 'watchCommonLess', 'watchSVG'])
console.log('[开始编译]'.green, '...')
gulp.start('default', function () {
console.log('[编译完成]'.green, '恭喜你全部文件编译完成。')
console.log('[监听更改]'.green, '...')
})