-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
400 lines (365 loc) · 11.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// get gulp dependencies
var argv = require('minimist')(process.argv.slice(2));
var autoprefixer = require('gulp-autoprefixer');
var babelify = require("babelify");
var browserify = require('browserify');
var browserSync = require('browser-sync');
var buffer = require('vinyl-buffer');
var concat = require('gulp-concat');
var exec = require('child-process-promise').exec;
var fs = require('fs');
var globby = require('globby');
var gulp = require('gulp');
var gulpSequence = require('gulp-sequence').use(gulp);
var gls = require('gulp-live-server');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var inject = require('gulp-inject-string');
var jshint = require('gulp-jshint');
var minifyCSS = require('gulp-minify-css');
var nunjucksRender = require('gulp-nunjucks-render');
var pathExists = require('path-exists');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var source = require('vinyl-source-stream');
var sourceMaps = require('gulp-sourcemaps');
var through = require('through2');
var uglify = require('gulp-uglify');
var autoPrefixBrowserList = [
'last 2 version',
'safari 5',
'ie 8',
'ie 9',
'opera 12.1',
'ios 6',
'android 4'
];
var fileNames = {
html: 'index.html',
navigator: 'navigator.js',
scripts: 'scripts.js',
styles: 'styles.css',
};
var flags = {
create: ['c', 'create', 'g', 'generate'],
delete: ['d', 'delete', 'r', 'remove'],
production: ['p', 'prod', 'production'],
development: ['d', 'dev', 'development'],
}
var paths = {
root: "public",
images: {
origin:'public/assets/images/*',
dest:'build/assets/images',
},
fonts: {
origin:'public/assets/fonts/*',
dest:'build/assets/fonts',
},
scripts: {
all:['public/components/**/*.js', 'public/pages/**/*.js', 'public/general/scripts/*.js',],
general: 'public/general/scripts/',
index: 'public/' + fileNames.scripts,
navigator: 'public/general/scripts/navigator.js',
},
styles: {
all: ['public/**/*.scss', 'public/**/*.sass'],
general: 'public/general/styles',
index: 'public/' + fileNames.styles,
main: 'public/general/styles/index.scss',
},
views: {
all: ['public/pages/**/*.html', 'public/components/**/*.html','public/**/*.njk'],
general: 'public/general/views',
index: 'public/' + fileNames.html,
main: 'public/general/views/index.njk',
},
build: {
root: 'build',
scripts: 'build',
styles: 'build',
},
pages: 'public/pages',
components: 'public/components',
};
var commands = {
compile: {
html: 'html',
scripts: 'scripts',
styles: 'styles',
},
deploy: {
fonts: 'fonts-deploy',
images: 'images-deploy',
html: 'html-deploy',
misc: 'misc-deploy',
scaffold: 'scaffold',
scripts: 'scripts-deploy',
styles: 'styles-deploy',
},
clean: 'clean',
clearExample: 'clear-example',
component: 'component',
concatJs: 'concatJs',
htmlReload: 'html-reload',
images: 'images',
jsBrowserify: 'jsBrowserify',
page: 'page',
lint: 'lint',
serve: 'serve',
};
//this is our master task when you run `gulp` in CLI / Terminal
gulp.task('default',
[
commands.lint,
commands.compile.html,
commands.compile.scripts,
commands.compile.styles,
commands.serve,
], function() {
//a list of watchers, so it will watch all of the following files waiting for changes
gulp.watch(paths.scripts.all, [commands.compile.scripts]);
gulp.watch(paths.styles.all, [commands.compile.styles]);
gulp.watch(paths.images.origin, [commands.images]);
gulp.watch(paths.views.all, [commands.compile.html, commands.htmlReload]);
});
// this will build for production
gulp.task('build', [commands.clean], gulpSequence(
commands.compile.html,
commands.compile.scripts,
commands.compile.styles,
[
commands.deploy.scripts,
commands.deploy.styles,
commands.deploy.images,
commands.deploy.fonts,
commands.deploy.html,
]
));
var compileForDev = function(){
gulpSequence(
commands.lint,
commands.compile.html,
commands.compile.scripts,
commands.compile.styles
);
}
// SCRIPTS
//compiling our Javascripts
gulp.task(commands.compile.scripts, function() {
// gulp expects tasks to return a stream, so we create one here.
var bundledStream = through();
bundledStream
// turns the output bundle stream into a stream containing
// the normal attributes gulp plugins expect.
.pipe(source(fileNames.scripts))
// the rest of the gulp task, as you would normally write it.
.pipe(buffer())
.pipe(sourceMaps.init({loadMaps: true}))
// Add gulp plugins to the pipeline here.
.on('error', gutil.log)
.pipe(sourceMaps.write('./'))
.pipe(gulp.dest(paths.root))
.pipe(browserSync.reload({stream: true}));
// "globby" replaces the normal "gulp.src" as Browserify
// creates it's own readable stream.
globby(paths.scripts.all).then(function(entries) {
// create the Browserify instance.
var b = browserify({
entries: entries,
debug: true,
});
// pipe the Browserify stream into the stream we created earlier
// this starts our gulp pipeline.
b
.transform("babelify", {presets: ["es2015"]})
.bundle()
.pipe(bundledStream)
}).catch(function(err) {
// ensure any errors from globby are handled
bundledStream.emit('error', err);
});
// finally, we return the stream, so gulp knows when this task is done.
return bundledStream;
});
//moving our Javascripts to deployment
gulp.task(commands.deploy.scripts, function() {
//this is where our dev JS scripts are
return gulp.src(paths.scripts.index)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
//compress :D
.pipe(uglify())
//where we will store our finalized, compressed script
.pipe(gulp.dest(paths.build.scripts));
});
gulp.task(commands.lint, function(){
return gulp.src(paths.scripts.all)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
// linting!
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
});
// STYLES
//compiling our SCSS files
gulp.task(commands.compile.styles, function() {
//the initializer / master SCSS file, which will just be a file that imports everything
return gulp.src(paths.styles.main)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber({
errorHandler: function (err) {
console.log(err);
this.emit('end');
}
}))
//get sourceMaps ready
.pipe(sourceMaps.init())
//include SCSS and list every "include" folder
.pipe(sass({
errLogToConsole: true,
}))
.pipe(autoprefixer({
browsers: autoPrefixBrowserList,
cascade: true
}))
//catch errors
.on('error', gutil.log)
//the final filename of our combined css file
.pipe(concat(fileNames.styles))
//get our sources via sourceMaps
.pipe(sourceMaps.write())
//where to save our final, compressed css file
.pipe(gulp.dest(paths.root))
//notify browserSync to refresh
.pipe(browserSync.reload({stream: true}));
});
//compiling our SCSS files for deployment
gulp.task(commands.deploy.styles, function() {
//the initializer / master SCSS file, which will just be a file that imports everything
return gulp.src(paths.styles.index)
.pipe(plumber())
// compress
.pipe(minifyCSS())
//where to save our final, compressed css file
.pipe(gulp.dest(paths.build.styles));
});
// HTML
//basically just keeping an eye on all HTML files
gulp.task(commands.htmlReload, function() {
//watch any and all HTML files and refresh when something changes
return gulp.src(paths.views.all)
.pipe(plumber())
.pipe(browserSync.reload({stream: true}))
//catch errors
.on('error', gutil.log);
});
gulp.task(commands.compile.html, function() {
// Gets .html and .njk files in pages
return gulp.src(paths.views.main)
// Renders template with nunjucks
.pipe(nunjucksRender({
path: [paths.root]
}))
// output files in public folder
.pipe(gulp.dest(paths.root))
});
//migrating over all HTML files for deployment
gulp.task(commands.deploy.html, function() {
//grab html index file
gulp.src(paths.views.index)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
.pipe(gulp.dest(paths.build.root));
});
// ASSETS & MISC
//compressing images & handle SVG files
gulp.task(commands.images, function(tmp) {
gulp.src(paths.images.origin)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
.pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
.pipe(gulp.dest(paths.images.dest));
});
//compressing images & handle SVG files
gulp.task(commands.deploy.images, function() {
gulp.src(paths.images.origin)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
.pipe(gulp.dest(paths.images.dest));
});
// fonts
gulp.task(commands.deploy.fonts, function() {
gulp.src(paths.fonts.origin)
//prevent pipe breaking caused by errors from gulp plugins
.pipe(plumber())
.pipe(gulp.dest(paths.fonts.dest));
});
// GENERAL
//cleans our build directory in case things got deleted
gulp.task(commands.clean, function() {
exec('rm -rf build')
.catch(function(err){ });
exec('rm ' + paths.views.index)
.catch(function(err){ });
exec('rm ' + paths.scripts.index)
.catch(function(err){ });
exec('rm ' + paths.styles.index)
.catch(function(err){ });
exec('rm ' + paths.scripts.index + '.map')
.catch(function(err){ });
});
gulp.task(commands.clearExample, [commands.clean], function() {
// beware looking further into the bowels of this custom cleanse, ye be warned
require('./gulpFsTasks.js').cleanExApp(paths, compileForDev);
});
//create folders using shell
gulp.task(commands.deploy.scaffold, function() {
exec('mkdir build');
exec('mkdir build/assets');
exec('mkdir build/assets/fonts');
exec('mkdir build/assets/images');
});
gulp.task(commands.serve, function() {
var productionMode = false;
var env;
// running with -p or -production flag runs through build folder
for (var flag in argv){
if (flags.production.includes(flag)) {
productionMode = true;
}
}
if (productionMode) {
var env = 'production';
// we want to make sure our build files exist before calling them from app.js
gulpSequence(
commands.deploy.scripts,
commands.deploy.styles,
commands.deploy.images,
commands.deploy.fonts,
commands.deploy.html
)();
} else {
var env = 'development';
}
// launch our server
var port = 3000;
var server = gls('app.js', {env: {NODE_ENV: env, PORT: port}}, false);
server.start();
// we use browserSync's great live reloading, listening through proxy server
browserSync.init({
proxy: `localhost:${port}`,
port: (port+1),
notify: true,
});
});
// CREATE components and pages
// generate a component
gulp.task(commands.component, function(){
require('./gulpFsTasks.js').componentTask(argv, flags, paths, compileForDev);
});
//generate a page
gulp.task(commands.page, function(){
require('./gulpFsTasks.js').pageTask(argv, flags, fileNames, paths, compileForDev);
});