-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
176 lines (155 loc) · 4.73 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
var gulp = require('gulp');
var browserSync = require('browser-sync');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var nodemon = require('gulp-nodemon');
var sass = require('gulp-sass');
var nano = require('gulp-cssnano');
var inject = require('gulp-inject');
var gulpif = require('gulp-if');
var babelify = require('babelify');
var rename = require('gulp-rename');
var argv = require('yargs').argv;
var del = require('del');
var requireGlobify = require('require-globify');
var watchify = require('watchify');
var glob = require('glob');
var path = require('path');
// TODO: Use these
var paths = {
SCSS: ['./app/styles/**/*.scss'],
HBS: ['./app/views/**/*.hbs'],
VIEWS_DIR: ['./app/views'],
TMP_DIR: './.tmp',
BUILD_BASE_DIR: './public',
BUILD_CSS_DIR: './public/stylesheets'
};
gulp.task('clean', function() {
return del(['./public/stylesheets/*.*']);
});
gulp.task('sass:compile', ['clean'], function(){
return gulp.src(['./app/styles/**/*.scss'])
.pipe(sass.sync().on('error', function(err){
sass.logError(err);
}))
.pipe(gulpif(argv.production, nano()))
.pipe(gulpif(argv.production, rename({suffix: '.min'})))
.pipe(gulp.dest('./public/stylesheets'))
.pipe(browserSync.stream());
});
gulp.task('sass:watch', function () {
gulp.watch('./app/styles/**/*.scss', ['sass:compile']);
});
gulp.task('inject:assets', ['sass:compile'], function() {
var target = gulp.src(['./app/views/*.hbs']);
var source = gulp.src(['./public/stylesheets/*.css', './public/js/ui.js'], {read:false});
return target.pipe(inject(source))
.pipe(gulp.dest('./app/views/'));
});
function runWatchify(file, output, standaloneLib) {
output += '.js';
var b = browserify({
debug: !process.env.production,
extensions: ['.jsx'],
cache: {},
packageCache: {},
fullPaths: true,
standalone: standaloneLib,
paths:['public/vendorJs']
});
b.add(file);
b = watchify(b);
b.transform(babelify, {presets: ["es2015", "react"]})
.transform(requireGlobify)
.on('update', function(){
browserifyBundle(b, output).on('end', function(){
browserSync.reload({ stream: false });
});
})
.on('log', function(msg){
console.log(msg);
});
return browserifyBundle(b, output);
}
function runBrowserifyProduction(file, output, standaloneLib) {
output += '.js';
var b = browserify({
debug: false,
extensions: ['.jsx'],
cache: {},
packageCache: {},
fullPaths: true,
standalone: standaloneLib,
paths:['public/vendorJs']
});
b.add(file);
b.transform(babelify, {presets: ["es2015", "react"]})
.transform(requireGlobify)
.bundle()
.pipe(source(output))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./public/js'));
}
function browserifyBundle(b, output) {
return b.bundle()
.on('error', function(e){
console.log(e.message);
this.emit('end');
})
.pipe(source(output))
.pipe(gulp.dest('./public/js'));
}
gulp.task('bundle', function(){
var files = glob.sync('./app/components/custom/*.jsx');
runWatchify('app/main.jsx', 'ui', 'LensUI');
runWatchify('app/published.jsx', 'published', 'LensPublished');
files.forEach(function(file) {
return runWatchify(file, path.basename(file, '.jsx'), path.basename(file, '.jsx'));
})
});
gulp.task('bundle:production', function(){
runBrowserifyProduction('app/main.jsx', 'ui', 'LensUI');
runBrowserifyProduction('app/published.jsx', 'published', 'LensPublished');
});
gulp.task('watch:components', function() {
gulp.watch('app/components/core/*.jsx', function(e){
if(e.type == 'deleted') {
del('public/js/' + path.basename(e.path, '.jsx') + '.js');
console.log('deleted: public/js/' + path.basename(e.path, '.jsx') + '.js');
} else if(e.type == 'added') {
console.log('Started Watching:' + path.basename(e.path));
runWatchify(e.path, path.basename(e.path, '.jsx'), path.basename(e.path, '.jsx'));
}
});
});
gulp.task('nodemon', function (cb) {
var started = false;
return nodemon({
script: 'server/main.js',
options: {
watchedExtensions: ['js']
},
ignore: ['app/**', 'public/**', './*.js']
}).on('start', function () {
if (!started) {
cb();
started = true;
}
}) .on('restart', function () {
setTimeout(function() {
browserSync.reload({ stream: false });
}, 1000);
});
});
// Add a bundle for production builds without watchify
gulp.task('build', ['inject:assets', 'sass:compile', 'bundle:production']);
gulp.task('serve', ['bundle', 'inject:assets', 'sass:watch', 'nodemon', 'watch:components'], function(){
browserSync.init(null, {
proxy: "http://localhost:7777",
files: ['public/images'],
port: 9001
});
});