-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
222 lines (195 loc) · 8.44 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var basePaths = { // Paths for source and bundled parts of app
src: 'src/', dest: 'assets/', npm: 'node_modules/', bower: 'bower_components/'
},
gulp = require( 'gulp' ), // Require plugins
es = require( 'event-stream' ),
gutil = require( 'gulp-util' ),
bourbon = require( 'node-bourbon' ),
path = require( 'relative-path' ),
runSequence = require( 'run-sequence' ),
del = require( 'del' ),
plugins = require( 'gulp-load-plugins' )({ // Plugins - load gulp-* plugins without direct calls
pattern: [ 'gulp-*', 'gulp.*' ], replaceString: /\bgulp[\-.]/
}),
// Env - call gulp --prod to go into production mode
sassStyle = 'expanded', // SASS syntax
sourceMap = true, // Wheter to build source maps
isProduction = false, // Mode flag
changeEvent = function( evt ) { // Log
gutil.log( 'File', gutil.colors.cyan( evt.path.replace( new RegExp( '/.*(?=/' + basePaths.src +
')/' ), '' ) ), 'was', gutil.colors.magenta( evt.type ) );
};
if ( true === gutil.env.prod ) {
isProduction = true;
sassStyle = 'compressed';
sourceMap = false;
}
//js
gulp.task( 'build-js', function() {
var vendorFiles = [
basePaths.npm + 'imagesloaded/imagesloaded.pkgd.js'
], appFiles = [ basePaths.src + 'js/*', basePaths.src + 'js/front/*' ]; //our own JS files
return gulp.src( vendorFiles.concat( appFiles ) ) //join them
.pipe( plugins.filter( '*.js' ) )//select only .js ones
.pipe( plugins.concat( 'bundle.js' ) )//combine them into bundle.js
.pipe( isProduction ? plugins.uglify() : gutil.noop() ) //minification
.pipe( plugins.size() ) //print size for log
.on( 'error', console.log ) //log
.pipe( gulp.dest( basePaths.dest + 'js' ) ); //write results into file
});
//js
gulp.task( 'build-admin-js', function() {
var vendorFiles = [
// basePaths.npm + 'imagesloaded/imagesloaded.pkgd.js'
], appFiles = [ basePaths.src + 'js/admin/*' ]; //our own JS files
return gulp.src( vendorFiles.concat( appFiles ) ) //join them
.pipe( plugins.filter( '*.js' ) )//select only .js ones
.pipe( plugins.concat( 'admin.js' ) )//combine them into bundle.js
.pipe( isProduction ? plugins.uglify() : gutil.noop() ) //minification
.pipe( plugins.size() ) //print size for log
.on( 'error', console.log ) //log
.pipe( gulp.dest( basePaths.dest + 'js' ) ); //write results into file
});
// Sass
gulp.task( 'build-css', function() {
// Paths for mdl and bourbon
var paths = require( 'node-bourbon' ).includePaths;
//mdl = path('./node_modules/material-design-lite/src');
//paths.push(mdl);
paths.push( basePaths.npm + 'modularscale-sass/stylesheets' );
var vendorFiles = gulp.src( [] ), // Components
appFiles = gulp.src( basePaths.src + 'sass/front-main.scss' ) // Main file with @import-s
.pipe( ! isProduction ? plugins.sourcemaps.init() : gutil.noop() ) // Process the
// original sources
// for sourcemap
.pipe( plugins.sass( {
outputStyle: sassStyle, // SASS syntax
includePaths: paths // Add bourbon
} ).on( 'error', plugins.sass.logError ) ) // SASS own error log
.pipe( plugins.autoprefixer( { // Aautoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) ).pipe( ! isProduction ? plugins.sourcemaps.write() : gutil.noop() ) // Add the map
// to modified
// source
.on( 'error', console.log ); // Log
return es.concat( appFiles, vendorFiles ) // Combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'bundle.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
gulp.task( 'build-editor-css', function() {
var paths = require( 'node-bourbon' ).includePaths, vendorFiles = gulp.src( [] ),
appFiles = gulp.src( basePaths.src + 'sass/editor-main.scss' ).
pipe( ! isProduction ? plugins.sourcemaps.init() : gutil.noop() ) // Process the
// original sources
// for sourcemap
.pipe( plugins.sass( {
outputStyle: sassStyle, //SASS syntas
includePaths: paths //add bourbon + mdl
} ).on( 'error', plugins.sass.logError ) )//sass own error log
.pipe( plugins.autoprefixer( { //autoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) ).pipe( ! isProduction ? plugins.sourcemaps.write() : gutil.noop() ) // Add the map
// to modified
// source
.on( 'error', console.log ); //log
return es.concat( appFiles, vendorFiles ) //combine vendor CSS files and our files after-SASS
.pipe( plugins.concat( 'editor.css' ) ) //combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) //minification on production
.pipe( plugins.size() ) //display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) //write file
.on( 'error', console.log ); //log
});
gulp.task( 'build-admin-css', function() {
var paths = require( 'node-bourbon' ).includePaths,
appFiles = gulp.src( basePaths.src + 'sass/admin-main.scss' ).
pipe( ! isProduction ? plugins.sourcemaps.init() : gutil.noop() ) // Process the
// original sources
// for sourcemap
.pipe( plugins.sass( {
outputStyle: sassStyle, //SASS syntas
includePaths: paths //add bourbon + mdl
} ).on( 'error', plugins.sass.logError ) )//sass own error log
.pipe( plugins.autoprefixer( { //autoprefixer
browsers: [ 'last 4 versions' ], cascade: false
} ) ).pipe( ! isProduction ? plugins.sourcemaps.write() : gutil.noop() ) // Add the map
// to modified
// source
.on( 'error', console.log ); // Log
return appFiles.pipe( plugins.concat( 'admin.css' ) ) // Combine into file
.pipe( isProduction ? plugins.cssmin() : gutil.noop() ) // Minification on production
.pipe( plugins.size() ) // Display size
.pipe( gulp.dest( basePaths.dest + 'css' ) ) // Write file
.on( 'error', console.log ); // Log
});
// Revision
gulp.task( 'revision-clean', function() {
// Clean folder https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
return del( [ basePaths.dest + 'rev/**/*' ] );
});
gulp.task( 'revision', function() {
return gulp.src( [ basePaths.dest + 'css/*.css', basePaths.dest + 'js/*.js' ] ).
pipe( plugins.rev() ).pipe( gulp.dest( basePaths.dest + 'rev' ) ).
pipe( plugins.rev.manifest() ).pipe( gulp.dest( basePaths.dest + 'rev' ) ) // Write
// manifest to
// build dir
.on( 'error', console.log ); // Log
});
// Builds
gulp.task( 'full-build', function( callback ) {
runSequence( 'build-css', 'build-editor-css', 'build-admin-css', 'build-js', 'svg-opt', 'revision-clean', 'revision', callback );
});
gulp.task( 'full-build-css', function( callback ) {
runSequence( 'build-css', 'build-editor-css', 'build-admin-css', 'revision-clean', 'revision', callback );
});
gulp.task( 'full-build-js', function( callback ) {
runSequence( 'build-js', 'build-admin-js', 'revision-clean', 'revision', callback );
});
// Svg - combine and clear svg assets
gulp.task( 'svg-opt', function() {
var icons = gulp.src( [ basePaths.src + 'svg/icon-*.svg' ] ).pipe( plugins.svgmin( {
plugins: [
{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}
]
} ) ) // Minification
.pipe( plugins.cheerio( {
run: function( $ ) { //remove fill from icons
$( '[fill]' ).removeAttr( 'fill' );
$( '[fill-rule]' ).removeAttr( 'fill-rule' );
}, parserOptions: { xmlMode: true }
} ) ), pics = gulp.src( [ basePaths.src + 'svg/pic-*.svg' ] ).pipe( plugins.svgmin( {
plugins: [
{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}
]
} ) ); // Minification
// Combine for inline usage
return es.concat( icons, pics ).pipe( plugins.svgstore( { inlineSvg: true } ) )
.pipe( gulp.dest( basePaths.dest + 'svg' ) );
});
// Watchers
gulp.task( 'watch', function() {
gulp.watch( [
basePaths.src + 'sass/*.scss', basePaths.src + 'sass/**/*.scss'
], [ 'full-build-css' ] ).on( 'change', function( evt ) {
changeEvent( evt );
});
gulp.watch( [
basePaths.src + 'js/*.js', basePaths.src + 'js/front/*.js', basePaths.src + 'js/admin/*.js'
], [ 'full-build-js' ] ).on( 'change', function( evt ) {
changeEvent( evt );
});
});
// Default
gulp.task( 'default', [ 'full-build', 'watch' ] );