-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
110 lines (98 loc) · 3.06 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const gulp = require('gulp'), // Gulp
concat = require('gulp-concat'), // Concat multiple files
gulpif = require('gulp-if'), // If statements
clean = require('gulp-clean'), // Remove files and folders
sourcemaps = require('gulp-sourcemaps'), // Inline sourcemaps in dev mode
autoprefixer = require('gulp-autoprefixer'), // Add browser prefixes
sass = require('gulp-sass')(require('sass')),
uglify = require('gulp-uglify-es').default, // JS minify (ES6 Supported)
babel = require('gulp-babel'),
browserSync = require('browser-sync').create(); // Synchronised browser testing
// File Paths
const jsPaths = 'app/assets/js/',
jsLibPaths = 'app/assets/js/lib/',
scssPath = 'app/assets/scss/',
cssPath = 'app/assets/css/',
htmlPath = 'app/**/*.html',
mapsPath = 'app/assets/**/*/*.map';
// JS Paths
const paths = {
scriptsLib: ['node_modules/jquery/dist/jquery.min.js'],
scripts: [jsPaths + 'core.js'],
scss: [scssPath + 'core.scss']
};
// Set env value
var env = process.env.NODE_ENV || 'dev';
//------------------------------------------------------------
// = TASKS
//------------------------------------------------------------
// BrowserSync
function browserSyncInit(done) {
browserSync.init({
server: {
baseDir: './app/'
},
port: 3000
});
done();
}
// BrowserSync Reload
function browserSyncReload() {
browserSync.reload();
}
// Clean
function cleanMap(done) {
gulp
.src(mapsPath, {
read: false
})
.pipe(clean());
done();
}
// Scripts
function scripts(done) {
gulp
.src(paths.scripts)
.pipe(gulpif(env === 'dev', sourcemaps.init({ largeFile: true })))
.pipe(babel({presets: ['@babel/preset-env']}))
.pipe(gulpif(env === 'prod', uglify()))
.pipe(concat('core.min.js'))
.pipe(gulpif(env === 'dev', sourcemaps.write('/')))
.pipe(gulp.dest(jsPaths))
.pipe(browserSync.stream());
done();
}
// SCSS
function scss(done) {
gulp
.src(paths.scss)
.pipe(gulpif(env === 'dev', sourcemaps.init({ largeFile: true })))
.pipe(
gulpif(
env === 'prod',
sass({ outputStyle: 'compressed' }).on('error', sass.logError)
)
)
.pipe(gulpif(env === 'dev', sass().on('error', sass.logError)))
.pipe(autoprefixer())
.pipe(gulpif(env === 'dev', sourcemaps.write('/')))
.pipe(gulp.dest(cssPath))
.pipe(browserSync.stream());
done();
}
//------------------------------------------------------------
// = RUN TASKS
//------------------------------------------------------------
function watchFiles(done) {
gulp.watch(paths.scripts, gulp.series(scripts));
gulp.watch(scssPath + '**/*.scss', gulp.series(scss));
gulp.watch(htmlPath).on('change', browserSyncReload);
done();
}
const watch = gulp.series(browserSyncInit, watchFiles);
const build = gulp.series(gulp.parallel(scripts, scss), cleanMap);
const develop = gulp.series(gulp.parallel(scripts, scss), watch);
exports.cleanmap = cleanMap;
exports.watch = watch;
exports.build = build;
exports.default = develop;