-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
331 lines (288 loc) · 8.63 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
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
'use strict';
// This gulpfile makes use of new JavaScript features.
// Babel handles this without us having to do anything. It just works.
// You can read more about the new JavaScript features here:
// https://babeljs.io/docs/learn-es2015/
const { src, dest, parallel, series, watch } = require('gulp');
const del = require('del');
const babel = require('gulp-babel');
const browserSync = require('browser-sync').create();
var fs = require('fs');
var pkg = JSON.parse(fs.readFileSync('./package.json'));
const imagemin = require('gulp-imagemin');
const argv = require('yargs').argv;
const $ = require('gulp-load-plugins')();
const reload = browserSync.reload;
const isDemo = argv.build === 'demo';
const terser = require('gulp-terser');
const zipFileName = `markitasdone.com__${pkg.name}__v${pkg.version}`;
// Lint JavaScript
function lint() {
return src(['src/scripts/**/*.js', '!node_modules/**'])
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.if(!browserSync.active, $.eslint.failAfterError()));
}
exports.lint = lint;
// Optimize images
function images() {
return src('src/images/**/*')
.pipe(imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{ removeViewBox: true },
{ cleanupIDs: false }
]
})
]))
.pipe(dest('dist/images'))
.pipe($.size({ title: 'images' }));
}
exports.images = images;
// Copy all files at the root level (src)
function copy() {
return src(
[
'src/*',
'!src/*.html',
'!src/images-placeholder'
],
{
dot: true
}
)
.pipe(dest('dist'))
.pipe($.size({ title: 'copy' }));
}
exports.copy = copy;
// Copy all files at the root level (src)
function fonts(){
return src('src/fonts/**/*')
.pipe(dest('dist/fonts/'))
.pipe($.size({ title: 'copy fonts' }))
}
exports.fonts = fonts;
// Copy all files at the root level (src)
function placeholder(){
return src('src/images-placeholder/**/*')
.pipe(dest('dist/images/'))
.pipe($.size({ title: 'copy fonts' }));
}
exports.placeholder = placeholder;
// Compile and automatically prefix stylesheets
function styles() {
const AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
// For best performance, don't add Sass partials to `gulp.src`
return src(['src/styles/**/*.scss', 'src/styles/**/*.css'])
.pipe($.newer('.tmp/styles'))
.pipe(
$.sass({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
}).on('error', $.sass.logError)
)
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe($.size({ title: 'styles' }))
.pipe(dest('.tmp/styles'));
}
exports.styles = styles;
// Concatenate and minify JavaScript. Optionally transpiles ES2015 code to ES5.
// to enable ES2015 support remove the line `'only': 'gulpfile.babel.js',` in the
// `.babelrc` file.
function scripts() {
return src([
// Note: Since we are not using useref in the scripts build pipeline,
// you need to explicitly list your scripts here in the right order
// to be correctly concatenated
'./src/scripts/main.js'
// Other scripts
])
.pipe($.newer('.tmp/scripts'))
.pipe($.sourcemaps.init())
.pipe(babel({
presets: ['@babel/env']
}))
.pipe($.sourcemaps.write())
.pipe(dest('.tmp/scripts'))
.pipe($.concat('main.min.js'))
.pipe(terser())
// Output files
.pipe($.size({ title: 'scripts' }))
.pipe(dest('dist/scripts'))
.pipe(dest('.tmp/scripts'));
}
exports.scripts = scripts;
// Scan your HTML for assets & optimize them
function html() {
return ( src('src/**/*.html')
.pipe(
$.useref({
searchPath: ['.', '.tmp', 'src']
})
)
.pipe($.if('*.css', $.cssnano({ safe: true, autoprefixer: false })))
.pipe($.if('*.js', terser()))
// Minify any HTML
.pipe(
$.if(
file => $.match(file, '*.html') && isDemo,
$.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true
})
)
)
// Output files
.pipe($.if('*.html', $.size({ title: 'html', showFiles: true })))
.pipe(dest('dist'))
);
}
exports.html = html;
// Clean output directory
function clean(cb) {
del(['.tmp', 'dist/*', '!dist/.git'], { dot: true });
cb();
}
exports.clean = clean;
// Watch files for changes & reload
// const serve = series(parallel(styles, scripts), function () {
// browserSync.init({
// notify: false,
// // Customize the Browsersync console logging prefix
// logPrefix: 'WSK',
// // Allow scroll syncing across breakpoints
// scrollElementMapping: ['main', '.mdl-layout'],
// // Run as an https by uncommenting 'https: true'
// // Note: this uses an unsigned certificate which on first access
// // will present a certificate warning in the browser.
// // https: true,
// server: {
// baseDir: ['.tmp', 'src'],
// routes: {
// '/node_modules': 'node_modules'
// }
// },
// port: 3000
// });
// watch('src/**/*.html').on('change', reload);
// watch('src/styles/**/*.{scss,css}', styles).on('change', reload);
// watch('src/scripts/**/*.js', parallel(lint, scripts)).on('change', reload);
// watch('src/images/**/*').on('change', reload);
// });
function start() {
browserSync.init({
notify: false,
// Customize the Browsersync console logging prefix
logPrefix: 'WSK',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main', '.mdl-layout'],
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: {
baseDir: ['.tmp', 'src'],
routes: {
'/node_modules': 'node_modules'
}
},
port: 3000
});
watch('src/**/*.html').on('change', reload);
watch('src/styles/**/*.{scss,css}', styles).on('change', reload);
watch('src/scripts/**/*.js', parallel(lint, scripts)).on('change', reload);
watch('src/images/**/*').on('change', reload);
}
exports.start = start;
exports.serve = series(parallel(styles, scripts), start);
// Build and serve the output from the dist build
function servedist() {
browserSync.init({
notify: false,
logPrefix: 'WSK',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main'],
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: 'dist',
port: 3001
})
}
exports.servedist = servedist;
// Zip demo files
function zip() {
return src('dist/**/*', { dot: true })
.pipe($.zip(`${zipFileName}${isDemo ? '__DEMO' : ''}.zip`))
.pipe(dest('.tmp'));
}
exports.zip = zip;
// Zip Release files
function ziprelease() {
return src('.tmp/release/**/*', { dot: true })
.pipe($.zip(`${zipFileName}.zip`))
.pipe(dest('.tmp'));
}
exports.ziprelease = ziprelease;
// Build production files, the default task
exports.default = series(clean, styles, lint, html, scripts, images, copy, fonts, zip);
/**
* Generrate release commands
* @return {Array} commands
*/
function getReleaseCommands() {
const basePath = './.tmp/release';
const cp = [
'dist',
'src',
'.babelrc',
'.gitattributes',
'.gitignore',
'gulpfile.babel.prod.js',
'package.prod.json',
'README.md',
'CHANGELOG.md',
'LICENSE'
].map(p => `cp -rf ./${p} ${basePath}/${p.replace('.prod', '')}`);
return [
`mkdir ${basePath}`,
...cp,
`rm -rf ${basePath}/src/images`,
`mv ${basePath}/src/images-placeholder ${basePath}/src/images`
];
}
exports.getReleaseCommands = getReleaseCommands;
// Create release directory into .tmp for generating release zip
// gulp.task('cp-release', run(getReleaseCommands()));
// // Build release zip
// gulp.task('release', ['clean'], cb => {
// runSequence(
// 'styles',
// ['lint', 'html', 'scripts', 'cp-placeholders', 'copy', 'fonts'],
// 'cp-release',
// 'zip-release',
// cb
// );
// });