-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
149 lines (129 loc) · 4.75 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
var gulp = require('gulp');
var less = require('gulp-less');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var browserSync = require('browser-sync');
var gulpif = require('gulp-if');
var rename = require('gulp-rename');
var lazypipe = require('lazypipe');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var changed = require('gulp-changed');
var del = require('del');
var data = require('gulp-data');
var path = require('path');
var swig = require('gulp-swig');
var sourcemaps = require('gulp-sourcemaps');
var gulpFilter = require('gulp-filter');
var concat = require('gulp-concat');
var statuses = {
isRelease: false,
isWatch: false
};
var paths = {
appData: './app/data/',
appLayouts: './app/layouts/',
appPartials: './app/partials/',
appViews: './app/views/',
appStatic: './app/static/',
dist: './dist/',
distStatic: './dist/static/'
};
function getJsonData(file, next) {
var fileNameFull = path.basename(file.path);
var fileExt = path.extname(file.path);
var fileName = fileNameFull.slice(0, -fileExt.length);
try {
var jsonData = require('./app/data/' + fileName + '.json');
return next(undefined, jsonData);
} catch(e) {
return next();
}
}
gulp.task('html', function () {
gulp.src(paths.appViews + '**/*.html')
.pipe(data(getJsonData))
.pipe(swig({ defaults: { cache: false } }))
.pipe(gulp.dest(paths.dist));
});
gulp.task('fonts', function () {
return gulp.src(paths.appStatic + 'fonts/**/*.*')
.pipe(changed(paths.distStatic + 'fonts/'))
.pipe(gulp.dest(paths.distStatic + 'fonts/'));
});
gulp.task('images', function () {
var devPipe = lazypipe()
.pipe(changed, paths.distStatic + 'images/')
.pipe(gulp.dest, paths.distStatic + 'images/')
.pipe(function () {
return gulpif(statuses.isWatch, browserSync.reload({ stream: true }));
});
var releasePipe = lazypipe()
.pipe(imagemin, { optimizationLevel: 3, progressive: true, interlaced: true })
.pipe(gulp.dest, paths.distStatic + 'images/');
return gulp.src(paths.appStatic + 'images/**/*.*')
.pipe(gulpif(statuses.isRelease, releasePipe(), devPipe()));
});
gulp.task('scripts', function () {
var releasePipe = lazypipe()
.pipe(uglify)
.pipe(rename, { suffix: '.min' })
.pipe(gulp.dest, paths.distStatic + 'scripts/');
return gulp.src([
paths.appStatic + 'scripts/vendor/jquery.js'
, paths.appStatic + 'scripts/main.js'
])
.pipe(sourcemaps.init())
.pipe(concat('main.js'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.distStatic + 'scripts/'))
.pipe(gulpFilter('**/*.js'))
.pipe(gulpif(statuses.isRelease, releasePipe()));
});
gulp.task('styles', function () {
var devPipe = lazypipe()
.pipe(function () {
return gulpif(statuses.isWatch, browserSync.reload({ stream: true }));
});
var releasePipe = lazypipe()
.pipe(minifyCSS)
.pipe(rename, { suffix: '.min' })
.pipe(gulp.dest, paths.distStatic + 'styles/');
return gulp.src(paths.appStatic + 'styles/main.less')
// https://github.com/sindresorhus/gulp-autoprefixer/issues/2
// .pipe(sourcemaps.init())
.pipe(less())
.pipe(autoprefixer({ cascade: false }))
// // https://github.com/sindresorhus/gulp-autoprefixer/issues/2
// .pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.distStatic + 'styles/'))
.pipe(gulpFilter('**/*.css'))
.pipe(gulpif(statuses.isRelease, releasePipe(), devPipe()));
});
gulp.task('videos', function () {
return gulp.src(paths.appStatic + 'videos/**/*.*')
.pipe(changed(paths.distStatic + 'videos/'))
.pipe(gulp.dest(paths.distStatic + 'videos/'));
});
gulp.task('clean', function () {
del.sync([paths.dist]);
});
gulp.task('watch', ['browser-sync'], function () {
gulp.watch([
paths.appViews + '**/*.html',
paths.appLayouts + '**/*.html',
paths.appPartials + '**/*.html'
], ['html', browserSync.reload]);
gulp.watch(paths.appStatic + 'styles/**/*.{less,css}', ['styles']);
gulp.watch(paths.appStatic + 'scripts/**/*.js', ['scripts', browserSync.reload]);
gulp.watch(paths.appStatic + 'images/**/*.*', ['images']);
});
gulp.task('browser-sync', ['default'], function () {
browserSync({ server: { baseDir: paths.dist } });
statuses.isWatch = true;
});
gulp.task('set-release-state', function () {
statuses.isRelease = true;
});
gulp.task('default', ['html', 'fonts', 'images', 'scripts', 'styles', 'videos']);
gulp.task('release', ['set-release-state', 'clean', 'default']);