-
Notifications
You must be signed in to change notification settings - Fork 176
/
gulpfile.js
executable file
·141 lines (122 loc) · 3.43 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
var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var px2rem = require('gulp-px3rem');
var eslint = require('gulp-eslint');
var htmlone = require('gulp-htmlone');
var rename = require('gulp-rename');
var del = require('del');
var source = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
var gulpify = require('gulpify');
var sassdoc = require('sassdoc');
var paths = {
src: './src',
build: './build'
};
// using gulpify:
gulp.task('gulpify', function() {
gulp.src('index.js')
.pipe(gulpify())
.pipe(uglify())
.pipe(rename('bundle.js'))
.pipe(gulp.dest('./'))
})
// using vinyl-source-stream:
gulp.task('browserify', function() {
var bundleStream = browserify('./index.js').bundle()
bundleStream
.pipe(source('index.js'))
.pipe(streamify(uglify()))
.pipe(rename('bundle.js'))
.pipe(gulp.dest('./'))
})
// css
gulp.task('sass', function () {
return sass(paths.src + '/**/*.scss')
.on('error', sass.logError)
.pipe(gulp.dest(paths.build + '/css/'));
});
gulp.task('css', ['sass'], function () {
return gulp.src(paths.src + '/**/*.css', {base: paths.src})
.pipe(px2rem({
threeVersion: false,
// XXX: 以下两项根据项目实际情况做修改
remUnit: 75,
baseDpr: 2,
forcePxComment: 'px',
keepComment: 'no'
}))
// px3rem 默认输出的文件带有 .debug 后缀
.pipe(rename(function (path) {
path.basename = path.basename.replace('.debug', '');
}))
.pipe(postcss([
// 自动添加厂商前缀
autoprefixer({
browsers: ['> 1%', 'last 2 versions']
})
]))
.pipe(gulp.dest(paths.src));
});
// js
gulp.task('eslint', function () {
return gulp.src(paths.src + '/js/**/*.js', {base: paths.src})
.pipe(eslint())
.pipe(eslint.format())
// 如果 eslint 有 error 则报错,不执行后续操作
.pipe(eslint.failAfterError());
});
// combo
// 1. htmlone 集成了 uglify-js 和 ycssmin 插件,所以不需要额外的 minify 方案
gulp.task('htmlone', ['css', 'eslint'], function () {
// 先保存一个未压缩版
gulp.src(paths.src + '/**/*.html', {base: paths.src})
.pipe(htmlone({
cssminify: false,
jsminify: false
}))
.pipe(rename({
suffix: '.debug'
}))
.pipe(gulp.dest(paths.build));
// 再保存一个压缩版
return gulp.src(paths.src + '/**/*.html', {base: paths.src})
.pipe(htmlone({
cssminify: true,
jsminify: true
}))
.pipe(gulp.dest(paths.build));
});
// clean
gulp.task('clean', function () {
del(paths.build);
});
gulp.task('sassdoc', function () {
var options = {
dest: 'docs',
verbose: true,
display: {
access: ['public', 'private'],
alias: true,
watermark: true,
},
// groups: {
// 'undefined': 'Ungrouped',
// foo: 'Foo group',
// bar: 'Bar group',
// },
basePath: 'https://github.com/W3cplus/SassMagic/blob/master/src',
};
return gulp.src('src/**/*.scss')
.pipe(sassdoc(options));
});
gulp.task('watch', ['default'], function () {
//gulp.watch([paths.src + '/js/*.js'], ['eslint']);
gulp.watch([paths.src + '/**/*.scss'], ['css']);
});
gulp.task('build', ['clean', 'sass', 'css', 'eslint', 'htmlone','sassdoc']);
gulp.task('default', ['build']);