-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
138 lines (107 loc) · 3.83 KB
/
gulpfile.js
File metadata and controls
138 lines (107 loc) · 3.83 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
var bower = require('gulp-bower');
browserify = require('browserify'),
glob = require('glob'),
gulp = require('gulp'),
gutil = require('gulp-util');
minify = require('html-minifier').minify,
nodemon = require('gulp-nodemon'),
replace = require('gulp-replace'),
source = require('vinyl-source-stream'),
transform = require('vinyl-transform'),
fs = require('fs'),
path = require('path'),
watchify = require('watchify');
gulp.task('init', ['bower', 'browserify']);
gulp.task('bower', function() {
return bower().pipe(gulp.dest('src/js/client/bower_components'))
});
/**
* Task used to create the client Javascript file using Browserify. The produced file will be stored in
* 'src/client/bundle.js'.
*/
gulp.task(
'browserify',
['inline-templates'],
function() {
return browserify(
{
entries: ['tmp/bootstrap.js'],
debug: true
}
).bundle()
// Log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(gulp.dest('src/js/client'));
}
);
/**
* Task used to inspect all "require('*.tpl')" calls in the source code and replace those calls by a string with the
* template content.
*/
gulp.task(
'inline-templates',
function() {
var stream = gulp.src(['src/js/common/views/**/*.js'])
.pipe(replace(/require\(\'.*\.tpl\'\)/g, function(toBeReplaced, file) {
// Extracts the template relative path
var tplRelativePath = toBeReplaced.substring(9);
tplRelativePath = tplRelativePath.substring(0, tplRelativePath.length - 2);
// Computes the template absolute path
var tplAbsolutePath = path.join(path.dirname(file.path), tplRelativePath);
// Gets the template content
var tpl = fs.readFileSync(tplAbsolutePath, 'utf8');
// Remove line breaks
tpl = tpl.replace(/(\r\n|\n|\r)/gm,"")
// Minify the template content
tpl = minify(tpl, {
collapseWhitespace : true,
removeComments: true
});
// Escapte quotes and double quotes
tpl = tpl.replace(/"/g, '\\"').replace(/'/g, "\\'");
return '\'' + tpl + '\'';
}))
.pipe(gulp.dest('./tmp/views'));
return stream;
}
);
gulp.task(
'serve',
function () {
nodemon(
{
cwd: 'src/js/server',
script: 'server.js',
ext: 'js html',
env: {
'NODE_ENV': 'development'
}
}
);
}
);
gulp.task(
'watch',
function() {
var templateWatcher = gulp.watch('src/js/common/views/**/*', ['inline-templates']);
var browserified = browserify(
{
entries: ['tmp/bootstrap.js'],
debug: true
}
);
var watchifiedBrowserified = watchify(browserified);
var watchifiedBrowserifiedBundle = createBundle();
watchifiedBrowserified.on('update', createBundle);
watchifiedBrowserified.on('log', gutil.log);
function createBundle() {
return watchifiedBrowserified
.bundle()
// Log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(gulp.dest('src/js/client'));
}
}
);