-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
193 lines (154 loc) · 4.75 KB
/
Copy pathgulpfile.js
File metadata and controls
193 lines (154 loc) · 4.75 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
185
186
187
188
189
190
191
192
193
'use strict'
// References to gulp and all the gulp plugins
var gulp = require('gulp')
var clean = require('gulp-clean')
var concat = require('gulp-concat')
var gutil = require('gulp-util')
var imagemin = require('gulp-imagemin')
var print = require('gulp-print')
var rename = require('gulp-rename')
var sass = require('gulp-sass')
var stream = require('stream')
var streamify = require('gulp-streamify')
var template = require('gulp-template')
var uglify = require('gulp-uglify')
var watch = require('gulp-watch')
// Other node libraries
var _ = require('underscore')
var browserify = require('browserify')
var serialize = require('node-serialize')
var stream = require('stream')
var source = require('vinyl-source-stream')
// Get configuration information from the JSON files
var pkg = require('./package.json')
gutil.log(gutil.colors.yellow('Name'), gutil.colors.cyan(pkg.name))
gutil.log(gutil.colors.yellow('Version'), gutil.colors.cyan(pkg.version))
// Libraries the app depends on
var libs = _.keys(pkg.dependencies)
gutil.log("3rd party libraries: "+ libs.join(" "))
gulp.task('clean', function() {
gulp.src('build', {read: false})
.pipe(clean())
})
gulp.task('images', function(){
return gulp.src('./src/main/images/**')
.pipe(imagemin())
.pipe(gulp.dest('./build/dist/images'))
.pipe(gulp.dest('./build/dev/images'))
})
gulp.task('vendor', function() {
var b = browserify()
gutil.log("Creating a vendor bundle for:")
_.each(libs, function (name) {
gutil.log(" "+ name)
b.require(name)
})
b.require('react/addons')
return b
.bundle()
.pipe(source('vendor.js'))
.pipe(gulp.dest('./build/dev'))
.pipe(rename('vendor-'+ pkg.version +'.js'))
.pipe(gulp.dest('./build/dist'))
})
gulp.task('bundle', function() {
var b = browserify({
entries: ['./src/main/js/app.js'],
extensions: ['.jsx'],
})
_.each(libs, function (name) {
b.exclude(name)
})
b.exclude('react/addons')
return b
.bundle()
.on('error', function(err){
log_error(err)
this.emit('end') // end this stream to make gulp happy
})
.pipe(source('bundle.js'))
.pipe(gulp.dest('./build/dev'))
.pipe(rename('bundle-'+ pkg.version +'.js'))
.pipe(streamify(uglify()))
.pipe(gulp.dest('./build/dist'))
})
gulp.task('styles', function () {
return gulp.src('./src/main/scss/*.scss')
.pipe(print())
.pipe(concat('styles.scss'))
.pipe(sass({
outputStyle: 'expanded',
errLogToConsole: true
}))
.pipe(gulp.dest('./build/dev'))
.pipe(sass({
outputStyle: 'compressed',
errLogToConsole: true
}))
.pipe(rename("styles-"+ pkg.version +".css"))
.pipe(gulp.dest('./build/dist'))
})
gulp.task('config', function () {
delete require.cache[require.resolve('./config.json')]
var config = require('./config.json')
config.app_name = pkg.name
config.version = pkg.version
var config_json = serialize.serialize(config)
gutil.log("Configuration", gutil.colors.cyan(config_json))
return string_src("config.js", "_config = "+ config_json)
.pipe(print())
.pipe(gulp.dest('build/dev'))
.pipe(gulp.dest('build/dist'))
})
gulp.src('./src/index.html')
.pipe(gulp.dest("./dist"));
gulp.task('index-dev', function () {
return index_stream([css_tag('styles.css'), js_tag('vendor.js'), js_tag('bundle.js')])
.pipe(gulp.dest('build/dev'))
})
gulp.task('index-dist', function () {
return index_stream([css_tag('styles-'+ pkg.version +'.css'), js_tag('vendor-'+ pkg.version +'.js'), js_tag('bundle-'+ pkg.version +'.js')])
.pipe(gulp.dest('build/dist'))
})
gulp.task('index', ['index-dev', 'index-dist'])
gulp.task('build', ['styles', 'vendor', 'bundle', 'images', 'index', 'config'])
gulp.task('watch', ['build'], function() {
gulp.watch('config.json', ['config'])
gulp.watch('src/main/images/**', ['images'])
gulp.watch('src/main/js/**/*.js', ['bundle'])
gulp.watch('src/main/js/**/*.jsx', ['bundle'])
gulp.watch('src/main/scss/*.scss', ['styles'])
gulp.watch('src/main/index.tmpl', ['index'])
})
gulp.task('default', ['build'])
function log_error(err) {
if ( typeof err.message !== 'undefined' ) {
gutil.log(err.message)
}
var cleaned = _.omit(err, 'stream')
if ( _.keys(cleaned).length > 0 ) {
gutil.log(cleaned)
}
}
function js_tag(path) {
return '<script src="'+ path +'"></script>'
}
function css_tag(path) {
return '<link rel="stylesheet" href="'+ path +'">'
}
function index_stream(includes) {
includes = [js_tag('config.js')].concat(includes)
gutil.log(includes)
return gulp.src('src/main/index.tmpl')
.pipe(rename('index.html'))
.pipe(template({ includes: includes.join("\n") }))
.on('error', gutil.log)
}
function string_src(path, string) {
var src = stream.Readable({ objectMode: true })
src._read = function () {
this.push(new gutil.File({ cwd: "", base: "", path: path, contents: new Buffer(string) }))
this.push(null)
}
return src
}