1 | var 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 |
|
14 | sass.compiler = require('node-sass');
|
15 | var Server = require('karma').Server;
|
16 | var browserSync = require('browser-sync').create();
|
17 | var reload = browserSync.reload;
|
18 |
|
19 |
|
20 | var SRC_JS = 'src/js/*.js';
|
21 | var SRC_CSS = 'src/css/*.css';
|
22 | var SRC_SCSS = 'src/scss/*.scss';
|
23 |
|
24 |
|
25 | var DEST_JS = 'dist/js';
|
26 | var DEST_CSS = 'dist/css';
|
27 | var DEST_SCSS = 'src/css';
|
28 |
|
29 |
|
30 |
|
31 | const 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 |
|
51 | const 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 |
|
64 | const 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 |
|
73 | const build = gulp.parallel(build_js, build_scss);
|
74 | build.description = 'Build the files';
|
75 |
|
76 |
|
77 |
|
78 | const lint = gulp.parallel(lint_js);
|
79 | lint.description = 'Link js files';
|
80 |
|
81 | function 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 |
|
91 | const clean = gulp.parallel(clean_js, clean_css);
|
92 | clean.description = 'Clean the files'
|
93 |
|
94 | function clean_js(cb) {
|
95 | del.sync([DEST_JS]);
|
96 |
|
97 | cb();
|
98 | }
|
99 |
|
100 | function clean_css(cb) {
|
101 | del.sync([DEST_CSS]);
|
102 |
|
103 | cb();
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 | const watch = gulp.parallel(watch_all);
|
109 | watch.description = 'Watch for changes to all source';
|
110 |
|
111 | function 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 |
|
120 | const serve = gulp.parallel(serve_all);
|
121 |
|
122 | function serve_all(cb) {
|
123 |
|
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 |
|
139 | function test(cb) {
|
140 | new Server({
|
141 | configFile: __dirname + '/karma.conf.js',
|
142 | singleRun: true
|
143 | }, done).start();
|
144 |
|
145 | cb();
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | exports.clean = clean;
|
151 | exports.build = build;
|
152 | exports.lint = lint;
|
153 | exports.watch = watch;
|
154 | exports.test = test;
|
155 | exports.serve = serve;
|
156 | exports.default = exports.serve;
|