UNPKG

4.1 kBJavaScriptView Raw
1var gulp = require('gulp'),
2 jshint = require('gulp-jshint'),
3 rename = require('gulp-rename'),
4 uglify = require('gulp-uglify'),
5 saveLicense = require('uglify-save-license'),
6 babel = require("gulp-babel"),
7 postcss = require('gulp-postcss'),
8 cleanCSS = require('gulp-clean-css'),
9 cssbeautify = require('gulp-cssbeautify'),
10 autoprefixer = require('autoprefixer'),
11 sass = require('gulp-sass'),
12 del = require('del');
13
14sass.compiler = require('node-sass');
15var Server = require('karma').Server;
16var browserSync = require('browser-sync').create();
17var reload = browserSync.reload;
18
19// Source files
20var SRC_JS = 'src/js/*.js';
21var SRC_CSS = 'src/css/*.css';
22var SRC_SCSS = 'src/scss/*.scss';
23
24// Destination folders
25var DEST_JS = 'dist/js';
26var DEST_CSS = 'dist/css';
27var DEST_SCSS = 'src/css';
28
29
30// BUILD JS
31const build_js = gulp.series(clean_js, lint_js, function(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
51const build_css = gulp.series(clean_css, function(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
64const build_scss = gulp.series(function(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 }, build_css);
71
72// BUILD ALL
73const build = gulp.parallel(build_js, build_scss);
74build.description = 'Build the files';
75
76
77// LINT
78const lint = gulp.parallel(lint_js);
79lint.description = 'Link js files';
80
81function lint_js(cb) {
82 gulp.src(SRC_JS)
83 .pipe(jshint({ "esversion": 8 }))
84 .pipe(jshint.reporter('default'));
85
86 cb();
87}
88
89
90// CLEAN
91const clean = gulp.parallel(clean_js, clean_css);
92clean.description = 'Clean the files'
93
94function clean_js(cb) {
95 del.sync([DEST_JS]);
96
97 cb();
98}
99
100function clean_css(cb) {
101 del.sync([DEST_CSS]);
102
103 cb();
104}
105
106
107// WATCH
108const watch = gulp.parallel(watch_all);
109watch.description = 'Watch for changes to all source';
110
111function watch_all(cb) {
112 gulp.watch(SRC_JS, build_js);
113 gulp.watch(SRC_CSS, build_css);
114 gulp.watch(SRC_SCSS, build_scss);
115
116 cb();
117}
118
119// SERVE
120const serve = gulp.parallel(serve_all);
121
122function serve_all(cb) {
123 // Serve files from the root of this project
124 browserSync.init({
125 server: {
126 baseDir: ["examples", "dist"],
127 index: "index.html"
128 }
129 });
130
131 gulp.watch(SRC_JS, build_js).on("change", reload);
132 gulp.watch(SRC_CSS, build_css).on("change", reload);
133 gulp.watch(SRC_SCSS, build_scss).on("change", reload);
134
135 cb();
136}
137
138// TEST
139function test(cb) {
140 new Server({
141 configFile: __dirname + '/karma.conf.js',
142 singleRun: true
143 }, done).start();
144
145 cb();
146}
147
148
149// EXPORT methods
150exports.clean = clean;
151exports.build = build;
152exports.lint = lint;
153exports.watch = watch;
154exports.test = test;
155exports.serve = serve;
156exports.default = exports.serve;