-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
333 lines (295 loc) · 9.45 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*========================================*/
/* Gulp plugin
/*========================================*/
var fs = require('fs');
var gulp = require('gulp');
var watch = require('gulp-watch'); //追加ファイルも検知する
var ejs = require('gulp-ejs'); //ejs
var sass = require('gulp-sass'); //SASSコンパイル
var csscomb = require('gulp-csscomb'); //CSS順番
var autoprefixer = require('gulp-autoprefixer'); //自動でprefixつける
var minify = require('gulp-minify-css'); //CSS圧縮
var rename = require('gulp-rename'); //リネーム
var plumber = require('gulp-plumber'); //エラーが出ても動作を止めない
var notify = require('gulp-notify'); //エラー時に通知
var browserSync = require('browser-sync'); //ローカルホストとオートリロード
var imagemin = require('gulp-imagemin'); //画像圧縮
var pngquant = require('imagemin-pngquant'); //PNGの圧縮率を髙く
var changed = require('gulp-changed'); //変更したファイルだけ処理させる
var cached = require('gulp-cached'); //変更したファイルだけ処理させる
var concat = require('gulp-concat'); //ファイルの結合
var uglify = require('gulp-uglify'); //特定のコメントを残したまま圧縮
var htmlhint = require("gulp-htmlhint"); //htmlhint
var jshint = require('gulp-jshint'); //jshint
// var stylestats = require('gulp-stylestats'); //StyleStats
var sourcemaps = require('gulp-sourcemaps'); //ソースマップ出力
var hologram = require('gulp-hologram'); //GulpからHologram実行
var iconfont = require('gulp-iconfont'); //アイコンフォント作成
var consolidate = require('gulp-consolidate'); //Lo-DashをGulpから使えるようにする
var webpack = require('gulp-webpack'); //webpack
/*========================================*/
/* Setting
/*========================================*/
// 対象ブラウザ
var AUTOPREFIXER_BROWSERS = [
'ie >= 11',
'iOS >= 9',
'Android >= 4.4'
];
// 開発環境パス
var src = {
base: './dev/',
html: './dev/ejs/',
scss: './dev/scss/',
js : './dev/js/',
img : './dev/images/',
font: './dev/icons/',
docs: './dev/docs/',
hologram: './hologram/',
};
// 出力環境パス
var dist = {
base: './htdocs/',
html: './htdocs/',
css : './htdocs/assets/css/',
js : './htdocs/assets/js/',
img : './htdocs/assets/images/',
font: './htdocs/assets/fonts/',
docs: './htdocs/_docs/',
};
/*========================================*/
/* Task
/*========================================*/
/**
* ejsのコンパイル
*/
gulp.task('ejs', function() {
gulp.src([ src.html + '**/*.ejs', '!' + src.html + '**/_*.ejs' ])
.pipe(cached('ejs'))
.pipe(plumber({errorHandler: notify.onError('<%= error.message %>')}))
.pipe(ejs({
site: JSON.parse(fs.readFileSync( src.html + 'inc/config.json')),
pages: JSON.parse(fs.readFileSync( src.html + 'inc/pages.json'))
},
{
ext: '.html'
}
))
.pipe(gulp.dest(dist.html));
});
/**
* Sassのコンパイルと圧縮
*/
gulp.task('sass' , function(){
return gulp.src( src.scss + '*.scss' )
.pipe(cached('sass'))
.pipe(plumber({errorHandler: notify.onError('<%= error.message %>')}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer({
browsers: AUTOPREFIXER_BROWSERS
}))
.pipe(csscomb())
.pipe(sourcemaps.write('maps', {
includeContent: false,
sourceRoot: dist.css + 'maps'
}))
.pipe(gulp.dest(dist.css))
.pipe(minify())
.pipe(rename({ extname : '.min.css' }))
.pipe(gulp.dest(dist.css));
});
/**
* jsの結合・圧縮・コピーの処理まとめ
* デフォルトではWebpackの処理のみ
*/
// Webpackでjs結合
gulp.task( 'webpack', function () {
var webpackConfig = require('./webpack.config.js');
gulp.src( src.js + '*.js')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest(dist.js));
});
// concatでjs結合(結合ファイルは指定)
gulp.task( 'js_concat', function () {
gulp.src([
src.js + 'xx1.js',
src.js + 'xx2.js'
])
.pipe( plumber({errorHandler: notify.onError('<%= error.message %>')}) )
.pipe( concat( 'xxx.js' ) )
.pipe(gulp.dest(dist.js))
.pipe( uglify( {
preserveComments: 'some'
} ) )
.pipe(rename({ extname : '.min.js' }))
.pipe(gulp.dest(dist.js));
});
// そのまま出力したいjsファイルを指定して出力領域にそのまま出力と圧縮しての出力
gulp.task( 'js_copy', function () {
gulp.src([
src.js + 'xx1.js',
src.js + 'xx2.js'
])
.pipe( plumber({errorHandler: notify.onError('<%= error.message %>')}) )
.pipe(gulp.dest(dist.js))
.pipe( uglify( {
preserveComments: 'some'
} ) )
.pipe(rename({ extname : '.min.js' }))
.pipe(gulp.dest(dist.js));
});
// 処理をまとめて実行
gulp.task('js', function() {
gulp.start( 'webpack' );
// gulp.start( 'js_concat' );
// gulp.start( 'js_copy' );
});
/**
* imageminで画像を圧縮
* changedを使用して変更のあった画像だけ処理
* PNGは圧縮率の高いpngquantを使用
*/
gulp.task( 'imagemin', function () {
return gulp.src( [ src.img + '**/*' ] )
.pipe(changed( dist.img ))
.pipe(imagemin({
progressive: true,
svgoPlugins: [
{ removeViewBox: false },
{ cleanupIDs: false }
],
use: [
pngquant({
quality: '65-80',
speed: 1
})()
]
}))
.pipe(gulp.dest( dist.img ));
});
/**
* 指定フォルダ内のSVGファイルからIconfont作成
* 読み込み用のscssファイルとStyleGuide用のscssファイルを生成
* ファイル監視はせずに手動実行
*/
gulp.task('iconfont', function(){
var fontName = 'icon';
return gulp.src([ src.font + '*.svg'])
.pipe(iconfont({
fontName: fontName,
prependUnicode: true,
formats: ['ttf', 'eot', 'woff']
}))
.on('glyphs', function(codepoints, options) {
var engine = 'lodash';
var templatePath = src.font + 'template/';
var templateName = '_icon';
var consolidateOptions = {
glyphs: codepoints,
fontName: fontName,
fontPath: '../fonts/',
className: 'icon'
};
//scss生成
gulp.src(templatePath + templateName + '_base.scss')
.pipe(consolidate(engine, consolidateOptions))
.pipe(rename({ basename: '_iconfont' }))
.pipe(gulp.dest(src.scss + 'base/'));
//mixin用のscss生成
gulp.src(templatePath + templateName + '_mixin.scss')
.pipe(consolidate(engine, consolidateOptions))
.pipe(rename({ basename: '_iconfont' }))
.pipe(gulp.dest(src.scss + 'mixin/'));
//StyleGuide用のscss生成
gulp.src(templatePath + templateName + '_doc.scss')
.pipe(consolidate(engine, consolidateOptions))
.pipe(rename({ basename:'_icon' }))
.pipe(gulp.dest(src.base + 'docs/'));
})
.pipe(gulp.dest(dist.font));
});
/**
* Gulpからhologramを実行してスタイルガイド作成
* そのままGithub Pages用に[docs]にコピー
*/
gulp.task('hologram', ['styleguide'], function() {
gulp.src("./htdocs/_docs/**/*")
.pipe(gulp.dest("./docs"));
});
gulp.task('styleguide', function() {
return gulp.src( src.hologram + 'config.yml')
.pipe(hologram());
});
/**
* Lint系のタスク
* ファイル監視はせずに手動実行
*/
// htmlLint
gulp.task('htmllint', function() {
gulp.src(dist.base + '**/*.html')
.pipe(htmlhint())
.pipe(htmlhint.reporter())
});
// StyleStats
// gulp.task('stylestats', function () {
// gulp.src('./htdocs/css/*.min.css')
// .pipe(stylestats());
// });
// jshint
gulp.task('jshint', function() {
gulp.src([ dist.js + '*.js', '!' + dist.js + '*.min.js'])
.pipe( jshint() )
.pipe( jshint.reporter( 'jshint-stylish' ) );
});
// 処理をまとめて実行
gulp.task('lint', function() {
gulp.start( 'htmllint' );
// gulp.start( 'stylestats' );
gulp.start( 'jshint' );
});
/*========================================*/
/* Server / Watch
/*========================================*/
// ローカルサーバーの起動
// --------------------
gulp.task('server', function() {
browserSync({
server: {
baseDir: dist.base
}
});
});
// ファイル監視
// --------------------
gulp.task('watch', function() {
// 出力領域が更新されたらオートリロード
watch([
dist.base + '**/*.html',
dist.base + '**/*.css',
dist.base + '**/*.js',
dist.base + '**/*.jpg',
dist.base + '**/*.png',
dist.base + '**/*.svg',
], function (){
browserSync.reload();
});
// 開発環境のファイルを監視
watch( src.base + '**/*.ejs' , function () {
gulp.start( 'ejs' );
});
watch( src.scss + '**/*.scss' , function () {
gulp.start( 'sass' );
});
watch( src.js + '**/*.js' , function () {
gulp.start( 'js' );
});
watch( src.img + '**/*.*' , function () {
gulp.start( 'imagemin' );
});
watch( [ src.docs + '*.scss', src.hologram + '**/*' ] , function () {
gulp.start( 'hologram' );
});
});
// gulpコマンドでサーバー起動とファイル監視
gulp.task('default', ['server', 'watch'] );