UNPKG

2.78 kBJavaScriptView Raw
1// Include the required tools used on tasks
2var gulp = require('gulp'),
3 jshint = require('gulp-jshint'),
4 rename = require('gulp-rename'),
5 uglify = require('gulp-uglify'),
6 saveLicense = require('uglify-save-license'),
7 babel = require("gulp-babel"),
8 postcss = require('gulp-postcss'),
9 cleanCSS = require('gulp-clean-css'),
10 cssbeautify = require('gulp-cssbeautify'),
11 autoprefixer = require('autoprefixer'),
12 del = require('del');
13
14var Server = require('karma').Server;
15var browserSync = require('browser-sync').create();
16
17// Specify the Source files
18var SRC_JS = 'src/js/*.js';
19var SRC_CSS = 'src/css/*.css';
20
21// Specify the Destination folders
22var DEST_JS = 'dist/js';
23var DEST_CSS = 'dist/css';
24
25// Example pages
26var EXAMPLE_HTML = 'examples/*.html';
27
28// BUILD JS
29function build_js(cb) {
30 gulp.src(SRC_JS)
31 .pipe(babel({
32 presets: ['@babel/env']
33 }))
34 .pipe(gulp.dest(DEST_JS))
35 .pipe(uglify({
36 output: {
37 comments: saveLicense
38 }
39 }))
40 .pipe(rename({
41 suffix: '.min'
42 }))
43 .pipe(gulp.dest(DEST_JS));
44
45 cb();
46}
47
48// BUILD CSS
49function build_css(cb) {
50 gulp.src(SRC_CSS)
51 .pipe(postcss( [autoprefixer()] ))
52 .pipe(cssbeautify({ autosemicolon: true }))
53 .pipe(gulp.dest(DEST_CSS))
54 .pipe(cleanCSS({compatibility: 'ie8'}))
55 .pipe(rename({suffix: '.min'}))
56 .pipe(gulp.dest(DEST_CSS));
57
58 cb();
59}
60
61// LINT
62function lint_js(cb) {
63 gulp.src(SRC_JS)
64 .pipe(jshint({ "esversion": 8 }))
65 .pipe(jshint.reporter('default'));
66
67 cb();
68}
69
70// CLEAN
71function clean_js(cb) {
72 del.sync([DEST_JS]);
73
74 cb();
75}
76
77function clean_css(cb) {
78 del.sync([DEST_CSS]);
79
80 cb();
81}
82
83// WATCH
84function watch(cb) {
85 gulp.watch(SRC_JS, build_js);
86 gulp.watch(SRC_CSS, build_css);
87
88 cb();
89}
90
91// SERVE
92function serve(cb) {
93 // Serve files from the root of this project
94 browserSync.init({
95 server: {
96 baseDir: ["examples", "dist"],
97 index: "index.html"
98 }
99 });
100
101 gulp.watch(SRC_JS, build_js);
102 gulp.watch(SRC_CSS, build_css);
103
104 gulp.watch([DEST_JS, DEST_CSS, EXAMPLE_HTML]).on("change", browserSync.reload);
105
106 cb();
107}
108
109// TEST
110function test(cb) {
111 new Server({
112 configFile: __dirname + '/karma.conf.js',
113 singleRun: true
114 }, done).start();
115
116 cb();
117}
118
119// EXPORT methods
120exports.clean = gulp.parallel(clean_js, clean_css);
121exports.build = gulp.parallel(gulp.series(clean_js, lint_js, build_js), gulp.series(clean_css, build_css));
122exports.lint = lint_js;
123exports.watch = watch;
124exports.test = test;
125exports.serve = serve;
126exports.default = serve;