-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.mjs
130 lines (114 loc) · 4.42 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Import required packages.
*/
import gulp from 'gulp'; // Import Gulp for task automation.
import { deleteSync } from 'del'; // Import deleteSync from del package for asset cleanup.
import * as dartSass from 'sass'; // Import Dart Sass for SCSS compilation.
import autoprefixer from 'autoprefixer'; // Import autoprefixer for CSS autoprefixing.
import cleancss from 'gulp-clean-css'; // Import gulp-clean-css for CSS minification.
import clone from 'gulp-clone'; // Import gulp-clone for cloning streams.
import concat from 'gulp-concat'; // Import gulp-concat for concatenating files.
import duplicates from 'postcss-discard-duplicates'; // Import postcss-discard-duplicates for removing duplicate CSS rules.
import gcmq from 'gulp-group-css-media-queries'; // Import gulp-group-css-media-queries for grouping CSS media queries.
import gulpSass from 'gulp-sass'; // Import gulp-sass for SCSS compilation.
import merge from 'merge-stream'; // Import merge-stream for merging streams.
import plumber from 'gulp-plumber'; // Import gulp-plumber for error handling.
import postcss from 'gulp-postcss'; // Import gulp-postcss for PostCSS processing.
import rename from 'gulp-rename'; // Import gulp-rename for file renaming.
import terser from 'gulp-terser'; // Import gulp-terser for JavaScript minification.
const sass = gulpSass( dartSass ); // Initialize Sass compiler.
/**
* Paths to base asset directories. With trailing slashes.
* - `paths.src` - Path to the source files. Default: `src/`
* - `paths.dest` - Path to the build directory. Default: `assets/`
*/
const paths = {
src: 'src/',
dest: 'assets/',
scss: {
src: {
dashboard: [ 'src/scss/dashboard.scss' ],
frontend: [ 'src/scss/frontend.scss' ],
},
dest: 'assets/css',
},
js: {
src: {
ajaxify: [ 'src/js/ajaxify.min.js' ],
dashboard: [ 'src/js/dashboard.js' ],
frontend: [ 'src/js/frontend.js' ],
},
dest: 'assets/js',
},
};
/**
* Build CSS.
*
* @param {Function} done - Callback function to signal completion.
*/
const buildCSS = ( done ) => {
for ( const [ name, path ] of Object.entries( paths.scss.src ) ) {
const baseSource = gulp
.src( path )
.pipe( plumber() )
.pipe( sass( { outputStyle: 'expanded' } ) ) // Compile SCSS to expanded CSS.
.pipe( gcmq() ) // Group CSS media queries.
.pipe( concat( 'merged.css' ) ) // Concatenate CSS files.
.pipe( postcss( [ duplicates(), autoprefixer() ] ) ) // Run PostCSS plugins.
.pipe( cleancss( { format: 'beautify' } ) ) // Beautify the CSS.
.pipe( rename( { basename: name } ) ); // Rename output file.
const minified = baseSource
.pipe( clone() ) // Clone the stream to create a minified version.
.pipe( cleancss() ) // Minify the CSS.
.pipe( rename( { suffix: '.min' } ) ); // Rename minified file.
merge( baseSource, minified ).pipe( gulp.dest( paths.scss.dest ) ); // Merge and output CSS files.
}
done();
};
/**
* Build JavaScript.
*
* @param {Function} done - Callback function to signal completion.
*/
const buildJS = ( done ) => {
for ( const [ name, path ] of Object.entries( paths.js.src ) ) {
const baseSource = gulp
.src( path )
.pipe( plumber() )
.pipe( concat( 'merged.js' ) ) // Concatenate JavaScript files.
.pipe( rename( { basename: name } ) ); // Rename output file.
const minified = baseSource
.pipe( clone() ) // Clone the stream to create a minified version.
.pipe( terser() ) // Minify the JavaScript.
.pipe( rename( { suffix: '.min' } ) ); // Rename minified file.
merge( baseSource, minified ).pipe( gulp.dest( paths.js.dest ) ); // Merge and output JavaScript files.
}
done();
};
/**
* Clean the build directory.
*
* @param {Function} done - Callback function to signal completion.
*/
const cleanAssets = ( done ) => {
deleteSync( paths.dest ); // Delete build directory.
done();
};
/**
* Watch for changes in assets and trigger corresponding build tasks.
*
* @param {Function} done - Callback function to signal completion.
*/
const watchAssets = ( done ) => {
gulp.watch( paths.src + 'scss/**/*.scss', gulp.series( buildCSS ) ); // Watch SCSS files.
gulp.watch( paths.src + 'js/**/*.js', gulp.series( buildJS ) ); // Watch JavaScript files.
done();
};
// Define Gulp tasks.
const css = gulp.series( buildCSS );
const js = gulp.series( buildJS );
const build = gulp.series( cleanAssets, buildCSS, buildJS );
const watcher = gulp.series( watchAssets );
// Export tasks for usage.
export { css, js, build };
export { watcher as watch };