-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.mjs
69 lines (59 loc) · 2.16 KB
/
gulpfile.mjs
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
// gulpfile.mjs
// This file defines the SCSS and JS file compilation workflow when using gulp
// and reloads browsers using browsersync on HTML, CSS, and JS Changes
import browserSync from 'browser-sync';
import gulp from 'gulp';
import autoprefixer from 'gulp-autoprefixer';
import cleanCSS from 'gulp-clean-css';
import gulpSass from 'gulp-sass';
import terser from 'gulp-terser';
import gulpTypescript from 'gulp-typescript';
import * as sass from 'sass';
const sassCompiler = gulpSass(sass);
const browserSyncServer = browserSync.create();
// Define file paths
const paths = {
scss: './scss/**/*.scss',
css: './css',
js: './js/main.js',
jsOut: './js',
compiledJS: './minifiedJS',
ts: './ts/**/*.ts',
};
// Compile SCSS to CSS
const compileSCSS = () => {
return gulp
.src(paths.scss)
.pipe(sassCompiler().on('error', sassCompiler.logError)) // Compile SCSS and handle errors
.pipe(autoprefixer()) // Add vendor prefixes
.pipe(cleanCSS()) // Minify CSS
.pipe(gulp.dest(paths.css)) // Output CSS
.pipe(browserSyncServer.stream()); // Stream changes to browser
};
const tsProject = gulpTypescript.createProject('tsconfig.json');
const compileTS = () => {
return tsProject
.src()
.pipe(tsProject())
.pipe(gulp.dest(paths.jsOut)); // Output compiled JS to the specified directory
};
const minifyJS = () => {
return gulp
.src(`${paths.jsOut}/**/*.js`) // Use the compiled JS files as the source
.pipe(terser()) // Minify the JS
.pipe(gulp.dest(paths.compiledJS)); // Output minified JS to the specified directory
};
// Watch files for changes
const watchFiles = () => {
browserSyncServer.init({
server: {
baseDir: './', // Base directory for BrowserSync
},
});
gulp.watch(paths.scss, compileSCSS); // Watch SCSS files
gulp.watch('./*.html').on('change', browserSyncServer.reload); // Reload on HTML changes
gulp.watch(paths.ts, gulp.series(compileTS, minifyJS)); // Watch TS files, compile and minify
gulp.watch(`${paths.compiledJS}/**/*.js`).on('change', browserSyncServer.reload); // Reload on JS changes
};
// Export tasks
export default gulp.series(compileSCSS, minifyJS, watchFiles);