-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
265 lines (223 loc) · 6.54 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
'use strict';
var gulp = require('gulp');
var del = require('del');
var path = require('path');
// Load plugins
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var sourceFile = './app/scripts/app.js';
var destFolder = './dist/scripts';
var destFileName = 'app.js';
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var eslint = require('gulp-eslint');
var jscs = require('gulp-jscs');
// Styles
gulp.task('styles', ['sass', 'moveCss']);
gulp.task('moveCss', ['clean'], function() {
// the base option sets the relative root for the set of files,
// preserving the folder structure
gulp.src(['./app/styles/**/*.css'], { base: './app/styles/' })
.pipe(gulp.dest('dist/styles'));
});
gulp.task('sass', function() {
return $.rubySass('./app/styles', {
style: 'expanded',
precision: 10,
loadPath: ['app/bower_components'],
})
.pipe($.autoprefixer('last 1 version'))
.pipe(gulp.dest('dist/styles'))
.pipe($.size());
});
var bundler = watchify(browserify({
entries: [sourceFile],
debug: true,
insertGlobals: true,
cache: {},
packageCache: {},
fullPaths: true,
}));
bundler.on('update', rebundle);
bundler.on('log', $.util.log);
function rebundle() {
return bundler.bundle()
// log errors if they happen
.on('error', $.util.log.bind($.util, 'Browserify Error'))
.pipe(source(destFileName))
.pipe(gulp.dest(destFolder))
.on('end', function() {
reload();
});
}
// Scripts
gulp.task('scripts', rebundle);
gulp.task('buildScripts', function() {
return browserify(sourceFile)
.bundle()
.pipe(source(destFileName))
.pipe(gulp.dest('dist/scripts'));
});
// HTML
gulp.task('html', function() {
return gulp.src('app/*.html')
.pipe($.useref())
.pipe(gulp.dest('dist'))
.pipe($.size());
});
// Images
gulp.task('images', function() {
return gulp.src('app/images/**/*')
.pipe($.cache($.imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true,
})))
.pipe(gulp.dest('dist/images'))
.pipe($.size());
});
// Fonts
gulp.task('fonts', function() {
return gulp.src(require('main-bower-files')({
filter: '**/*.{eot,svg,ttf,woff,woff2}',
}).concat('app/fonts/**/*'))
.pipe(gulp.dest('dist/fonts'));
});
// Clean
gulp.task('clean', function(cb) {
$.cache.clearAll();
cb(del.sync(['dist/styles', 'dist/scripts', 'dist/images']));
});
// Bundle
gulp.task('bundle', ['styles', 'scripts', 'bower'], function() {
return gulp.src('./app/*.html')
.pipe($.useref.assets())
.pipe($.useref.restore())
.pipe($.useref())
.pipe(gulp.dest('dist'));
});
gulp.task('buildBundle', ['styles', 'buildScripts', 'moveLibraries', 'bower'], function() {
return gulp.src('./app/*.html')
.pipe($.useref.assets())
.pipe($.useref.restore())
.pipe($.useref())
.pipe(gulp.dest('dist'));
});
// Move JS Files and Libraries
gulp.task('moveLibraries', ['clean'], function() {
// the base option sets the relative root for the set of files,
// preserving the folder structure
gulp.src(['./app/scripts/**/*.js'], { base: './app/scripts/' })
.pipe(gulp.dest('dist/scripts'));
});
// Bower helper
gulp.task('bower', function() {
gulp.src('app/bower_components/**/*.js', {
base: 'app/bower_components',
})
.pipe(gulp.dest('dist/bower_components/'));
});
gulp.task('json', function() {
gulp.src('app/scripts/json/**/*.json', {
base: 'app/scripts',
})
.pipe(gulp.dest('dist/scripts/'));
});
// Robots.txt and favicon.ico
gulp.task('extras', function() {
return gulp.src(['app/*.txt', 'app/*.ico'])
.pipe(gulp.dest('dist/'))
.pipe($.size());
});
// Watch
gulp.task('watch', ['html', 'fonts', 'bundle'], function() {
browserSync({
notify: false,
logPrefix: 'BS',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: ['dist', 'app'],
});
// Watch .json files
gulp.watch('app/scripts/**/*.json', ['json']);
// Watch .html files
gulp.watch('app/*.html', ['html']);
gulp.watch(['app/styles/**/*.scss', 'app/styles/**/*.css'], ['styles', 'scripts', reload]);
// Watch image files
gulp.watch('app/images/**/*', reload);
});
// Lint
gulp.task('lint', function() {
return gulp.src(['app/scripts/**/*.js'])
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe(eslint({
baseConfig: {
ecmaFeatures: {
jsx: true,
},
extends: [
'eslint:recommended',
],
},
envs: [
'browser',
'jest',
'node',
],
plugins: [
'react',
],
rules: {
'comma-dangle': 0,
// 'jsx-quotes': 1,
// 'react/display-name': 1,
'react/jsx-boolean-value': 1,
'react/jsx-curly-spacing': 1,
'react/jsx-max-props-per-line': 1,
'react/jsx-no-duplicate-props': 1,
'react/jsx-no-undef': 1,
'react/jsx-sort-prop-types': 1,
'react/jsx-sort-props': 1,
'react/jsx-uses-react': 1,
'react/jsx-uses-vars': 1,
'react/no-danger': 1,
'react/no-did-mount-set-state': 1,
'react/no-did-update-set-state': 1,
'react/no-multi-comp': 1,
'react/no-unknown-property': 1,
// 'react/prop-types': 1,
'react/react-in-jsx-scope': 1,
'react/require-extension': 1,
'react/self-closing-comp': 1,
'react/sort-comp': 1,
'react/wrap-multilines': 1,
},
}))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failOnError last.
.pipe(eslint.failOnError());
});
// This doesn't work yet. For now: jscs --fix app/scripts/**/*.js
// gulp.task('autoformat', function () {
// return gulp.src(['app/scripts/**/*.js'])
// .pipe(jscs({fix: true}))
// .pipe(gulp.dest('src'));
// });
// Build
gulp.task('build', ['html', 'buildBundle', 'images', 'fonts', 'extras'], function() {
gulp.src('dist/scripts/app.js')
.pipe($.uglify())
.pipe($.stripDebug())
.pipe(gulp.dest('dist/scripts'));
});
// Default task
// TODO: add jest after build
gulp.task('default', ['clean', 'lint', 'build']);