forked from library-miner/library-miner-frontyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
426 lines (368 loc) · 12.4 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
/**
*
* gulp build - build for development
* gulp watch - build and watch files for change
* gulp - default task [watch]
* gulp build --dist - build for production
* gulp browser-sync - create http server for testing
* gulp bump --major - bump major version
* gulp bump --minor - bump minor version
* gulp bump --patch - bump patch version
*
*/
var del = require('del'),
path = require('path'),
gulp = require('gulp'),
gutil = require('gulp-util'),
concat = require('gulp-concat'),
browserSync = require('browser-sync').create(),
historyApiFallback = require('connect-history-api-fallback'),
uglify = require('gulp-uglify'),
sass = require('gulp-sass'),
postcss = require('gulp-postcss'),
assets = require('postcss-assets'),
autoprefixer = require('autoprefixer'),
cssnano = require('cssnano'),
file = require('gulp-file'),
modernizr = require('modernizr'),
sourcemaps = require('gulp-sourcemaps'),
notify = require('gulp-notify'),
notifier = require('node-notifier'),
extend = require('gulp-extend'),
jsonlint = require('gulp-jsonlint'),
minimist = require('minimist'),
gulpif = require('gulp-if'),
runSequence = require('run-sequence'),
eslint = require('gulp-eslint'),
webpack = require('webpack'),
ProgressBarPlugin = require('progress-bar-webpack-plugin'),
bump = require('gulp-bump'),
jeditor = require('gulp-json-editor'),
moment = require('moment'),
modernConfig = require('./modernizr-config.json'),
webpackConfig = require('./webpack.config.js'),
myConfig = Object.create(webpackConfig),
// get configuration
config = require('./config.json'),
rootFiles = config.root,
scssIncludePaths = config.scssIncludePaths,
cssVendor = config.cssVendor,
// parse parameters
argv = minimist(process.argv.slice(2), { boolean: true });
/**
*
* Build config
*
*/
var BUILD_DIR = 'website',
AUTO_PREFIXER_RULES = ['last 2 versions'];
/**
*
* Helper variables
*
*/
var TASK_NOTIFICATION = false,
LIVE_RELOAD = false,
MODERNIZR_LIB,
BUMP_TYPE;
/**
*
* Webpack config
*
*/
myConfig.output = {
path: BUILD_DIR + '/scripts',
publicPath: 'scripts/',
filename: '[name].js',
sourceMapFilename: 'maps/[file].map'
};
myConfig.plugins = [
new ProgressBarPlugin()
];
if (argv.dist) {
myConfig.plugins.push(new webpack.optimize.UglifyJsPlugin());
} else {
myConfig.debug = true;
myConfig.devtool = '#cheap-module-source-map';
}
/**
*
* Server
*
*/
gulp.task('browser-sync', function () {
browserSync.init({
server: {
baseDir: './' + BUILD_DIR,
middleware: [historyApiFallback()]
}
});
});
/**
*
* Bump version
*
*/
gulp.task('bump', function (cb) {
if (argv.major) {
BUMP_TYPE = 'major';
} else if (argv.minor) {
BUMP_TYPE = 'minor';
} else if (argv.patch) {
BUMP_TYPE = 'patch';
} else {
cb();
gutil.log(gutil.colors.blue('Specify valid semver version type to bump!'));
return;
}
runSequence('_version-timestamp', '_version-bump', cb);
});
gulp.task('_version-timestamp', function () {
return gulp.src('./src/scripts/data/version.json')
.pipe(jeditor({ 'time': moment().format('DD.MM.YYYY HH:mm:ss (ZZ)') }))
.pipe(gulp.dest('./src/scripts/data/'));
});
gulp.task('_version-bump', function () {
return gulp.src([
'./package.json',
'./src/scripts/data/version.json'
], { base: './' })
.pipe(bump({ type: BUMP_TYPE }))
.pipe(gulp.dest('./'));
});
/**
*
* Clean task
*
*/
gulp.task('_clean', function () {
return del([
BUILD_DIR + '/css/*.*',
BUILD_DIR + '/css/assets/*.*',
BUILD_DIR + '/css/vendor/*.css',
BUILD_DIR + '/css/vendor/*.map',
BUILD_DIR + '/templates/**',
BUILD_DIR + '/scripts/**'
], { force: true });
});
/**
*
* Build tasks
*
*/
// 本番環境では ENV=production gulp などでビルド
var environment = process.env.ENV || 'development';
var CONSTANT_JSON = 'environments/environment.json';
gulp.task('setup-env', function () {
return gulp
.src(CONSTANT_JSON)
.pipe(gulp.dest("./src/scripts/data"))
});
// Build main css
gulp.task('_css-build', function () {
return gulp.src('src/scss/**/*.scss')
.pipe(gulpif(!argv.dist, sourcemaps.init()))
.pipe(sass({ includePaths: scssIncludePaths })
.on('error', notify.onError('Error: <%= error.message %>')))
.pipe(gulpif(!argv.dist, postcss([
assets({ basePath: BUILD_DIR }),
autoprefixer({ browsers: AUTO_PREFIXER_RULES })
])))
.pipe(gulpif(argv.dist, postcss([
assets({ basePath: BUILD_DIR }),
autoprefixer({ browsers: AUTO_PREFIXER_RULES }),
cssnano
])))
.pipe(gulpif(!argv.dist, sourcemaps.write('./')))
.pipe(gulp.dest(BUILD_DIR + '/css/'))
.pipe(gulpif(LIVE_RELOAD, browserSync.stream()))
.pipe(gulpif(TASK_NOTIFICATION, notify({ message: 'CSS build completed.', onLast: true })));
});
// Build vendor css
gulp.task('_css-vendor-build', function () {
return gulp.src(cssVendor)
.pipe(gulpif(!argv.dist, sourcemaps.init()))
.pipe(concat('vendor.css'))
.pipe(gulpif(!argv.dist, postcss([
autoprefixer({ browsers: AUTO_PREFIXER_RULES })
])))
.pipe(gulpif(argv.dist, postcss([
autoprefixer({ browsers: AUTO_PREFIXER_RULES }),
cssnano
])))
.pipe(gulpif(!argv.dist, sourcemaps.write('./')))
.pipe(gulp.dest(BUILD_DIR + '/css/vendor/'))
.pipe(gulpif(LIVE_RELOAD, browserSync.stream()))
.pipe(gulpif(TASK_NOTIFICATION, notify({ message: 'Vendor CSS build completed.', onLast: true })));
});
// Build js files
var createWebpackCb = function (cb) {
var calledOnce = false;
var webpackCb = function (err, stats) {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack]', stats.toString({ chunks: false, colors: true }));
if (stats.hasErrors()) {
if (!TASK_NOTIFICATION) {
throw new gutil.PluginError('webpack', new Error('JavaScript build error.'));
} else {
notifier.notify({
title: 'Error running Gulp',
message: 'JavaScript build error.',
icon: path.join(__dirname, 'node_modules', 'gulp-notify', 'assets', 'gulp-error.png'),
sound: 'Frog'
});
gutil.log(
gutil.colors.cyan('gulp-notify:'),
gutil.colors.blue('[Error running Gulp]'),
gutil.colors.green('JavaScript build error.')
);
gutil.log(
gutil.colors.white('Finished'),
gutil.colors.cyan('\'_js-watch\''),
gutil.colors.white('after'),
gutil.colors.magenta(stats.toJson().time + ' ms')
);
}
} else {
if (TASK_NOTIFICATION) {
notifier.notify({
title: 'Gulp notification',
message: 'JavaScript build completed.',
icon: path.join(__dirname, 'node_modules', 'gulp-notify', 'assets', 'gulp.png')
});
gutil.log(
gutil.colors.cyan('gulp-notify:'),
gutil.colors.blue('[Gulp notification]'),
gutil.colors.green('JavaScript build completed.')
);
gutil.log(
gutil.colors.white('Finished'),
gutil.colors.cyan('\'_js-watch\''),
gutil.colors.white('after'),
gutil.colors.magenta(stats.toJson().time + ' ms')
);
}
if (LIVE_RELOAD) {
browserSync.reload();
}
}
if (!calledOnce) {
calledOnce = true;
cb();
}
};
return function (err, stats) {
runSequence('_js-lint', function () {
webpackCb(err, stats);
});
};
};
var compiler = webpack(myConfig);
gulp.task('_js-build', function (cb) {
compiler.run(createWebpackCb(cb));
});
gulp.task('_js-watch', function (cb) {
compiler.watch({}, createWebpackCb(cb));
});
// Build modernizr js
gulp.task('_modernizr-generate', function (cb) {
modernizr.build(modernConfig, function (result) {
MODERNIZR_LIB = result;
cb();
});
});
gulp.task('_modernizr-build', ['_modernizr-generate'], function () {
return file('modernizr.js', MODERNIZR_LIB, { src: true })
.pipe(gulpif(argv.dist, uglify()))
.pipe(gulp.dest(BUILD_DIR + '/scripts/'));
});
// Copy templates
gulp.task('_templates-build', function () {
return gulp.src('src/templates/**/*.html')
.pipe(gulp.dest(BUILD_DIR + '/templates/'))
.pipe(gulpif(LIVE_RELOAD, browserSync.stream()))
.pipe(gulpif(TASK_NOTIFICATION, notify({ message: 'Template build completed.', onLast: true })));
});
// Copy root files
gulp.task('_root-files-build', function () {
return gulp.src(rootFiles)
.pipe(gulp.dest(BUILD_DIR + '/'))
.pipe(gulpif(LIVE_RELOAD, browserSync.stream()))
.pipe(gulpif(TASK_NOTIFICATION, notify({ message: 'Root files build completed.', onLast: true })));
});
// Build data
gulp.task('_data-build', function () {
return gulp.src('src/data/**/*.json')
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(jsonlint.failOnError())
.on('error', notify.onError('JSON data build error.'))
.pipe(extend('data.json'))
.pipe(gulp.dest(BUILD_DIR + '/data/'))
.pipe(gulpif(LIVE_RELOAD, browserSync.stream()))
.pipe(gulpif(TASK_NOTIFICATION, notify({ message: 'Data build completed.', onLast: true })));
});
/**
*
* ESLint task
*
*/
gulp.task('_js-lint', function () {
return gulp.src(['src/scripts/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.results(function (results) {
if (results.errorCount === 0 && results.warningCount === 0) {
return;
}
notifier.notify({
title: 'Error running Gulp',
message: 'JavaScript ESLint error.',
icon: path.join(__dirname, 'node_modules', 'gulp-notify', 'assets', 'gulp-error.png'),
sound: 'Frog'
});
}));
});
/**
*
* Main build task
*
*/
gulp.task('_build', ['setup-env', '_css-build', '_css-vendor-build', '_templates-build', '_modernizr-build',
'_root-files-build', '_data-build'], function () {
notifier.notify({
title: 'Gulp notification',
message: 'Build completed.',
icon: path.join(__dirname, 'node_modules', 'gulp-notify', 'assets', 'gulp.png')
});
});
gulp.task('build', function (cb) {
runSequence('_clean', '_js-build', '_build', cb);
});
/**
*
* Watch task
*
*/
gulp.task('_watch', function () {
gulp.watch('src/scss/**/*.scss', ['_css-build']);
gulp.watch('src/templates/**/*.html', ['_templates-build']);
gulp.watch(rootFiles, ['_root-files-build']);
gulp.watch('src/data/**/*.json', ['_data-build']);
});
gulp.task('watch', function (cb) {
runSequence('_clean', '_js-watch', '_build', '_watch', 'browser-sync', function () {
TASK_NOTIFICATION = true;
LIVE_RELOAD = true;
cb();
});
});
/**
*
* Set DEFAULT task
*
*/
gulp.task('default', ['watch']);