forked from junni-inc/next.junni.co.jp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
203 lines (142 loc) · 3.39 KB
/
gulpfile.babel.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
// gulp
import gulp from 'gulp';
import gulpIf from 'gulp-if';
// utils
import browserSync from 'browser-sync';
import plumber from 'gulp-plumber';
import del from 'del';
// css
import gulpSass from 'gulp-sass';
import dartSass from 'sass';
import cssmin from 'gulp-cssmin';
import autoprefixer from 'gulp-autoprefixer';
const sass = gulpSass( dartSass );
// eslint
import eslint from 'gulp-eslint';
// ts
import webpackStream from 'webpack-stream';
import webpack from 'webpack';
import webpackConfig from './webpack.config.js';
import fancyLog from 'fancy-log';
import supportsColor from 'supports-color';
// variables
const srcDir = './src';
const outDir = './public';
const isFixed = ( file ) => {
return file.eslint != null && file.eslint.fixed;
}
const lint = ( cb ) => {
let paths = [ srcDir ];
for ( let i = 0; i < paths.length; i ++ ) {
gulp.src( paths[ i ] + '**/*.ts' )
.pipe( eslint( { useEslintrc: true, fix: true } ) ) // .eslintrc を参照
.pipe( eslint.format() )
.pipe( gulpIf( isFixed, gulp.dest( paths[ i ] ) ) )
.pipe( eslint.failAfterError() );
}
cb();
}
const setDevMode = ( cb ) => {
webpackConfig.mode = 'development';
cb();
}
const setPrdMode = ( cb ) => {
webpackConfig.mode = 'production';
webpackConfig.watch = false;
cb();
}
const buildWebpack = ( cb ) => {
webpackConfig.entry.main = srcDir + '/ts/main.ts';
webpackConfig.output.filename = 'script.js';
webpackStream( webpackConfig, webpack, ( err, stats ) => {
if (err) {
console.log(err);
return;
}
stats = stats || {};
var statusLog = stats.toString({
colors: supportsColor.stdout.hasBasic,
hash: false,
timings: false,
chunks: false,
chunkModules: false,
modules: false,
children: true,
version: true,
cached: false,
cachedAssets: false,
reasons: false,
source: false,
errorDetails: false
});
if (statusLog) {
fancyLog(statusLog);
}
reload();
})
.on( 'error', function() { this.emit( 'end' ) } )
.pipe( gulp.dest( outDir + '/js/' ) )
cb();
}
const buildSass = () => {
return gulp.src( srcDir + '/scss/style.scss' )
.pipe( plumber( {
errorHandler: ( err ) => {
console.log( err.messageFormatted );
this.emit('end');
} } ) )
.pipe( sass() )
.pipe( autoprefixer( [ 'last 2 versions'] ) )
.pipe( cssmin() )
.pipe( gulp.dest( outDir + '/css/' ) )
.pipe( browserSync.stream() );
}
const copy = ( c ) => {
gulp.src( [srcDir + '/html/**/*'] ).pipe( gulp.dest( outDir ) );
gulp.src( [srcDir + '/assets/**/*'] ).pipe( gulp.dest( outDir + '/assets/' ) );
c();
}
const brSync = () => {
browserSync.init( {
server: {
baseDir: outDir,
index: 'index.html'
},
open: true,
notify: false,
ghostMode: false
} );
}
const clean = ( c ) => {
del(
[ outDir ],
{
force: true,
}
).then( () => {
c();
} );
}
const reload = ( cb ) => {
browserSync.reload();
cb && cb();
}
const watch = () => {
gulp.watch( srcDir + '/scss/**/*', gulp.series( buildSass ) );
gulp.watch( srcDir + '/html/**/*', gulp.series( copy, reload ) );
gulp.watch( srcDir + '/assets/**/*', gulp.series( copy, reload ) );
}
exports.default = gulp.series(
clean,
setDevMode,
gulp.parallel( buildWebpack, buildSass ),
copy,
gulp.parallel( brSync, watch )
);
exports.build = gulp.series(
clean,
setPrdMode,
gulp.parallel( buildWebpack, buildSass ),
copy,
)
exports.lint = gulp.task( lint );