-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
executable file
·204 lines (181 loc) · 4.58 KB
/
gulpfile.babel.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
import { dest, series, src, task, watch } from 'gulp'
import yargs from 'yargs'
import del from 'del'
import webpack from 'webpack-stream'
import gulpif from 'gulp-if'
import standard from 'gulp-standard'
import notify from 'gulp-notify'
import plumber from 'gulp-plumber'
import rename from 'gulp-rename'
import sass from 'gulp-sass'
import sassLint from 'gulp-sass-lint'
import postcss from 'gulp-postcss'
import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
import sorter from 'css-declaration-sorter'
let browserSync = require('browser-sync').create()
let { reload } = browserSync
let { exec } = require('child_process')
/**
* Set up prod/dev tasks
*/
const PROD = !(yargs.argv.dev)
/**
* Set up file paths
*/
const _assetsDir = './assets'
const _srcDir = `${_assetsDir}/src`
const _distDir = `${_assetsDir}/dist`
const _devDir = `${_assetsDir}/dev`
const _buildDir = PROD ? _distDir : _devDir
/**
* Error notification settings
*/
const errorAlert = err => {
let _notifyOpts = {
title: 'Gulp <%= error.name %>: <%= error.plugin %>',
message: '<%= error.stack %>',
sound: 'Basso'
}
notify.onError(_notifyOpts)(err)
}
/**
* Clean the dist/dev directories
*/
task('clean', () => del(_buildDir))
/**
* Lints the gulpfile for errors
*/
task('lint:gulpfile', () => {
return src('gulpfile.*')
.pipe(standard())
.pipe(standard.reporter('default'))
.on('error', errorAlert)
})
/**
* Lints the source js files for errors
*/
task('lint:js', () => {
let _src = [
`${_srcDir}/js/**/*.js`,
'!**/libs/**/*.js'
]
return src(_src)
.pipe(standard())
.pipe(standard.reporter('default'))
.on('error', errorAlert)
})
/**
* Lint the Sass
*/
task('lint:sass', () => {
return src(`${_srcDir}/scss/**/*.scss`)
.pipe(sassLint({
'merge-default-rules': true
}))
.pipe(sassLint.format())
})
/**
* Lints all the js files for errors
*/
task('lint', series('lint:gulpfile', 'lint:js', 'lint:sass', cb => cb()))
/*
* Fixes standardJS errors in Gulpfile
*/
task('standard:gulpfile', () => exec('standard --fix gulpfile.*'))
/*
* Fixes standardJS errors in JS files
*/
task('standard:js', () => exec(`standard --fix ${_srcDir}/js/**/*.js`))
/**
* Fixes all the JS files with standardJS
*/
task('standard', series('standard:gulpfile', 'standard:js', cb => cb()))
/**
* Concatenates, minifies and renames the source JS files for dist/dev
*/
task('scripts', () => {
return src(`${_srcDir}/js/index.js`)
.pipe(plumber({ errorHandler: errorAlert }))
.pipe(webpack({
entry: `${_srcDir}/js/index.js`,
output: {
filename: `index.js`
},
mode: PROD ? 'production' : 'development',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
}
]
}
}))
.pipe(gulpif(PROD, rename({ suffix: '.min' })))
.pipe(dest(_buildDir))
.pipe(reload({ stream: true }))
.on('error', errorAlert)
.pipe(
notify({
message: `${PROD ? 'Prod' : 'Dev'} scripts have been transpiled${PROD ? ' and minified' : ''}`,
onLast: true
})
)
})
/**
* Compiles and compresses the source Sass files for dist/dev
*/
task('styles', () => {
const _sassOpts = {
outputStyle: 'expanded',
sourceComments: !PROD
}
const _postcssOpts = [
sorter({ order: 'smacss' }),
autoprefixer({ grid: true })
]
if (PROD) _postcssOpts.push(cssnano())
return src(`${_srcDir}/scss/style.scss`)
.pipe(plumber({ errorHandler: errorAlert }))
.pipe(sass(_sassOpts))
.pipe(gulpif(PROD, rename({ suffix: '.min' })))
.pipe(postcss(_postcssOpts))
.pipe(dest(_buildDir))
.pipe(reload({ stream: true }))
.on('error', errorAlert)
.pipe(
notify({
message: `${PROD ? 'Prod' : 'Dev'} styles have been compiled${PROD ? ' and minified' : ''}`,
onLast: true
})
)
})
/**
* Builds for distribution (staging or production)
*/
task('build', series('clean', 'styles', 'scripts', cb => cb()))
/**
* Builds assets and reloads the page when any php, html, img or dev files change
*/
task('watch', series('build', () => {
browserSync.init({
server: {
baseDir: './'
},
notify: true
})
watch(`${_srcDir}/scss/**/*`, series('styles'))
watch(`${_srcDir}/js/**/*`, series('scripts'))
watch('./**/*.html').on('change', reload)
}))
/**
* Backup default task just triggers a build
*/
task('default', series('build', cb => cb()))