forked from ripple/ripple-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
280 lines (242 loc) · 7.64 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict';
var gulp = require('gulp'),
merge = require('merge-stream'),
modRewrite = require('connect-modrewrite'),
BannerPlugin = require('gulp-webpack/node_modules/webpack/lib/BannerPlugin'),
UglifyJsPlugin = require('gulp-webpack/node_modules/webpack/lib/optimize/UglifyJsPlugin'),
jade = require('jade'),
jadeL10n = require('jade-l10n'),
meta = require('./package.json'),
languages = require('./l10n/languages.json').active;
var $ = require('gulp-load-plugins')({
pattern: ['gulp-*', 'del', 'browser-sync']
});
var buildDirPath = 'build';
require('events').EventEmitter.prototype._maxListeners = 100;
// Clean the build folder
gulp.task('clean:dev', function () {
$.del.sync([buildDirPath + '/dev/*']);
});
gulp.task('clean:dist', function () {
$.del.sync([buildDirPath + '/dist/*']);
});
gulp.task('bower', function() {
return $.bower();
});
// Webpack
gulp.task('webpack:dev', function() {
// TODO jshint
// TODO move to js/entry.js
return gulp.src('src/js/entry/web.js')
.pipe($.webpack({
module: {
loaders: [
{ test: /\.jade$/, loader: "jade-loader" },
{ test: /\.json$/, loader: "json-loader" }
]
},
output: {
filename: "app.js"
},
cache: true,
debug: true,
devtool: 'eval'
}))
.pipe(gulp.dest(buildDirPath + '/dev/js/'))
.pipe($.browserSync.reload({stream:true}));
});
gulp.task('webpack:dist', function() {
return gulp.src('src/js/entry/web.js')
.pipe($.webpack({
module: {
loaders: [
{ test: /\.jade$/, loader: "jade-loader" },
{ test: /\.json$/, loader: "json-loader" }
]
},
output: {
filename: "app.js"
},
plugins: [
new BannerPlugin('Ripple Client v' + meta.version + '\nCopyright (c) ' + new Date().getFullYear() + ' ' + meta.author.name + '\nLicensed under the ' + meta.license + ' license.'),
new UglifyJsPlugin({
compress: {
warnings: false
}
})
],
debug: false
}))
.pipe(gulp.dest(buildDirPath + '/dist/js/'));
});
// TODO SASS
// Less
gulp.task('less', function () {
return gulp.src('src/less/ripple/web.less')
.pipe($.less({
paths: ['src/less']
}))
.pipe(gulp.dest(buildDirPath + '/dev/css'))
.pipe($.browserSync.reload({stream:true}));
});
// Extracts l10n strings from template files
gulp.task('l10nExtract', function () {
return gulp.src('src/templates/**/*.jade')
.pipe($.jadeL10nExtractor({
filename: 'messages.pot'
}))
.pipe(gulp.dest('./l10n/templates'))
});
// Static server
gulp.task('serve:dev', function() {
$.browserSync({
server: {
baseDir: [".", buildDirPath + "/dev", buildDirPath, buildDirPath + "/dist", "./res", "./deps/js"],
middleware: [
modRewrite([
'!\\.html|\\.js|\\.css|\\.png|\\.jpg|\\.gif|\\.svg|\\.txt|\\.eot|\\.woff|\\.woff2|\\.ttf$ /index.html [L]'
])
]
}
});
});
gulp.task('serve:dist', function() {
$.browserSync({
server: {
baseDir: [buildDirPath + "/dist"],
middleware: [
modRewrite([
'!\\.html|\\.js|\\.css|\\.png|\\.jpg|\\.gif|\\.svg|\\.txt|\\.eot|\\.woff|\\.woff2|\\.ttf$ /index.html [L]'
])
]
},
middleware: require("connect-logger")()
});
});
// Static files
gulp.task('static', function() {
// Ripple.txt and mixpanel script
var rpl = gulp.src(['ripple.txt', 'deps/js/mixpanel.js'])
.pipe(gulp.dest(buildDirPath + '/dist/'));
// Fonts and icons
var fontsIcons = gulp.src(['res/**/*'])
.pipe(gulp.dest(buildDirPath + '/dist/'));
// Images
var images = gulp.src('img/**/*')
.pipe(gulp.dest(buildDirPath + '/dist/img/'));
return merge(rpl, fontsIcons, images);
});
// Version branch
gulp.task('gitVersion', function (cb) {
require('child_process').exec('git rev-parse --abbrev-ref HEAD', function(err, stdout) {
meta.gitVersionBranch = stdout.replace(/\n$/, '');
require('child_process').exec('git describe --tags --always', function(err, stdout) {
meta.gitVersion = stdout.replace(/\n$/, '');
cb(err)
})
})
});
// Preprocess
gulp.task('preprocess:dev', ['gitVersion'], function() {
return gulp.src(buildDirPath + '/dev/templates/en/index.html')
.pipe($.preprocess({
context: {
MODE: 'dev',
VERSION: meta.gitVersion,
VERSIONBRANCH: meta.gitVersionBranch,
VERSIONFULL: meta.gitVersion + '-' + meta.gitVersionBranch
}
}))
.pipe(gulp.dest(buildDirPath + '/dev/'))
.pipe($.browserSync.reload({stream:true}))
});
gulp.task('preprocess:dist', ['gitVersion'], function() {
return gulp.src(buildDirPath + '/dist/templates/en/index.html')
.pipe($.preprocess({
context: {
MODE: 'dist',
VERSION: meta.gitVersion,
VERSIONBRANCH: meta.gitVersionBranch,
VERSIONFULL: meta.gitVersion
}
}))
.pipe(gulp.dest(buildDirPath + '/dist/'))
});
// Languages
gulp.task('templates:dev', function () {
return gulp.src('src/templates/**/*.jade')
.pipe($.jade({
jade: jade,
pretty: true
}))
.pipe(gulp.dest('build/dev/templates/en'));
});
var languageTasks = [];
languages.forEach(function(language){
gulp.task('templates:' + language.code, function(){
return gulp.src('src/templates/**/*.jade')
.pipe($.jade({
jade: jadeL10n,
languageFile: 'l10n/' + language.code + '/messages.po',
pretty: true
}))
.pipe(gulp.dest(buildDirPath + '/dist/templates/' + language.code));
});
languageTasks.push('templates:' + language.code);
});
gulp.task('templates:dist', $.sync(gulp).sync(languageTasks));
// Default Task (Dev environment)
gulp.task('default', ['dev'], function() {
gulp.start('serve:dev');
// Webpack
gulp.watch(['src/js/**/*.js'], ['webpack:dev']);
// Templates
$.watch('src/templates/**/*.jade')
.pipe($.jadeFindAffected())
.pipe($.jade({jade: jade, pretty: true}))
.pipe(gulp.dest('build/dev/templates/en'))
.pipe($.browserSync.reload({stream:true}));
// Htmls
gulp.watch(buildDirPath + '/dev/templates/en/*.html', ['preprocess:dev']);
// TODO Config
gulp.watch('src/less/**/*', ['less']);
});
// Development
gulp.task('dev', ['clean:dev', 'bower', 'webpack:dev', 'less', 'templates:dev'], function () {
gulp.start('preprocess:dev');
});
gulp.task('deps', ['preprocess:dist'], function () {
var assets = $.useref.assets();
return gulp.src([buildDirPath + '/dist/index.html'])
// Concatenates asset files from the build blocks inside the HTML
.pipe(assets)
// Appends hash to extracted files app.css → app-098f6bcd.css
.pipe($.rev())
// Adds AngularJS dependency injection annotations
// We don't need this, cuz the app doesn't go thru this anymore
//.pipe($.if('*.js', $.ngAnnotate()))
// Uglifies js files
.pipe($.if('*.js', $.uglify()))
// Minifies css files
.pipe($.if('*.css', $.csso()))
// Brings back the previously filtered HTML files
.pipe(assets.restore())
// Parses build blocks in html to replace references to non-optimized scripts or stylesheets
.pipe($.useref())
// Rewrites occurences of filenames which have been renamed by rev
.pipe($.revReplace())
// Minifies html
.pipe($.if('*.html', $.minifyHtml({
empty: true,
spare: true,
quotes: true
})))
// Creates the actual files
.pipe(gulp.dest(buildDirPath + '/dist/'))
// Print the file sizes
.pipe($.size({ title: buildDirPath + '/dist/', showFiles: true }));
});
// Distribution
gulp.task('dist', ['clean:dist', 'bower', 'less', 'l10nExtract', 'webpack:dist', 'templates:dist', 'static'], function () {
gulp.start('deps');
});