-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
174 lines (146 loc) · 3.78 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
var gulp = require('gulp'),
pkg = require('./package.json'),
$ = require('gulp-load-plugins')(),
colors = require('ansi-colors'),
log = require('fancy-log'),
del = require('del');
// production mode (see build task)
var isProduction = false;
// styles sourcemaps
var useSourceMaps = false;
var headerTemplate = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @author <%= pkg.author.name %> <<%= pkg.author.email %>> (<%= pkg.author.url %>)',
' * @link <%= pkg.author.url %>',
' * @maintainers',
' * <%for(var i in pkg.maintainers){%> <%=pkg.maintainers[i].name%> <<%-pkg.maintainers[i].email%>> (<%-pkg.maintainers[i].url%>)<%}%>',
' * @license <%= pkg.license %>',
' */',
''
].join('\n');
var uglifyOptions = {
output: {
comments: 'some'
}
}
// MAIN PATHS
var paths = {
app: 'app/',
scripts: 'src/'
}
var app = {
name: pkg.name || 'app',
dist: 'dist'
}
// SOURCES CONFIG
var source = {
scripts: [paths.scripts + 'core.module.js',
paths.scripts + '**/*.module.js',
paths.scripts + '**/*.constant.js',
paths.scripts + '**/*.provider.js',
paths.scripts + '**/*.factory.js',
paths.scripts + '**/*.service.js',
paths.scripts + '**/*.filter.js',
paths.scripts + '**/*.js'
]
};
//---------------
// TASKS
//---------------
// JS APP
gulp.task('scripts:core', function() {
info('Building scripts..');
// Minify and copy all JavaScript (except vendor scripts)
return gulp.src(source.scripts)
.pipe($.jsvalidate())
.on('error', handleError)
.pipe($.if(useSourceMaps, $.sourcemaps.init()))
.pipe($.concat(app.name + '.js'))
.pipe($.header(headerTemplate, {
pkg: pkg
}))
.pipe(gulp.dest(app.dist))
.pipe($.rename(app.name + '.min.js'))
.pipe($.ngAnnotate())
.on('error', handleError)
.pipe($.uglify(uglifyOptions).on('error', handleError))
.pipe($.if(useSourceMaps, $.sourcemaps.write('./')))
.on('error', handleError)
.pipe(gulp.dest(app.dist));
});
//---------------
// WATCH
//---------------
// Rerun the task when a file changes
gulp.task('watch', function() {
info('Watching source files..');
gulp.watch(source.scripts, gulp.series('scripts:core'));
startEditingMessage();
});
// lint javascript
gulp.task('lint', function() {
return gulp
.src(source.scripts)
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish', {
verbose: true
}))
.pipe($.jshint.reporter('fail'));
});
// Remove all files from the build paths
gulp.task('clean', function(done) {
var delconfig = [].concat(
app.dist
);
info('Cleaning: ' + colors.blue(delconfig.join(', ')));
// force: clean files outside current directory
return del(delconfig, {
force: true
}, done);
});
//---------------
// MAIN TASKS
//---------------
gulp.task('prod', function(done) {
info('Starting production build...');
isProduction = true;
done();
});
// build for production (minify)
gulp.task('build', gulp.series(
'lint',
'prod',
'scripts:core', function (done) {
done();
}));
// default (no minify)
gulp.task('default', gulp.series('build', function (done) {
done();
}));
gulp.task('usesources', function(done) {
useSourceMaps = true;
done();
});
// build with sourcemaps (no minify)
gulp.task('sourcemaps', gulp.series('usesources', 'default', function (done) {
done();
}));
/////////////////////
function startEditingMessage() {
info('************');
info('* All Done * You can start editing your code, BrowserSync will update your browser after any change..');
info('************');
}
// Error handler
function handleError(err) {
error(err.toString());
this.emit('end');
}
function error(msg) {
log.error(colors.red.bold(msg));
}
// log to console using
function info(msg) {
log.info(colors.blue.bold(msg));
}