-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
176 lines (158 loc) · 5.64 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
'use strict';
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const del = require('del');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const handlebars = require('gulp-compile-handlebars');
const rename = require('gulp-rename');
const spritesmith = require('gulp.spritesmith');
const htmlbeautify = require('gulp-html-beautify');
const glob = require('glob');
const changed = require('gulp-changed');
const nunjucks = require('gulp-nunjucks-render');
const imageResize = require('gulp-image-resize');
var config = {
device: 'mobile',
sprite_src: 'src/sprite/',
sprite_dest: 'src/img/',
css_src: 'src/scss/',
css_dest: 'src/css/',
layout_src: 'src',
layout_dest: 'dist',
};
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function (file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
gulp.task('watch', ['nunjucks_watch', 'asset_watch', 'scss_watch', 'sprite_watch']);
gulp.task('sprite', ['makeSprite', 'makeSpriteMap']);
gulp.task('dev', ['sprite', 'sass', 'watch']);
gulp.task('makeSprite', function () {
var stream_arr = [];
var options = {
spritesmith: function (folder) {
return {
imgPath: path.posix.relative(config.css_dest, path.posix.join(config.sprite_dest, 'sp_' + folder + '.png')),
imgName: 'sp_' + folder + '.png',
cssName: '_sp_' + folder + '.scss',
cssFormat: 'scss',
padding: 6,
cssTemplate: './gulpconf/sprite_template.hbs',
cssSpritesheetName: 'sp_' + folder
}
}
};
getFolders(config.sprite_src).map(function (folder) {
var spriteData = gulp.src(path.join(config.sprite_src, folder, '*.png'))
.pipe(spritesmith(options.spritesmith(folder)));
stream_arr.push(new Promise(function (resolve) {
spriteData.img
.pipe(gulp.dest(config.sprite_dest))
.on('end', resolve);
}));
stream_arr.push(new Promise(function (resolve) {
spriteData.css
.pipe(gulp.dest(path.join(config.css_src, 'sprite')))
.on('end', resolve);
}));
});
return Promise.all(stream_arr);
});
gulp.task('makeSpriteMap', ['makeSprite'], function () {
return gulp.src('gulpconf/sprite_maps_template.hbs')
.pipe(handlebars({
prefix: 'sp_',
path: path.posix.relative(path.posix.join(config.css_src, 'import'), path.posix.join(config.css_src, 'sprite')),
import: getFolders(config.sprite_src),
ratio: (config.device == 'pc') ? 1 : 3
}))
.pipe(rename('_sprite_maps.scss'))
.pipe(gulp.dest(path.join(config.css_src, 'import')));
});
gulp.task('sass', function () {
return gulp.src(path.join(config.css_src, '**/*.scss'))
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'expanded',
indentType: 'tab',
indentWidth: 1
}).on('error', sass.logError))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.css_dest));
});
// img, css modify dest
gulp.task('asset', function () {
return gulp.src([path.join(config.layout_src, '**/*.+(css|jpg|png|gif|js)')])
.pipe(gulp.dest(config.layout_dest))
})
var beautify_options = {
"indent_size": 4,
"max_preserve_newlines": 1,
"end_with_newline": false,
};
gulp.task('nunjucks', function () {
var includeDirs = [path.join(config.layout_src, '**/template'), path.join(config.layout_src, 'include'), path.join(config.layout_src, 'common_include'), path.join(config.layout_src, '**/inc'), path.join(config.layout_src, 'common_template'), path.join(config.layout_src, 'include_partial'), path.join(config.layout_src, 'common_js')]
return gulp.src([path.join(config.layout_src, 'html/*.html')])
.pipe(changed(config.layout_dest))
.pipe(nunjucks({
envOptions: {
autoescape: false
},
path: includeDirs.reduce(function (dirs, g) {
return dirs.concat(glob.sync(g));
}, []),
}).on('error', function (e) { console.log('Error! Check file : ' + e.fileName); }))
.pipe(htmlbeautify({
"indent_size": 4,
"max_preserve_newlines": 1,
"end_with_newline": false
}))
.pipe(gulp.dest(config.layout_dest));
});
gulp.task('beautify', function () {
return gulp.src([path.join(config.layout_dest, '**/*.html')])
.pipe(htmlbeautify(beautify_options))
.pipe(gulp.dest(config.layout_dest))
})
gulp.task('nunjucks_watch', function () {
gulp.watch([path.join(config.layout_src, 'html/*.html')], ['nunjucks']).on('change', function (event) {
if (event.type === 'deleted') {
var filePathFromSrc = path.relative(path.resolve(config.layout_src), event.path);
var destFilePath = path.resolve(config.layout_dest, filePathFromSrc);
del.sync(destFilePath);
}
});
})
gulp.task('asset_watch', function () {
gulp.watch([path.join(config.layout_src, '**/*.+(css|jpg|png|gif|js)')], ['asset']).on('change', function (event) {
if (event.type === 'deleted') {
var filePathFromSrc = path.relative(path.resolve(config.layout_src), event.path);
var destFilePath = path.resolve(config.layout_dest, filePathFromSrc);
del.sync(destFilePath);
}
});
})
gulp.task('scss_watch', function () {
gulp.watch([path.join(config.css_src, '/**/*')], ['sass']);
})
gulp.task('sprite_watch', function () {
gulp.watch([path.join(config.sprite_src, '/**/*')], ['sprite']);
})
gulp.task('build', function () {
gulp.task('build', ['nunjucks', 'beautify', 'asset', 'index'])
})
gulp.task('img_resize', function () {
gulp.src('src/img/sp_w_layout.png')
.pipe(imageResize({
percentage: 50
}))
.pipe(rename('sp_w_layout_x1.png'))
.pipe(gulp.dest('src/img/'));
});
gulp.task("clean", function () {
return del(["dist/*.js", "dist/*.css", "dist/components/", "dist/containers/", "dist/contexts/", "dist/pages/", "dist/css/import/", "dist/css/fa-brands.css", "dist/css/fa-regular.css", "dist/css/fa-solid.css", "dist/css/fontawesome.css", "dist/css/v4-shims.css"], { force: true });
});