-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
142 lines (123 loc) · 3.68 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
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var jade = require('gulp-jade');
var watchify = require('watchify');
var browserify = require('browserify');
var reactify = require('reactify');
var streamify = require('gulp-streamify');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var minifer = require('gulp-uglify/minifier');
var uglifyjs = require('uglify-js');
var es = require('event-stream');
var rename = require('gulp-rename');
var path = {
JS: './frontend/js/**/*.js',
JS_PATH: './frontend/js/',
ENTRIES: ['app.js','chat.js'],
ENTRY_APP: 'app.js',
ENTRY_CHAT: 'chat.js',
SCSS:'frontend/scss/*.scss',
SCSS_PATH:'frontend/scss',
CSS_OUT:'src/main/webapp/style',
JADE_WATCH: 'frontend/jade/**/*.jade', //watch to see if layouts change
JADE_VIEWS: 'frontend/jade/views/*.jade', //but only compile views so we won't create empty layout htmls
HTML_OUT:'src/main/webapp',
DEST_BUILD:'src/main/webapp/js'
}
function onError(e) {
var s = "";
s += "file: " + e.fileName + "\n";
s += "descr: " + e.description + "\n";
s += "line: " + e.lineNumber + "\n";
console.log(s);
if(!e.description) {
console.log(e);
}
this.emit("end");
}
function onScssError(e) {
sass.logError(e);
this.emit("end");
}
//based on https://gist.github.com/Sigmus/9253068
function buildScript(filename, watch) {
var props = {entries: [path.JS_PATH + filename],cache: {}, packageCache: {}, debug:true};
var bundler = watch ? watchify(browserify(props)) : browserify(props);
bundler.transform(reactify);
function rebundle() {
var stream = bundler.bundle();
//there might be a way to pipe conditionally
if(process.env.NODE_ENV === 'production') {
return stream.on('error', onError)
.pipe(source(filename))
.pipe(streamify(minifer({}, uglifyjs).on('error', gutil.log)))
.pipe(rename({
extname: '.min.js'
}))
.pipe(gulp.dest(path.DEST_BUILD));
} else {
return stream.on('error', onError)
.pipe(source(filename))
.pipe(gulp.dest(path.DEST_BUILD));
}
}
bundler.on('update', function() {
rebundle();
console.log('update');
});
return rebundle();
}
gulp.task('scss', function() {
return gulp.src(path.SCSS)
.pipe(sass().on('error', sass.logError))
//.pipe( csso() ) //minimizer
.pipe(gulp.dest(path.CSS_OUT));
});
gulp.task('jade', function() {
return gulp.src(path.JADE_VIEWS)
.pipe(jade({
pretty: true,
locals: {env:process.env.NODE_ENV}
}))
.pipe(gulp.dest(path.HTML_OUT));
});
//REMOVE
gulp.task("js", function() {
var tasks = path.ENTRIES.map(function(entry) {
return browserify({
entries: [path.JS_PATH + entry],
transform: [reactify],
})
.bundle().on('error', onError)
.pipe(source(entry))
.pipe(gulp.dest(path.DEST_BUILD) );
});
return es.merge.apply(null, tasks);
});
gulp.task("js-app", function() {
return buildScript(path.ENTRY_APP, false);
});
gulp.task("js-chat", function() {
return buildScript(path.ENTRY_CHAT, false);
});
gulp.task("watch-app", function() {
return buildScript(path.ENTRY_APP, true);
});
gulp.task("watch-chat", function() {
return buildScript(path.ENTRY_CHAT, true);
});
gulp.task("set-prod-env", function() {
return process.env.NODE_ENV = 'production';
});
gulp.task('watch-all', ['js-app', 'jade', 'scss'], function() {
gulp.watch(path.JS, ['js-app']);
gulp.watch(path.SCSS, ['scss']);
gulp.watch(path.JADE_WATCH, ['jade']);
});
gulp.task('build', ['set-prod-env','js-app','jade','scss'], function(){
console.log("build done");
});
gulp.task('default', ['watch-all']);