-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
321 lines (317 loc) · 12.3 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
/****************************************************************************************************/
// MODULES IMPORT
/****************************************************************************************************/
const { src, dest, symlink, lastRun, series, parallel, watch } = require('gulp')
const validator = require('gulp-w3c-html-validator')
const fileinclude = require('gulp-file-include')
const htmlmin = require('gulp-html-minifier2')
const postcss = require('gulp-postcss')
const postcssPresetEnv = require('postcss-preset-env')
const postcssNormalize = require('postcss-normalize')
const postcssImport = require('postcss-import')
const postcssCsso = require('postcss-csso')
const tailwind = require('tailwindcss')
const gulpPurgeCss = require('gulp-purgecss')
const webpack = require('webpack')
const gulpwebpack = require('webpack-stream')
const ts = require('gulp-typescript')
const uglify = require('gulp-uglify-es').default
const mainNpmFiles = require('npmfiles')
const imagemin = require('gulp-imagemin')
const webp = require('gulp-webp')
const svgSprite = require('gulp-svg-sprite')
const favicons = require('gulp-favicons')
const plumber = require('gulp-plumber')
const newer = require('gulp-newer')
const debug = require('gulp-debug')
const gulpIf = require('gulp-if')
const del = require('del')
const flatten = require('gulp-flatten')
const remember = require('gulp-remember')
const cached = require('gulp-cached')
const path = require('path')
const fs = require('fs')
const { create } = require('browser-sync')
/****************************************************************************************************/
// DEV OR PRODUCTION
/****************************************************************************************************/
const isDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'
/****************************************************************************************************/
// CREATE BROWSER-SYNC INSTANCE
/****************************************************************************************************/
const browserSync = create()
/****************************************************************************************************/
// BROWSER-SYNC TASK
/****************************************************************************************************/
const serve = cb => {
browserSync.init({
server: 'build',
watch: true,
open: false,
notify: false
})
cb()
}
exports.serve = serve
/****************************************************************************************************/
// DEL BUILD DIRECTORY TASK
/****************************************************************************************************/
const clean = () => del('build')
exports.clean = clean
/****************************************************************************************************/
// PATHS AND SETTINGS
/****************************************************************************************************/
const cms = {
modx: {
html: 'build/',
css: 'build/assets/css/',
js: 'build/assets/js/',
img: 'build/assets/',
libs: 'build/assets/libs/',
fonts: 'build/assets/fonts/',
favicon: 'build/assets/img/favicons'
}
}
/****************************************************************************************************/
// HTML TASK
/****************************************************************************************************/
const html = () =>
src('src/views/*.html', { since: lastRun(html) })
.pipe(plumber())
.pipe(fileinclude({ basepath: './src/templates' }))
.pipe(gulpIf(!isDevelopment, htmlmin({ collapseWhitespace: true })))
.pipe(dest(cms.modx.html))
exports.html = html
/****************************************************************************************************/
// HTML TEMPLATES TASK
/****************************************************************************************************/
const htmltemplates = () =>
src('src/views/*.html')
.pipe(plumber())
.pipe(fileinclude({ basepath: './src/templates' }))
.pipe(dest(cms.modx.html))
exports.htmltemplates = htmltemplates
/****************************************************************************************************/
// HTML VALIDATION TASK
/****************************************************************************************************/
const validation = () =>
src('build/*.html')
.pipe(validator())
.pipe(validator.reporter())
exports.validation = validation
/****************************************************************************************************/
// CSS TASK
/****************************************************************************************************/
const css = () =>
src('src/css/style.css', { sourcemaps: true })
.pipe(plumber())
.pipe(postcss([
postcssImport({ path: ['src/css'] }),
postcssNormalize({ forceImport: true }),
tailwind(),
postcssPresetEnv({
stage: 2,
features: {
'nesting-rules': true,
'custom-media-queries': true
},
autoprefixer: { cascade: false }
})
]))
.pipe(gulpIf(!isDevelopment, gulpPurgeCss({
content: ['src/views/*.html', 'src/templates/*.html', 'src/js/**/*.js', 'src/js/**/*.ts'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
})))
.pipe(gulpIf(!isDevelopment, postcss([postcssCsso({
restructure: false,
comments: false
})])))
.pipe(gulpIf(!isDevelopment, dest(cms.modx.css), dest(cms.modx.css, { sourcemaps: '.' })))
exports.css = css
/****************************************************************************************************/
// JS TASK WITH BABEL AND WEBPACK
/****************************************************************************************************/
/*const js = () =>
src('src/js/main.js')
.pipe(plumber())
.pipe(gulpwebpack(require('./webpack.config.js'), webpack))
.pipe(dest(cms.modx.js))
exports.js = js*/
/****************************************************************************************************/
// JS TASK WITHOUT BABEL AND WEBPACK, BUT WITH TypeScript
/****************************************************************************************************/
const tsProject = ts.createProject('tsconfig.json')
const js = () =>
src('src/js/**/*.ts')
.pipe(plumber())
.pipe(tsProject())
.pipe(gulpIf(!isDevelopment, uglify()))
.pipe(dest(cms.modx.js))
exports.js = js
/****************************************************************************************************/
// LIBS TASK
/****************************************************************************************************/
const libs = () =>
src(mainNpmFiles(), { base: './node_modules' })
.pipe(flatten({ includeParents: 1 }))
.pipe(newer(cms.modx.libs))
.pipe(dest(cms.modx.libs))
exports.libs = libs
/****************************************************************************************************/
// FONTS TASK
/****************************************************************************************************/
const fonts = () =>
src('src/fonts/**/*.*')
.pipe(newer(cms.modx.fonts))
.pipe(gulpIf(isDevelopment, symlink(cms.modx.fonts), dest(cms.modx.fonts)))
exports.fonts = fonts
/****************************************************************************************************/
// IMG TASK (JPG,PNG,GIF)
/****************************************************************************************************/
const img = () =>
src(['src/img/**/*.*', 'src/images/**/*.*', '!src/img/icons/*.*'], { base: 'src' })
.pipe(newer(cms.modx.img))
.pipe(gulpIf(!isDevelopment, imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({ removeViewBox: false, collapseGroups: true })
])))
.pipe(gulpIf(!isDevelopment, dest(cms.modx.img), symlink(cms.modx.img)))
exports.img = img
/****************************************************************************************************/
// IMG TASK (JPG,PNG,GIF)
/****************************************************************************************************/
const webpimages = () =>
src(['src/img/**/*.{jpg,png,gif}', 'src/images/**/*.{jpg,png,gif}', '!src/img/favicons/*.*'], {
base: 'src',
since: lastRun(webpimages)
})
.pipe(plumber())
.pipe(webp())
.pipe(dest('src'))
exports.webpimages = webpimages
/****************************************************************************************************/
// SVG SPRITE ICONS TASK
/****************************************************************************************************/
const config = {
shape: {
dimension: {
maxWidth: 50,
maxHeight: 50
},
spacing: {
padding: 0,
box: 'icon'
},
transform: [
{
svgo: {
plugins: [
{ removeXMLNS: true },
{ cleanupListOfValues: true },
{ convertShapeToPath: false },
{ removeAttrs: { attrs: ['data-name', 'version'] } },
{ removeStyleElement: true },
{ removeScriptElement: true }
],
floatPrecision: 1
}
}
]
},
svg: {
xmlDeclaration: false,
doctypeDeclaration: false,
dimensionAttributes: false
},
mode: {
stack: {
dest: '.',
sprite: 'img/sprite.svg',
render: {
css: {
template: 'src/templates/icon_template.html',
dest: 'css/modules/sprite.css'
}
}
}
}
}
const svgicons = () =>
src('src/img/icons/*.svg')
.pipe(cached('svg:icons'))
.pipe(remember('svg:icons'))
.pipe(svgSprite(config))
.pipe(dest('src'))
exports.svgicons = svgicons
/****************************************************************************************************/
// COPY FAVICON
/****************************************************************************************************/
const faviconsGenerator = () =>
src('src/img/favicons/favicon.png')
.pipe(favicons({
appName: 'My App',
appShortName: 'App',
appDescription: 'This is my application',
background: '#020307',
path: 'assets/img/favicons/',
url: 'http://mysite.ru/',
display: 'standalone',
lang: 'ru-RU',
orientation: 'portrait',
scope: '/',
start_url: '/',
version: 1.0,
logging: false,
html: 'index.html',
pipeHTML: true,
replace: true,
icons: {
coast: false,
firefox: false,
windows: false,
yandex: false
}
}))
.pipe(dest(cms.modx.favicon))
exports.faviconsGenerator = faviconsGenerator
/****************************************************************************************************/
// WATCHERS
/****************************************************************************************************/
const watchers = cb => {
watch('src/views/*.html', html)
.on('unlink', filepath => {
const filePathFromSrc = path.relative(path.resolve('src/views'), filepath)
const destFilePath = path.resolve(cms.modx.html, filePathFromSrc)
fs.unlinkSync(destFilePath)
})
watch('src/templates/*.html', htmltemplates)
watch('src/css/**/*.css', css)
watch('src/js/**/*.{js,ts}', js)
watch(['src/**/*.{jpg,png,gif,webp}', '!src/img/icons/'], img)
.on('unlink', filepath => {
const filePathFromSrc = path.relative(path.resolve('src'), filepath)
const destFilePath = path.resolve(cms.modx.img, filePathFromSrc)
fs.unlinkSync(destFilePath)
})
watch('src/img/icons/*.svg', svgicons)
.on('unlink', filepath => {
remember.forget('svg:icons', path.resolve(filepath))
delete cached.caches['svg:icons'][path.resolve(filepath)]
})
watch('src/fonts/**/*.*', fonts)
.on('unlink', filepath => {
const filePathFromSrc = path.relative(path.resolve('src/fonts'), filepath)
const destFilePath = path.resolve(cms.modx.fonts, filePathFromSrc)
fs.unlinkSync(destFilePath)
})
cb()
}
/****************************************************************************************************/
// GLOBAL TASKS
/****************************************************************************************************/
const build = series(svgicons, parallel(html, css, js, libs, fonts, img))
exports.build = build
const dev = series(build, parallel(watchers, serve))
exports.dev = dev