-
Notifications
You must be signed in to change notification settings - Fork 21
/
gulpfile.babel.js
89 lines (77 loc) · 2.26 KB
/
gulpfile.babel.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
'use strict';
var yargs = require('yargs').argv;
import gulp from 'gulp';
import chokidar from 'chokidar';
import sass from 'gulp-sass';
import minify from 'gulp-clean-css';
import autoprefixer from 'gulp-autoprefixer';
import rename from 'gulp-rename';
import header from 'gulp-header';
import pkg from './package.json';
import path from 'path';
import express from 'express';
// 构建后的目标地址
const dist = path.join(__dirname, 'dist');
// release 整个项目
gulp.task('release', () => {
const option = {base: 'src'};
const banner = [
'/*!',
' * WeUI-sass v<%= pkg.version %> (<%= pkg.homepage %>)',
' * Author: <%= pkg.author %>.',
' * Time: <%= new Date().getFullYear() %>/<%= new Date().getMonth() %>/<%= new Date().getDate() %>.',
' */',
''].join('\n');
// 复制非scss文件到dist
gulp.src('src/example/**/*.!(scss)', option)
.pipe(gulp.dest(dist));
gulp.src('src/example/**/*.scss', option)
.pipe(sass())
.pipe(gulp.dest(dist));
gulp.src('src/style/weui.scss', option)
.pipe(sass().on('error', (e) => {
console.error(e.message);
this.emit('end');
}))
.pipe(header(banner, { pkg : pkg } ))
.pipe(autoprefixer({
browsers: ['iOS >= 7', 'Android >= 4.1']
}))
.pipe(gulp.dest(dist))
.pipe(minify())
.pipe(rename( (path) => {
path.basename += '.min';
}))
.pipe(gulp.dest(dist));
});
// 如果文件发生变动则直接release
gulp.task('watch', () => {
chokidar.watch('src/**/*.*').on('all', () => {
gulp.run('release');
});
});
// 启动server
gulp.task('server', () => {
const app = express();
const port = yargs.p || yargs.port || 8080;
app.use(express.static(dist));
app.listen(port, () => {
const url = 'http://127.0.0.1' + (port === 80 ? '' : ':' + port);
console.log(url);
});
});
// 参数说明
// -w: 实时监听
// -s: 启动服务器
// -p: 服务器启动端口,默认8080
gulp.task('default', () => {
if (yargs.w){
gulp.start('release');
gulp.start('watch');
}else{
gulp.start('release');
}
if (yargs.s){
gulp.start('server');
}
});