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 | sass = require('gulp-sass'),
|
13 | del = require('del');
|
14 |
|
15 | sass.compiler = require('node-sass');
|
16 | var Server = require('karma').Server;
|
17 | var browserSync = require('browser-sync').create();
|
18 | var reload = browserSync.reload;
|
19 |
|
20 |
|
21 | var SRC_JS = 'src/js/*.js';
|
22 | var SRC_CSS = 'src/css/*.css';
|
23 | var SRC_SCSS = 'src/scss/*.scss';
|
24 |
|
25 |
|
26 | var DEST_JS = 'dist/js';
|
27 | var DEST_CSS = 'dist/css';
|
28 | var DEST_SCSS = 'src/css';
|
29 |
|
30 |
|
31 | function 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 |
|
51 | function 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 |
|
64 | function 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 |
|
73 | function lint_js(cb) {
|
74 | gulp.src(SRC_JS)
|
75 | .pipe(jshint({ "esversion": 8 }))
|
76 | .pipe(jshint.reporter('default'));
|
77 |
|
78 | cb();
|
79 | }
|
80 |
|
81 |
|
82 | function clean_js(cb) {
|
83 | del.sync([DEST_JS]);
|
84 |
|
85 | cb();
|
86 | }
|
87 |
|
88 | function clean_css(cb) {
|
89 | del.sync([DEST_CSS]);
|
90 |
|
91 | cb();
|
92 | }
|
93 |
|
94 |
|
95 | function 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 |
|
104 | function serve(cb) {
|
105 |
|
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 |
|
121 | function test(cb) {
|
122 | new Server({
|
123 | configFile: __dirname + '/karma.conf.js',
|
124 | singleRun: true
|
125 | }, done).start();
|
126 |
|
127 | cb();
|
128 | }
|
129 |
|
130 |
|
131 | exports.clean = gulp.parallel(clean_js, clean_css);
|
132 | exports.build = gulp.parallel(gulp.series(clean_js, lint_js, build_js), gulp.series(build_scss, build_css, clean_css));
|
133 | exports.lint = lint_js;
|
134 | exports.watch = watch;
|
135 | exports.test = test;
|
136 | exports.serve = serve;
|
137 | exports.default = serve;
|