Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 1fa9776

Browse files
committed
Task enhancements
lint is already referenced in build task. missing env:prod add client/server test tasks Add template cache and autoprefixer. Update .jshintrc
1 parent 01bd98b commit 1fa9776

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

config/assets/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ module.exports = {
3434
'modules/*/client/*.js',
3535
'modules/*/client/**/*.js'
3636
],
37-
views: ['modules/*/client/views/**/*.html']
37+
views: ['modules/*/client/views/**/*.html'],
38+
templates: ['build/templates.js']
3839
},
3940
server: {
4041
gruntConfig: 'gruntfile.js',

gulpfile.js

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ var _ = require('lodash'),
99
gulp = require('gulp'),
1010
gulpLoadPlugins = require('gulp-load-plugins'),
1111
runSequence = require('run-sequence'),
12-
plugins = gulpLoadPlugins(),
13-
path = require('path');
12+
plugins = gulpLoadPlugins({
13+
rename: {
14+
'gulp-angular-templatecache': 'templateCache'
15+
}
16+
}),
17+
path = require('path'),
18+
endOfLine = require('os').EOL;
1419

1520
// Set NODE_ENV to 'test'
1621
gulp.task('env:test', function () {
@@ -38,19 +43,25 @@ gulp.task('nodemon', function () {
3843
});
3944

4045
// Watch Files For Changes
41-
gulp.task('watch', function() {
46+
gulp.task('watch', function () {
4247
// Start livereload
4348
plugins.livereload.listen();
4449

4550
// Add watch rules
46-
gulp.watch(defaultAssets.server.gulpConfig, ['jshint']);
4751
gulp.watch(defaultAssets.server.views).on('change', plugins.livereload.changed);
4852
gulp.watch(defaultAssets.server.allJS, ['jshint']).on('change', plugins.livereload.changed);
49-
gulp.watch(defaultAssets.client.views).on('change', plugins.livereload.changed);
5053
gulp.watch(defaultAssets.client.js, ['jshint']).on('change', plugins.livereload.changed);
5154
gulp.watch(defaultAssets.client.css, ['csslint']).on('change', plugins.livereload.changed);
5255
gulp.watch(defaultAssets.client.sass, ['sass', 'csslint']).on('change', plugins.livereload.changed);
5356
gulp.watch(defaultAssets.client.less, ['less', 'csslint']).on('change', plugins.livereload.changed);
57+
58+
if (process.env.NODE_ENV === 'production') {
59+
gulp.watch(defaultAssets.server.gulpConfig, ['templatecache', 'jshint']);
60+
gulp.watch(defaultAssets.client.views, ['templatecache', 'jshint']).on('change', plugins.livereload.changed);
61+
} else {
62+
gulp.watch(defaultAssets.server.gulpConfig, ['jshint']);
63+
gulp.watch(defaultAssets.client.views).on('change', plugins.livereload.changed);
64+
}
5465
});
5566

5667
// CSS linting task
@@ -67,16 +78,29 @@ gulp.task('csslint', function (done) {
6778

6879
// JS linting task
6980
gulp.task('jshint', function () {
70-
return gulp.src(_.union(defaultAssets.server.gulpConfig, defaultAssets.server.allJS, defaultAssets.client.js, testAssets.tests.server, testAssets.tests.client, testAssets.tests.e2e))
81+
var assets = _.union(
82+
defaultAssets.server.gulpConfig,
83+
defaultAssets.server.allJS,
84+
defaultAssets.client.js,
85+
testAssets.tests.server,
86+
testAssets.tests.client,
87+
testAssets.tests.e2e
88+
);
89+
90+
return gulp.src(assets)
7191
.pipe(plugins.jshint())
7292
.pipe(plugins.jshint.reporter('default'))
7393
.pipe(plugins.jshint.reporter('fail'));
7494
});
7595

76-
7796
// JS minifying task
7897
gulp.task('uglify', function () {
79-
return gulp.src(defaultAssets.client.js)
98+
var assets = _.union(
99+
defaultAssets.client.js,
100+
defaultAssets.client.templates
101+
);
102+
103+
return gulp.src(assets)
80104
.pipe(plugins.ngAnnotate())
81105
.pipe(plugins.uglify({
82106
mangle: false
@@ -97,6 +121,7 @@ gulp.task('cssmin', function () {
97121
gulp.task('sass', function () {
98122
return gulp.src(defaultAssets.client.sass)
99123
.pipe(plugins.sass())
124+
.pipe(plugins.autoprefixer())
100125
.pipe(plugins.rename(function (file) {
101126
file.dirname = file.dirname.replace(path.sep + 'scss', path.sep + 'css');
102127
}))
@@ -107,20 +132,39 @@ gulp.task('sass', function () {
107132
gulp.task('less', function () {
108133
return gulp.src(defaultAssets.client.less)
109134
.pipe(plugins.less())
135+
.pipe(plugins.autoprefixer())
110136
.pipe(plugins.rename(function (file) {
111137
file.dirname = file.dirname.replace(path.sep + 'less', path.sep + 'css');
112138
}))
113139
.pipe(gulp.dest('./modules/'));
114140
});
115141

142+
// Angular template cache task
143+
gulp.task('templatecache', function () {
144+
var re = new RegExp('\\' + path.sep + 'client\\' + path.sep, 'g');
145+
146+
return gulp.src(defaultAssets.client.views)
147+
.pipe(plugins.templateCache('templates.js', {
148+
root: 'modules/',
149+
module: 'core',
150+
templateHeader: '(function () {' + endOfLine + ' \'use strict\';' + endOfLine + endOfLine + ' angular' + endOfLine + ' .module(\'<%= module %>\'<%= standalone %>)' + endOfLine + ' .run(templates);' + endOfLine + endOfLine + ' templates.$inject = [\'$templateCache\'];' + endOfLine + endOfLine + ' function templates($templateCache) {' + endOfLine,
151+
templateBody: ' $templateCache.put(\'<%= url %>\', \'<%= contents %>\');',
152+
templateFooter: ' }' + endOfLine + '})();' + endOfLine,
153+
transformUrl: function (url) {
154+
return url.replace(re, path.sep);
155+
}
156+
}))
157+
.pipe(gulp.dest('build'));
158+
});
159+
116160
// Mocha tests task
117161
gulp.task('mocha', function (done) {
118162
// Open mongoose connections
119163
var mongoose = require('./config/lib/mongoose.js');
120164
var error;
121165

122166
// Connect mongoose
123-
mongoose.connect(function() {
167+
mongoose.connect(function () {
124168
// Run the tests
125169
gulp.src(testAssets.tests.server)
126170
.pipe(plugins.mocha({
@@ -130,9 +174,9 @@ gulp.task('mocha', function (done) {
130174
// If an error occurs, save it
131175
error = err;
132176
})
133-
.on('end', function() {
177+
.on('end', function () {
134178
// When the tests are done, disconnect mongoose and pass the error state back to gulp
135-
mongoose.disconnect(function() {
179+
mongoose.disconnect(function () {
136180
done(error);
137181
});
138182
});
@@ -165,31 +209,39 @@ gulp.task('protractor', function () {
165209
});
166210

167211
// Lint CSS and JavaScript files.
168-
gulp.task('lint', function(done) {
212+
gulp.task('lint', function (done) {
169213
runSequence('less', 'sass', ['csslint', 'jshint'], done);
170214
});
171215

172216
// Lint project files and minify them into two production files.
173-
gulp.task('build', function(done) {
174-
runSequence('env:dev' ,'lint', ['uglify', 'cssmin'], done);
217+
gulp.task('build', function (done) {
218+
runSequence('env:dev', 'lint', ['uglify', 'cssmin'], done);
175219
});
176220

177221
// Run the project tests
178-
gulp.task('test', function(done) {
222+
gulp.task('test', function (done) {
179223
runSequence('env:test', ['karma', 'mocha'], done);
180224
});
181225

226+
gulp.task('test:server', function (done) {
227+
runSequence('env:test', ['mocha'], done);
228+
});
229+
230+
gulp.task('test:client', function (done) {
231+
runSequence('env:test', ['karma'], done);
232+
});
233+
182234
// Run the project in development mode
183-
gulp.task('default', function(done) {
235+
gulp.task('default', function (done) {
184236
runSequence('env:dev', 'lint', ['nodemon', 'watch'], done);
185237
});
186238

187239
// Run the project in debug mode
188-
gulp.task('debug', function(done) {
240+
gulp.task('debug', function (done) {
189241
runSequence('env:dev', 'lint', ['nodemon', 'watch'], done);
190242
});
191243

192244
// Run the project in production mode
193-
gulp.task('prod', function(done) {
194-
runSequence('build', 'env:prod', 'lint', ['nodemon', 'watch'], done);
245+
gulp.task('prod', function (done) {
246+
runSequence('templatecache', 'build', 'env:prod', 'lint', ['nodemon', 'watch'], done);
195247
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"grunt-nodemon": "~0.4.0",
8080
"grunt-protractor-runner": "^2.0.0",
8181
"gulp": "^3.9.0",
82+
"gulp-angular-templatecache": "^1.7.0",
83+
"gulp-autoprefixer": "^2.3.1",
8284
"gulp-concat": "^2.6.0",
8385
"gulp-csslint": "~0.1.5",
8486
"gulp-cssmin": "~0.1.7",
@@ -94,6 +96,7 @@
9496
"gulp-rename": "^1.2.2",
9597
"gulp-sass": "^2.0.3",
9698
"gulp-uglify": "^1.2.0",
99+
"gulp-util": "^3.0.6",
97100
"karma": "~0.12.37",
98101
"karma-chrome-launcher": "~0.2.0",
99102
"karma-coverage": "~0.4.2",

0 commit comments

Comments
 (0)