-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
188 lines (169 loc) · 5.21 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
var gulp = require('gulp');
var gulpif = require('gulp-if');
var jshint = require('gulp-jshint');
var notify = require("gulp-notify");
var stylish = require('jshint-stylish');
var sass = require('gulp-sass');
var source = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
var del = require('del');
var yargs = require('yargs');
var inline_base64 = require('gulp-inline-base64');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('autoprefixer-core');
var postcss = require('gulp-postcss');
//var merge = require('merge-stream');
var sources = {
browserify: {
watch: [
'static-source/public/js/**/*.js',
'static-source/admin/js/**/*.js'
],
files: [
'./static-source/public/js/main.js'
],
adminFiles: [
'./static-source/admin/js/main.js'
]
},
scss: {
watch: [
'static-source/public/scss/**/*',
'static-source/admin/scss/**/*',
],
files: [
'static-source/public/scss/**/*.scss',
],
adminFiles: [
'static-source/admin/scss/**/*.scss',
]
},
};
var destinations = {
scripts: 'static/js/',
styles: 'static/css/',
adminScripts: 'static/admin/js/',
adminStyles: 'static/admin/css/'
};
var lint = [
'gulpfile.js',
'static-source/public/js/**/*.js',
'static-source/admin/js/**/*.js',
];
var production = (yargs.argv.environment === 'production');
var verbose = yargs.argv.verbose;
var handleError = notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
});
var postcssProcessors = [
autoprefixer({
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1', 'IE 9'],
})
];
// Handler for browserify
var browserifyHandleError = function(err){
handleError(err);
this.end();
};
// Cleanup tasks
gulp.task('clean', ['clean_scripts', 'clean_styles', 'clean_admin_styles', 'clean_admin_scripts'], function(cb) {
cb();
});
gulp.task('clean_scripts', function(cb) {
del(['static/js/**/*'], cb);
});
gulp.task('clean_admin_scripts', function(cb) {
del(['static/admin/js/**/*'], cb);
});
gulp.task('clean_styles', function(cb) {
del(['static/css/**/*'], cb);
});
gulp.task('clean_admin_styles', function(cb) {
del(['static/admin/css/**/*'], cb);
});
// Copys all the user created scripts
gulp.task('scripts', ['clean_scripts'], function() {
return browserify({
entries: sources.browserify.files,
insertGlobals : true,
debug : !production,
})
.bundle()
.on('error', browserifyHandleError)
.pipe(source('main.js'))
.on('error', handleError)
.pipe(gulpif(production, streamify(uglify())))
.on('error', handleError)
.pipe(gulp.dest(destinations.scripts));
});
gulp.task('admin-scripts', ['clean_admin_scripts'], function() {
return browserify({
entries: sources.browserify.adminFiles,
insertGlobals : true,
debug : !production,
})
.bundle()
.on('error', browserifyHandleError)
.pipe(source('main.js'))
.on('error', handleError)
.pipe(gulpif(production, streamify(uglify())))
.on('error', handleError)
.pipe(gulp.dest(destinations.adminScripts));
});
// Generate css files from scss
gulp.task('styles', ['clean_styles'], function(){
return gulp.src(sources.scss.files)
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: production ? 'compressed' : 'nested',
}))
.on('error', handleError)
.pipe(inline_base64({
baseDir: destinations.styles,
maxSize: 14 * 1024,
debug: verbose,
}))
.on('error', handleError)
.pipe(postcss(postcssProcessors))
.on('error', handleError)
.pipe(sourcemaps.write('./maps'))
.on('error', handleError)
.pipe(gulp.dest(destinations.styles));
});
gulp.task('admin-styles', ['clean_admin_styles'], function(){
return gulp.src(sources.scss.adminFiles)
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: production ? 'compressed' : 'nested',
}))
.on('error', handleError)
.pipe(inline_base64({
baseDir: destinations.adminStyles,
maxSize: 14 * 1024,
debug: verbose,
}))
.on('error', handleError)
.pipe(postcss(postcssProcessors))
.on('error', handleError)
.pipe(sourcemaps.write('./maps'))
.on('error', handleError)
.pipe(gulp.dest(destinations.adminStyles));
});
gulp.task('lint', function() {
return gulp.src(lint)
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter(stylish));
});
// Watch the folders
gulp.task('watch', function() {
gulp.watch(sources.browserify.watch, ['lint', 'scripts', 'admin-scripts']);
gulp.watch(sources.scss.watch, ['styles', 'admin-styles']);
});
// build task
gulp.task('build', ['lint', 'scripts', 'styles', 'admin-scripts', 'admin-styles']);
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['build', 'watch']);