-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
189 lines (172 loc) · 6.93 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
(function(r) {
'use strict';
var
gulp = r('gulp'),
bom = r('gulp-bom'), //https://www.npmjs.com/package/gulp-bom; // 输出文件 utf-8 修复
sass = r('gulp-sass'), //https://www.npmjs.com/package/gulp-sass; // scss/sass 编译支持
cssmin = r('gulp-cssmin'), //https://www.npmjs.com/package/gulp-cssmin; // css 压缩
htmlmin = r('gulp-htmlmin'), //https://www.npmjs.com/package/gulp-htmlmin; // html 压缩
imagemin = require('gulp-imagemin'), //https://www.npmjs.com/package/gulp-imagemin // 图片压缩
concat = r('gulp-concat'), //https://www.npmjs.com/package/gulp-htmlmin; // 合并 js/css
rename = r('gulp-rename'), //https://www.npmjs.com/package/gulp-rename; // 输出文件重命名
reversion = r('gulp-rev'), //https://www.npmjs.com/package/gulp-rev; // 输出文件名追加 hash 版本
usemin = r('gulp-usemin'), //https://www.npmjs.com/package/gulp-usemin;// html 资源引用优化
autoprefixer = r('gulp-autoprefixer'), // https://www.npmjs.com/package/gulp-autoprefixer
through = r('through2'),
path = r('path'),
fs = r('fs'),
del = r('del'),
sha = r('sha'),
q = r('q');
/**
* path configuration
*/
const srcDir = './src/';
const buildDir = './src/';
const releaseDir = './';
const componentDir = './bower_components/';
/**
* 以源目录中的文件为基准验证目标目录中的文件是否已经发生变更。
* @param {*源目录中的文件信息} file
* @param {*文件源目录} srcDir
* @param {*文件目标目录} targetDir
*/
function isFileDifferent(file, srcDir, targetDir) {
var name = file.path;
var targetName = name.replace(path.resolve(srcDir), path.resolve(targetDir));
if (!fs.existsSync(targetName)) {
return true;
} else {
var actural = sha.getSync(name);
var expected = sha.getSync(targetName);
if (actural !== expected) {
return true;
}
}
return false;
}
/**
* build tasks
*/
gulp.task('copy:bootstrapfonts', function() {
return gulp.src(componentDir + 'bootstrap-sass/assets/fonts/**/*.*')
.pipe(through.obj(function(file, enc, cb) {
if (isFileDifferent(file, componentDir + 'bootstrap-sass/assets/fonts/bootstrap', buildDir + 'fonts/bootstrap')) {
this.push(file);
}
cb();
}))
.pipe(gulp.dest(buildDir + 'fonts/'));
})
gulp.task('build:bootstrap', ['copy:bootstrapfonts'], function() {
return gulp.src(srcDir + 'scss/bootstrap.scss')
.pipe(sass({
includePaths: [componentDir + 'bootstrap-sass/assets/stylesheets/']
}).on('error', sass.logError))
.pipe(bom())
.pipe(gulp.dest(buildDir + 'styles/'));
});
gulp.task('copy:font-awesome-fonts', function() {
return gulp.src(componentDir + 'font-awesome/fonts/**/*.*')
.pipe(through.obj(function(file, enc, cb) {
if (isFileDifferent(file, componentDir + 'font-awesome/fonts', buildDir + 'fonts/font-awesome')) {
this.push(file);
}
cb();
}))
.pipe(gulp.dest(buildDir + 'fonts/font-awesome/'));
});
gulp.task('build:font-awesome', ['copy:font-awesome-fonts'], function() {
return gulp.src(srcDir + 'scss/font-awesome.scss')
.pipe(sass({
includePaths: [componentDir + 'font-awesome/']
}).on('error', sass.logError))
.pipe(bom())
.pipe(gulp.dest(buildDir + 'styles/'));
});
gulp.task('copy:simple-line-icons-fonts', function() {
return gulp.src(componentDir + 'simple-line-icons/fonts/**/*.*')
.pipe(through.obj(function(file, enc, cb) {
if (isFileDifferent(file, componentDir + 'simple-line-icons/fonts', buildDir + 'fonts/simple-line-icons')) {
this.push(file);
}
cb();
}))
.pipe(gulp.dest(buildDir + 'fonts/simple-line-icons/'));
});
gulp.task('build:simple-line-icons', ['copy:simple-line-icons-fonts'], function() {
return gulp.src(srcDir + 'scss/simple-line-icons.scss')
.pipe(sass({
includePaths: [componentDir + 'simple-line-icons/']
}).on('error', sass.logError))
.pipe(bom())
.pipe(gulp.dest(buildDir + 'styles/'));
});
gulp.task('build:sitecss', function() {
return gulp.src(srcDir + 'scss/site.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
cascade: false
}))
.pipe(bom())
.pipe(gulp.dest(buildDir + 'styles/'));
});
gulp.task('build', ['build:bootstrap', 'build:font-awesome', 'build:simple-line-icons', 'build:sitecss']);
/**
* release tasks
*/
gulp.task('release:fonts', ['build'], function() {
return gulp.src(buildDir + 'fonts/**/*.*')
.pipe(through.obj(function(file, enc, cb) {
if (isFileDifferent(file, buildDir + 'fonts', releaseDir + 'fonts')) {
this.push(file);
}
cb();
}))
.pipe(gulp.dest(releaseDir + 'fonts/'));
});
gulp.task('release:images', ['build'], function() {
return gulp.src(buildDir + 'images/**/*.*')
.pipe(imagemin())
.pipe(through.obj(function(file, enc, cb) {
if (isFileDifferent(file, buildDir + 'images', releaseDir + 'images')) {
this.push(file);
}
cb();
}))
.pipe(gulp.dest(releaseDir + 'images'));
});
gulp.task('release:index', ['build'], function() {
return gulp.src(buildDir + 'index.html')
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(usemin({
css: [cssmin, reversion]
}))
.pipe(bom())
.pipe(gulp.dest(releaseDir))
})
gulp.task('release', ['release:index', 'release:fonts', 'release:images']);
gulp.task('default', ['build']);
/**
* clean task
*/
gulp.task('clean', function() {
return del([
buildDir + 'fonts/',
buildDir + 'styles/',
releaseDir + 'index.html',
releaseDir + 'fonts/',
releaseDir + 'styles/'
]);
});
/**
* 实时监听
*/
gulp.task('watch', function() {
var watcher = gulp.watch('src/**/*.scss', ['build']);
watcher.on('change', function(event) {
console.log('Event type: ' + event.type); // added, changed, or deleted
console.log('Event path: ' + event.path); // The path of the modified file
});
});
})(require);