-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
122 lines (107 loc) · 4.07 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
/* eslint-disable no-console */
/* eslint-disable no-multi-spaces */
// Includes - Defining what will be used below.
// These are pulled in from the node_modules folder.
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var livereload = require('gulp-livereload');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var sassLint = require('gulp-sass-lint');
var uglify = require('gulp-uglify');
var insert = require('gulp-insert');
var crlf = require('gulp-line-ending-corrector');
// Basic error logging function to be used below
function errorLog (error) {
console.error.bind(error);
this.emit('end');
}
// Uglify JS - Targets all .js files in the _js folder and converts
// them to functionally identical code that uses less bytes in the _scripts folder
gulp.task('uglify', function () {
gulp.src('_js/*.js')
.pipe(uglify())
.on('error', errorLog)
.pipe(insert.append('\n'))
.pipe(crlf({eolc:'CRLF', encoding:'utf8'}))
.pipe(gulp.dest('_scripts'));
});
// Create expanded and .min versions of Sass styles in the _styles folder as CSS
gulp.task('sass', function () {
gulp.src('_sass/style.scss')
.pipe(sass({ outputStyle: 'expanded' })
.on('error', sass.logError))
.pipe(crlf({eolc:'CRLF', encoding:'utf8'}))
.pipe(gulp.dest('_styles/'))
.pipe(rename('style.min.css'))
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(crlf({eolc:'CRLF', encoding:'utf8'}))
.pipe(gulp.dest('_styles/'))
.pipe(livereload());
});
// Create expanded and .min versions of Sass styles in the _styles folder as CSS
gulp.task('sassfont', function () {
gulp.src('_sass/fonts.scss')
.pipe(sass({ outputStyle: 'expanded' })
.on('error', sass.logError))
.pipe(crlf({eolc:'CRLF', encoding:'utf8'}))
.pipe(gulp.dest('_styles/'))
.pipe(rename('fonts.min.css'))
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(crlf({eolc:'CRLF', encoding:'utf8'}))
.pipe(gulp.dest('_styles/'))
.pipe(livereload());
});
// Lint the main.js file to ensure code consistency and catch any errors
gulp.task('lint', function () {
return gulp.src('_js/main.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('sasslint', function () {
gulp.src(['_sass/*.scss', '_sass/*.sass'])
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
// Run a local server on port 8000
gulp.task('serve', function (done) {
var express = require('express');
var app = express();
//path to the folder that will be served. __dirname is project root
var path = __dirname;
app.use(express.static(path));
app.listen(8000, function () {
done();
});
});
// Enable live reload listening from HTML files in the browser
// if you have the LiveReload browser extension installed.
gulp.task('html', function () {
gulp.src('*.html')
.pipe(livereload());
});
// Watch for changes in JS, Sass, and HTML files, then Lint,
// Uglify, Process the Sass, and reload the browser automatically
gulp.task('watch', function () {
gulp.watch('_js/*.js', ['lint']);
gulp.watch('_js/*.js', ['uglify']);
gulp.watch('_sass/*', ['sass', 'sassfont']);
gulp.watch('*.html', ['html']);
livereload.listen();
});
// Automatically opens the local server in your default browser
gulp.task('open', function () {
var url = 'http://localhost:8000';
var OS = process.platform;
var executable = '';
//OS Specific values for opening files.
if (OS == 'darwin') { executable = 'open "' + url + '"'; }
if (OS == 'linux') { executable = 'xdg-open ' + url; }
if (OS == 'win32') { executable = 'explorer ' + url; }
//Run the OS specific command to open the url in the default browser
require('child_process').exec(executable);
});
// The default Gulp task that happens when you run gulp.
// It runs all the other gulp tasks above in the correct order.
gulp.task('default', ['sass', 'sassfont', 'lint', 'uglify', 'watch', 'serve', 'open']);