forked from danielktaylor/fabric-js-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
159 lines (135 loc) · 3.98 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
/* ----- plugins ----- */
var gulp = require('gulp');
var browserify = require('browserify');
var clean = require('gulp-clean');
var merge = require('merge-stream');
var taskListing = require('gulp-task-listing');
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var uglifycss = require('gulp-uglifycss');
var rename = require('gulp-rename');
var webserver = require('gulp-webserver');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gulpif = require('gulp-if');
var del = require('del');
var runSequence = require('run-sequence');
var processes = require('child_process');
/* ----- build tasks ----- */
var exec = require('child_process').exec;
var isDebug;
// Handle errors
function errorHandler (error) {
console.log(error);
this.emit('end');
}
// copy correct JS config file
gulp.task('x-js-config', function() {
if (isDebug === true) {
gulp.src(['src/js/config/development.js'])
.pipe(rename("config.js"))
.pipe(gulp.dest('src/js/app/'));
} else {
gulp.src(['src/js/config/production.js'])
.pipe(rename("config.js"))
.pipe(gulp.dest('src/js/app/'));
}
});
// dev webserver
gulp.task('x-webserver', function() {
gulp.src('build/')
.pipe(webserver({
livereload: true,
directoryListing: false,
open: true
}));
});
// watch files for changes
gulp.task('x-watch', function() {
gulp.watch('src/js/*.js', ['lint', 'x-browserify']);
gulp.watch('src/js/app/*.js', ['lint', 'x-browserify']);
gulp.watch('src/scss/*.scss', ['x-sass']);
gulp.watch('src/js/config/*.js', ['x-js-config']);
gulp.watch(['src/images/**/*',
'src/js/lib/*',
'src/css/lib/*',
'src/html/*'
], ['x-copy']);
});
// compile JS
gulp.task('x-browserify', function() {
return browserify({
entries: 'src/js/editor.js',
debug: isDebug
})
.bundle()
.on("error", errorHandler)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulpif(!isDebug, uglify()))
.pipe(gulp.dest('build/js'));
});
// compile SASS
gulp.task('x-sass', function() {
return gulp.src('src/scss/*.scss')
.pipe(sass()
.on('error', sass.logError))
.pipe(gulp.dest('build/css'));
});
// copy supporting files
gulp.task('x-copy', function() {
var images = gulp.src(['src/images/**/*'])
.pipe(gulp.dest('build/images'));
var css = gulp.src(['src/css/lib/*.css'])
.pipe(gulp.dest('build/css/lib'));
var js = gulp.src(['src/js/lib/*.js'])
.pipe(gulp.dest('build/js/lib'));
var html = gulp.src(['src/html/*'])
.pipe(gulp.dest('build'));
return merge(images, css, js, html);
});
// minify assets
gulp.task('x-minify', function() {
return gulp.src('build/css/*.css')
.pipe(uglifycss({
"max-line-len": 80
}))
.pipe(gulp.dest('build/css'));
});
/* ----- user tasks ----- */
// cleanup the build directory
gulp.task('clean', function() {
return del(['./build/*']);
});
// lint javascript
gulp.task('lint', function() {
return gulp.src(['src/js/*.js', 'src/js/app/*.js'], {base: 'src/js/'})
.pipe(jshint())
.on('error', errorHandler)
.pipe(jshint.reporter('default'));
});
// build production assets
gulp.task('prod', function(done) {
isDebug = false;
return runSequence('clean',
'x-js-config',
'x-sass',
'x-browserify',
'x-minify',
'x-copy',
done);
});
// start dev server
gulp.task('dev', [], function(done) {
isDebug = true;
return runSequence('clean',
'x-js-config',
['x-sass', 'x-browserify'],
'x-copy',
['lint', 'x-webserver', 'x-watch'],
done);
});
// default: list tasks
gulp.task('default', taskListing.withFilters(null, 'x-'));