-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
152 lines (133 loc) · 3.7 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
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var useref = require('gulp-useref');
var gulpIf = require('gulp-if');
var cssnano = require('gulp-cssnano');
var imagemin = require('gulp-imagemin');
var cache = require('gulp-cache');
var del = require('del');
var runSequence = require('run-sequence');
var uglify = require('gulp-uglify');
var jshint = require('gulp-jshint');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var ngAnnotate = require('gulp-ng-annotate');
var browserSync = require('browser-sync').create();
var templateCache = require('gulp-angular-templatecache');
var src = 'assets/',
app = 'app/',
paths = {
scss: 'sass/**/*.scss',
images: 'resources/**/*.+(png|jpg|jpeg|gif|svg)',
fonts: 'fonts/**/*',
js: 'js/**/*.js',
appJs: app + '**/*.js',
appHtml: app + '**/*.html'
};
gulp.task('sass', function() {
return gulp.src(src + paths.scss)
.pipe(sass())
.pipe(gulp.dest(src + 'css'))
.pipe(browserSync.reload({
stream: true
}));
});
//watch for scss files changes
gulp.task('watch', function() {
gulp.watch(src + paths.scss, ['sass', 'useref']);
gulp.watch(paths.appJs, ['script', 'lint']);
gulp.watch(paths.appHtml, ['template']);
});
//concatenates and minifies css files
gulp.task('useref', function() {
return gulp.src('*.html')
.pipe(useref())
//Minifies only if it's a JS file
.pipe(gulpIf('*.js', uglify()))
//Minifies only if it's a CSS file
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'));
});
//Minifies images
gulp.task('images', function() {
return gulp.src(src + paths.images)
//Optimizing images is a slow process and it shouldn't be repeated each time
.pipe(cache(imagemin({
interlaced: true
})))
.pipe(gulp.dest('dist/resources'));
});
//Copy fonts to dist
gulp.task('fonts', function() {
return gulp.src(src + paths.fonts)
.pipe(gulp.dest('dist/fonts'));
});
//clean dist
gulp.task('clean:dist', function() {
return del.sync('dist');
});
//run sequence
gulp.task('build', ['framework', 'script', 'template'], function(callback) {
runSequence('clean:dist',
['sass', 'useref', 'images', 'fonts'],
callback
);
});
//default sequence tasks (only type "gulp")
gulp.task('default', function(callback) {
runSequence(['sass', 'lint', 'browserSync', 'watch'], callback);
});
gulp.task('lint', function() {
return gulp.src(paths.appJs)
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'));
});
gulp.task('framework', function() {
return browserify({
debug: false
})
.require('angular')
.require('angular-animate')
.require('angular-ui-bootstrap')
.require('angular-ui-router')
.bundle()
.pipe(source('framework.js'))
.pipe(gulp.dest(app + '.tmp'));
});
gulp.task('script', function() {
return browserify({
entries: app + 'app.js',
debug: true
})
.external('angular')
.external('angular-animate')
.external('angular-ui-bootstrap')
.external('angular-ui-router')
.bundle()
.pipe(source('app.js'))
.pipe(ngAnnotate())
.pipe(gulp.dest(app + '.tmp'));
});
//clear catche
gulp.task('cache:clear', function (callback) {
return cache.clearAll(callback);
});
//spin up server using Browser Sync
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: './'
},
})
});
gulp.task('template', function() {
return gulp.src(paths.appHtml)
.pipe(templateCache({
module: 'myApp',
root: 'app/',
moduleSystem: 'IIFE'
}))
.pipe(gulp.dest(app + '.tmp'));
});