forked from certainlyakey/timber-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
252 lines (192 loc) · 6.06 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
// GULP CONFIG
var _gulp = require('gulp');
// Allows to output different builds depending on current environment. .env is located in the root of the repo
// ignored if .env not found
var _dotenv = require('dotenv').config({ path: './../../../.env' });
var _gutil = require('gulp-util');
var _sass = require('gulp-sass');
// Restoring on build error
var _plumber = require('gulp-plumber');
// CSS minification
var _cssnano = require('gulp-cssnano');
// Joining CSS files
var _concat = require('gulp-concat');
// Build done notifications
var _notify = require('gulp-notify');
// Uglifying JS
var _uglify = require('gulp-uglify');
// Creates svg sprites from source svg files
var _svgsprite = require('gulp-svg-sprite');
// Checks JS for mistakes. jshint-stylish should be installed too
var _jshint = require('gulp-jshint');
// Autoprefixes SASS
var _autoprefixer = require('gulp-autoprefixer');
// Generates SASS/JS sourcemaps for easier inbrowser debugging
var _sourcemaps = require('gulp-sourcemaps');
// These handle merging JS modules
var _browserify = require('browserify');
var _source = require('vinyl-source-stream');
var _buffer = require('vinyl-buffer');
// Allows to use ES2015 in JS. babel-preset-es2015 should be installed too
var _babelify = require('babelify');
// Adds browser feature test results to HTML and JS
var _modernizr = require('gulp-modernizr');
// Source for variables shared between PHP, gulp, JS, and SASS
var _common_config = require('./common_config.json');
// Injects variables from common_config file to SASS
var _sassvariables = require('gulp-sass-variables');
// Allows to inject css @imports instead of just linking to them (Sass doesn't have this option by default)
var _sassmoduleimporter = require('sass-module-importer');
// Compile localization files from .po to .mo
var _gettext = require('gulp-gettext');
// TASKS CONFIG
// Standalone JS libraries to copy to production build
var libs_copied_from_nodemodules = [
'node_modules/jquery/dist/jquery.min.js'
];
// Environment variables
// if defined in a .env file or if defined as a gulp commandline argument (gulp --env=true)
var is_env_dev = true;
if (typeof process.env.ENV !== 'undefined') {
is_env_dev = (process.env.ENV === 'dev');
}
if (typeof _gutil.env.env !== 'undefined') {
is_env_dev = (_gutil.env.env === 'dev');
}
// SVG sprite config
var svgsprite_config = {
mode : {
symbol : {
dest : '.',
prefix : '.u-svg-%s',
dimensions : '-size',
sprite : 'img/sprite.svg',
render : {
scss : {
dest : 'scss/autogenerated/_svg-sprite.scss'
}
}
}
}
};
// Injecting common_config variables into SASS
var common_config_sass_vars = {};
for (var variable in _common_config) {
common_config_sass_vars['$_' + variable] = _common_config[variable];
}
common_config_sass_vars['$_is-env-dev'] = is_env_dev;
// TASKS
_gulp.task('svg-sprites', function() {
_gulp.src('img/svg-sprite-source/*.svg')
.pipe(_svgsprite(svgsprite_config)).on('error', function(error){ console.log(error); })
.pipe(_gulp.dest('./'))
.pipe(is_env_dev ? _notify('SVG sprite created successfully!') : _gutil.noop() );
});
_gulp.task('lint-css', function lintCssTask() {
const _gulp_stylelint = require('gulp-stylelint');
return _gulp
.src('scss/**/*.scss')
.pipe(_gulp_stylelint({
reporters: [
{formatter: 'string', console: true}
]
}));
});
_gulp.task('styles', ['lint-css'], function() {
_gulp.src(['scss/style.scss'])
.pipe(_plumber())
.pipe( is_env_dev ? _sourcemaps.init() : _gutil.noop() )
.pipe(_sassvariables(common_config_sass_vars))
.pipe(_sass({
style: 'expanded',
includePaths: [__dirname + '/node_modules/susy/sass'],
importer: _sassmoduleimporter()
}))
.pipe(_autoprefixer())
.pipe(_cssnano({
zindex: false
}))
.pipe(_concat('style.css'))
.pipe( is_env_dev ? _sourcemaps.write() : _gutil.noop() )
.pipe(_gulp.dest('./'))
.pipe(is_env_dev ? _notify('CSS compiled and concatenated successfully!') : _gutil.noop() );
});
_gulp.task('jshint', function(){
var src = [
'Gulpfile.js',
'js/modules/*.js'
];
_gulp.src(src)
.pipe(_jshint())
.pipe(_jshint.reporter(require('jshint-stylish')));
});
_gulp.task('modernizr', function(){
var src = [
'./js/scripts.min.js',
'./style.css'
];
_gulp.src(src)
.pipe(_modernizr('modernizr.min.js', {
options: ['setClasses'],
classPrefix: 'js-supports-'
}))
.pipe(_uglify())
.pipe(_gulp.dest('js'))
.on('error', _gutil.log);
});
_gulp.task('scripts', function(){
var bundler = _browserify('js/main.js');
bundler.transform(_babelify, {
presets: ['es2015']
})
.bundle()
.on('error', _gutil.log)
.pipe(_source('scripts.min.js'))
.pipe(_buffer())
// don't uglify if we're in local environment, do uglify if it's something other
.pipe(is_env_dev ? _gutil.noop() : _uglify())
.pipe(_gulp.dest('js'))
.pipe(is_env_dev ? _notify('JS compiled and concatenated successfully!') : _gutil.noop());
});
_gulp.task('po_to_mo', () => {
_gulp.src('languages/*.po')
.pipe(_gettext())
.pipe(_gulp.dest('languages'));
});
_gulp.task('copy_to_libs', () => _gulp
.src(libs_copied_from_nodemodules)
.pipe(_gulp.dest('js/libs'))
);
_gulp.task('default', [
'copy_to_libs',
'styles',
'scripts',
'svg-sprites',
'modernizr',
'po_to_mo'
]);
// WATCH
_gulp.task('watch', ['default'], function(){
// Gulpfile.js:
_gulp.watch('Gulpfile.js', [
'jshint'
]);
// Scripts:
_gulp.watch(['js/main.js', 'js/modules/*.js'], [
'jshint',
'scripts'
]);
// Styles:
_gulp.watch('scss/**/*.scss', [
'styles'
]);
// SVG:
_gulp.watch('img/svg-sprite-source/*.svg', [
'svg-sprites'
]);
// .po files:
_gulp.watch('languages/*.po', [
'po_to_mo'
]);
});