forked from hotosm/oam-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
184 lines (154 loc) · 5.44 KB
/
gulpfile.js
File metadata and controls
184 lines (154 loc) · 5.44 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
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
var gulp = require('gulp');
var compass = require('gulp-compass');
var browserSync = require('browser-sync');
var runSequence = require('run-sequence');
// Used to stream bundle for further handling
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var watchify = require('watchify');
var reactify = require('reactify');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var assign = require('lodash').assign;
var clean = require('gulp-clean');
var uglify = require('gulp-uglify');
var streamify = require('gulp-streamify');
////////////////////////////////////////////////////////////////////////////////
//--------------------------- Variables --------------------------------------//
//----------------------------------------------------------------------------//
// Control whether the browser should reload.
var shouldReload = true;
// Control weather we're watching for changes.
// Basically means the watch task was called.
var shouldWatch = false;
// Environment for the script.
var ENV = 'development';
////////////////////////////////////////////////////////////////////////////////
//------------------------- Callable tasks -----------------------------------//
//----------------------------------------------------------------------------//
// Default task.
// Builds the website, watches for changes and starts browserSync.
gulp.task('default', function(done) {
shouldWatch = true;
runSequence('build', 'watch', 'browser-sync', done);
});
// Main build task
gulp.task('build', function(done) {
runSequence(['copy_files', 'scripts', 'compass'], done);
});
// Prod build task
gulp.task('prod', function(done) {
// Change environment.
ENV = 'production';
runSequence('clean', ['copy_files', 'scripts', 'compass'], done);
});
// Clean.
gulp.task('clean', function () {
return gulp.src('dist', {read: false})
.pipe(clean());
});
gulp.task('no-reload', function(done) {
shouldReload = false;
shouldWatch = true;
runSequence('build', 'watch', 'browser-sync', done);
});
////////////////////////////////////////////////////////////////////////////////
//------------------------- Browserify tasks ---------------------------------//
//--------------------- (Not directly callable) ------------------------------//
//----------------------------------------------------------------------------//
gulp.task('scripts:build', function() {
var b = browserify({
entries: ['./app/assets/scripts/main.js'],
debug: true,
transform: [reactify],
})
.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('main.js'))
.pipe(buffer());
// Source maps.
if (ENV != 'production') {
b = b.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'));
}
if (ENV == 'production') {
b = b.pipe(streamify(uglify()));
}
// Output.
b.pipe(gulp.dest('./dist/assets/scripts'));
});
gulp.task('scripts:build:watch', function() {
var watcher = watchify(browserify({
entries: ['./app/assets/scripts/main.js'],
transform: [reactify],
debug: true,
cache: {}, packageCache: {}, fullPaths: true
}));
return watcher.on('update', function () {
watcher.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('main.js'))
.pipe(buffer())
// Source maps.
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/assets/scripts'))
.on('end', browserReload);
console.log('Scripts updated');
})
.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('main.js'))
.pipe(buffer())
// Source maps.
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/assets/scripts'))
.on('end', browserReload);
});
////////////////////////////////////////////////////////////////////////////////
//--------------------------- Helper tasks -----------------------------------//
//----------------------------------------------------------------------------//
// I'm watching you!
gulp.task('watch', function() {
gulp.watch('app/assets/styles/**/*.scss', function() {
runSequence('compass', browserReload);
});
gulp.watch(['app/**/*', '!app/assets/styles/**', '!app/assets/scripts/**'], function() {
runSequence('copy_files', browserReload);
});
});
gulp.task('scripts', function(done) {
runSequence(shouldWatch ? 'scripts:build:watch' : 'scripts:build', done);
});
gulp.task('copy_files', function() {
return gulp.src(['app/**/*', '!app/assets/styles/**', '!app/assets/scripts/**'])
.pipe(gulp.dest('dist'));
});
gulp.task('compass', function() {
return gulp.src('app/assets/styles/*.scss')
.pipe(compass({
css: 'dist/assets/styles',
sass: 'app/assets/styles',
style: ENV = 'production' ? 'compressed' : 'expanded',
sourcemap: ENV = 'production' ? false : true,
bundle_exec: true
}));
});
// Setup browserSync.
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./dist"
}
});
});
////////////////////////////////////////////////////////////////////////////////
//------------------------- Helper functions ---------------------------------//
//----------------------------------------------------------------------------//
function browserReload() {
if (shouldReload) {
browserSync.reload();
}
}