forked from qodesmith/datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
90 lines (77 loc) · 2.6 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
const gulp = require('gulp');
const babel = require('gulp-babel');
const UglifyJS = require('uglify-js');
const concat = require('gulp-concat-util');
const less = require('gulp-less');
const prefix = require('gulp-autoprefixer');
const fs = require('fs');
gulp.task('es2015', function() {
return gulp
.src('datepicker.js')
.pipe(babel({presets: [
['es2015', {modules: false}] // https://goo.gl/SnO01g
]}))
.on('error', onError)
.pipe(concat('datepicker-es5.js'))
.pipe(gulp.dest('./'));
});
gulp.task('uglify-js', function(done) {
// Followed the example here - https://goo.gl/AZD4r8
let code = fs.readFileSync('./datepicker-es5.js', 'utf-8').split('\n');
// Because babel puts the `_typeof` function in the global scope :/
let _typeof = code[0];
code.shift();
code.splice(2, 0 , _typeof);
code = code.join('\n');
let toplevel = (function() {
try {
// UglifyJS parse errors are horrendously vague.
// Try / catch will give us insight when errors are thrown.
return UglifyJS.parse(code);
} catch (error) {
console.log('ERROR:', error);
}
})();
let compressor = UglifyJS.Compressor({
sequences: 400,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true,
drop_console: false
});
toplevel.figure_out_scope();
let compressed_ast = toplevel.transform(compressor);
// According to the docs, this will usually break your code.
// That's EXACTLY the results we keep getting.
// compressed_ast = UglifyJS.mangle_properties(compressed_ast);
compressed_ast.figure_out_scope();
compressed_ast.compute_char_frequency();
compressed_ast.mangle_names();
fs.writeFileSync('datepicker.min.js', compressed_ast.print_to_string());
// fs.unlinkSync('./datepicker-es5.js');
return done(); // Async completion.
});
gulp.task('less', function() {
return gulp.src('less/datepicker.less')
.pipe(less()) // datepicker.less > datepicker.css
.on('error', onError)
.pipe(prefix({browsers: ['last 2 versions']})) // browserslist: https://github.com/ai/browserslist
.on('error', onError)
.pipe(concat('datepicker.css'))
.pipe(gulp.dest('./'));
});
gulp.task('watch', function(done) {
gulp.watch('./datepicker.js', gulp.series('es2015', 'uglify-js'));
gulp.watch('./less/datepicker.less', gulp.series('less'));
return done(); // Async completion.
});
gulp.task('default', gulp.series(/*'es2015', 'uglify-js', */'less', 'watch'));
// http://goo.gl/SboRZI
// Prevents gulp from crashing on errors.
function onError(err) {
console.log(err);
this.emit('end');
}