UNPKG

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