-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
gulpfile.js
208 lines (191 loc) · 5.11 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const fs = require('fs')
const path = require('path')
const gulp = require('gulp')
const gulpLoadPlugins = require('gulp-load-plugins')
const del = require('del')
const runSequence = require('run-sequence')
const inquirer = require('inquirer')
const generatePage = require('generate-weapp-page')
// load all gulp plugins
const plugins = gulpLoadPlugins()
const env = process.env.NODE_ENV || 'development'
const isProduction = () => env === 'production'
// utils functions
function generateFile (options) {
const files = generatePage({
root: path.resolve(__dirname, './src/pages/'),
name: options.pageName,
less: options.styleType === 'less',
scss: options.styleType === 'scss',
css: options.styleType === 'css',
json: options.needConfig
})
files.forEach && files.forEach(file => plugins.util.log('[generate]', file))
return files
}
function generateJson (options) {
const filename = path.resolve(__dirname, 'src/app.json')
const now = fs.readFileSync(filename, 'utf8')
const temp = now.split('\n // Dont remove this comment')
if (temp.length !== 2) {
return plugins.util.log('[generate]', 'Append json failed')
}
const result = `${temp[0].trim()},
"pages/${options.pageName}/${options.pageName}"
// Dont remove this comment
${temp[1].trim()}
`
fs.writeFileSync(filename, result)
}
/**
* Clean distribution directory
*/
gulp.task('clean', del.bind(null, ['dist/*']))
/**
* Lint source code
*/
gulp.task('lint', () => {
return gulp.src(['*.{js,json}', '**/*.{js,json}', '!node_modules/**', '!dist/**', '!**/bluebird.js'])
.pipe(plugins.eslint())
.pipe(plugins.eslint.format('node_modules/eslint-friendly-formatter'))
.pipe(plugins.eslint.failAfterError())
})
/**
* Compile js source to distribution directory
*/
gulp.task('compile:js', () => {
return gulp.src(['src/**/*.js'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.babel())
.pipe(plugins.if(isProduction, plugins.uglify()))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
/**
* Compile xml source to distribution directory
*/
gulp.task('compile:xml', () => {
return gulp.src(['src/**/*.xml'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.if(isProduction, plugins.htmlmin({
collapseWhitespace: true,
// collapseBooleanAttributes: true,
// removeAttributeQuotes: true,
keepClosingSlash: true, // xml
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})))
.pipe(plugins.rename({ extname: '.wxml' }))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
/**
* Compile less source to distribution directory
*/
gulp.task('compile:less', () => {
return gulp.src(['src/**/*.less'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.less())
.pipe(plugins.if(isProduction, plugins.cssnano({ compatibility: '*' })))
.pipe(plugins.rename({ extname: '.wxss' }))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
/**
* Compile json source to distribution directory
*/
gulp.task('compile:json', () => {
return gulp.src(['src/**/*.json'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.jsonminify())
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
/**
* Compile img source to distribution directory
*/
gulp.task('compile:img', () => {
return gulp.src(['src/**/*.{jpg,jpeg,png,gif}'])
.pipe(plugins.imagemin())
.pipe(gulp.dest('dist'))
})
/**
* Compile source to distribution directory
*/
gulp.task('compile', ['clean'], next => {
runSequence([
'compile:js',
'compile:xml',
'compile:less',
'compile:json',
'compile:img'
], next)
})
/**
* Copy extras to distribution directory
*/
gulp.task('extras', [], () => {
return gulp.src([
'src/**/*.*',
'!src/**/*.js',
'!src/**/*.xml',
'!src/**/*.less',
'!src/**/*.json',
'!src/**/*.{jpe?g,png,gif}'
])
.pipe(gulp.dest('dist'))
})
/**
* Build
*/
gulp.task('build', ['lint'], next => runSequence(['compile', 'extras'], next))
/**
* Watch source change
*/
gulp.task('watch', ['build'], () => {
gulp.watch('src/**/*.js', ['compile:js'])
gulp.watch('src/**/*.xml', ['compile:xml'])
gulp.watch('src/**/*.less', ['compile:less'])
gulp.watch('src/**/*.json', ['compile:json'])
gulp.watch('src/**/*.{jpe?g,png,gif}', ['compile:img'])
})
/**
* Generate new page
*/
gulp.task('generate', next => {
inquirer.prompt([
{
type: 'input',
name: 'pageName',
message: 'Input the page name',
default: 'index'
},
{
type: 'confirm',
name: 'needConfig',
message: 'Do you need a configuration file',
default: false
},
{
type: 'list',
name: 'styleType',
message: 'Select a style framework',
// choices: ['less', 'scss', 'css'],
choices: ['less'],
default: 'less'
}
])
.then(options => {
const res = generateFile(options)
if (res) generateJson(options)
})
.catch(err => {
throw new plugins.util.PluginError('generate', err)
})
})
/**
* Default task
*/
gulp.task('default', ['watch'])