-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
95 lines (80 loc) · 2.63 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
var gulp = require('gulp');
var requirejs = require('requirejs');
var vinylPaths = require('vinyl-paths');
var del = require('del');
var runSequence = require('run-sequence');
var merge = require('merge-stream');
var es = require('event-stream');
var hash = require('gulp-hash');
var extend = require('gulp-extend');
var production = process.env.NODE_ENV === 'production';
var configJsonPath = './config/build-config.json';
gulp.task('build', function(callback) {
runSequence(
'clean:build',
['minify-js-main', 'copy:assets'],
'hash-files',
callback
);
});
/** Delete build folder and config file if exist **/
gulp.task('clean:build', function () {
var buildStream = gulp.src('build')
.pipe(vinylPaths(del));
var configStream = gulp.src('config/build-config.json')
.pipe(vinylPaths(del));
return merge(buildStream, configStream);
});
/** RequireJS Optimizer options **/
var mainClientOptions = {
baseUrl: './theme/scripts',
out: './build/scripts/main.min.js',
name: '../../node_modules/almond/almond',
mainConfigFile: './theme/scripts/main.js',
include: 'main',
insertRequire: ['main'],
removeCombined: true,
optimize: 'uglify2',
generateSourceMaps: false,
preserveLicenseComments: false
};
gulp.task('minify-js-main', function(callback)
{
requirejs.optimize(mainClientOptions, function (buildResponse) {
callback();
}, function(err) {
callback(err);
console.error('[Error on require optimization]: ', err);
});
});
/** Copy the files to prod folder **/
gulp.task('copy:assets', function() {
return gulp.src('theme/**/*.*')
.pipe(gulp.dest('build/'));
});
/** Hash the config.js and the app.css files **/
var hashOptions = {
template: '<%= name %>-<%= hash %><%= ext %>'
};
gulp.task('hash-files', function(callback) {
runSequence('hash-js-main', callback);
});
gulp.task('hash-js-main', function() {
return addToManifest(
gulp.src('./build/scripts/main.min.js')
.pipe(hash(hashOptions))
.pipe(gulp.dest('build/scripts'))
);
});
/** ======================================== Functions ========================================= **/
/** ============================================================================================ **/
// Adds the files in `srcStream` to the manifest file, extending the manifest's current contents.
function addToManifest(srcStream) {
return es.concat(
gulp.src(configJsonPath),
srcStream
.pipe(hash.manifest(configJsonPath))
)
.pipe(extend(configJsonPath, false, 4))
.pipe(gulp.dest('.'));
}