1 |
|
2 | var 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 |
|
14 | var Server = require('karma').Server;
|
15 | var browserSync = require('browser-sync').create();
|
16 |
|
17 |
|
18 | var SRC_JS = 'src/js/*.js';
|
19 | var SRC_CSS = 'src/css/*.css';
|
20 |
|
21 |
|
22 | var DEST_JS = 'dist/js';
|
23 | var DEST_CSS = 'dist/css';
|
24 |
|
25 |
|
26 | var EXAMPLE_HTML = 'examples/*.html';
|
27 |
|
28 |
|
29 | function 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 |
|
49 | function 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 |
|
62 | function lint_js(cb) {
|
63 | gulp.src(SRC_JS)
|
64 | .pipe(jshint({ "esversion": 8 }))
|
65 | .pipe(jshint.reporter('default'));
|
66 |
|
67 | cb();
|
68 | }
|
69 |
|
70 |
|
71 | function clean_js(cb) {
|
72 | del.sync([DEST_JS]);
|
73 |
|
74 | cb();
|
75 | }
|
76 |
|
77 | function clean_css(cb) {
|
78 | del.sync([DEST_CSS]);
|
79 |
|
80 | cb();
|
81 | }
|
82 |
|
83 |
|
84 | function watch(cb) {
|
85 | gulp.watch(SRC_JS, build_js);
|
86 | gulp.watch(SRC_CSS, build_css);
|
87 |
|
88 | cb();
|
89 | }
|
90 |
|
91 |
|
92 | function serve(cb) {
|
93 |
|
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 |
|
110 | function test(cb) {
|
111 | new Server({
|
112 | configFile: __dirname + '/karma.conf.js',
|
113 | singleRun: true
|
114 | }, done).start();
|
115 |
|
116 | cb();
|
117 | }
|
118 |
|
119 |
|
120 | exports.clean = gulp.parallel(clean_js, clean_css);
|
121 | exports.build = gulp.parallel(gulp.series(clean_js, lint_js, build_js), gulp.series(clean_css, build_css));
|
122 | exports.lint = lint_js;
|
123 | exports.watch = watch;
|
124 | exports.test = test;
|
125 | exports.serve = serve;
|
126 | exports.default = serve;
|