-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
126 lines (98 loc) · 3.86 KB
/
gulpfile.js
File metadata and controls
126 lines (98 loc) · 3.86 KB
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
var gulp = require('gulp'),
watch = require('gulp-watch'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
browserify = require('browserify'),
collapse = require('bundle-collapser/plugin'),
watchify = require('watchify'),
literalify = require('literalify'),
minifycss = require('gulp-minify-css'),
sass = require('gulp-sass'),
reactify = require('reactify'),
uglify = require('gulp-uglify'),
gulpIf = require('gulp-if'),
mocha = require('gulp-mocha'),
cssSrc = './src/sass/**/*.scss',
jsIndex = './src/js/index.jsx.js',
jsSpecs = './specs/**/*.js',
jsWatch = ['./src/js/**/*.js', jsSpecs],
browserifyTaskGen = function (isDev, isWatching) {
process.env.NODE_ENV = isDev ? 'development' : 'production';
return function () {
var outputPath = './build/' + (isDev ? 'dev' : 'prod') + '/js/',
outputName = 'bundle.js',
bundler = browserify({
entries: [jsIndex], // Only need initial file, browserify finds the deps
transform: [
reactify,
literalify.configure({
atob: 'window.atob'
})],
debug: isDev,
cache: {}, packageCache: {}, fullPaths: isDev && isWatching // Requirement of watchify
});
if (isDev && isWatching) {
bundler = watchify(bundler).on('update', function () { // When any files update
var updateStart = Date.now();
console.log('Updating!');
bundler.bundle() // Create new bundle that uses the cache for high performance
.pipe(source(outputName))
.pipe(gulp.dest(outputPath));
console.log('Updated ' + outputName + '!', (Date.now() - updateStart) + 'ms');
});
}
if (!isDev) {
bundler = bundler.plugin(collapse);
}
return bundler
.bundle() // Create the initial bundle when starting the task
.pipe(source(outputName))
.pipe(buffer())
.pipe(gulpIf(!isDev, uglify()))
.pipe(gulp.dest(outputPath));
};
},
cssTaskGen = function (isDev) {
var outputPath = './build/' + (isDev ? 'dev' : 'prod') + '/css/';
return function () {
return gulp.src(cssSrc)
.pipe(sass({
errLogToConsole: true,
style: 'nested'
}))
.pipe(gulpIf(!isDev, minifycss()))
.pipe(gulp.dest(outputPath));
};
};
gulp.task('js-watch', browserifyTaskGen(true, true));
gulp.task('js-dev', browserifyTaskGen(true, false));
gulp.task('css-dev', cssTaskGen(true));
gulp.task('css-watch', ['css-dev'], function () {
watch(cssSrc, function () {
gulp.start('css-dev');
});
});
gulp.task('html-dev', function () {
gulp.src('./src/html/*')
.pipe(gulp.dest('./build/dev/'));
});
/* test */
gulp.task('test', function () {
return gulp.src('./specs/**/*-spec.js', {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha());
});
gulp.task('test-watch', function () {
gulp.watch(jsWatch, ['test']);
});
gulp.task('watch', ['test', 'test-watch', 'css-watch', 'js-watch']);
gulp.task('default', ['watch']);
gulp.task('build-dev', ['html-dev', 'js-dev', 'css-dev']);
/* prod tasks */
gulp.task('js-prod', browserifyTaskGen(false, false));
gulp.task('css-prod', cssTaskGen(false));
gulp.task('html-prod', function () {
gulp.src('./src/html/*')
.pipe(gulp.dest('./build/prod/'));
});
gulp.task('build-prod', ['html-prod', 'js-prod', 'css-prod']);