-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.mjs
179 lines (159 loc) · 6.02 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* 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 flatten from 'gulp-flatten'; // Import gulp-flatten for flattening file structures.
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 imagemin from 'gulp-imagemin'; // Import gulp-imagemin for image optimization.
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: {
admin: [ 'src/scss/admin.scss' ], // Source SCSS file(s) for admin styles.
frontend: [ 'src/scss/frontend.scss' ], // Source SCSS file(s) for frontend styles.
},
dest: 'assets/css', // Destination directory for compiled CSS.
},
js: {
src: {
admin: [ 'src/js/admin.js' ], // Source JavaScript file(s) for admin functionality.
frontend: [ 'src/js/frontend.js' ], // Source JavaScript file(s) for frontend scripts.
},
dest: 'assets/js', // Destination directory for compiled JavaScript.
},
images: {
src: 'src/images/**/*', // Source image files for optimization.
dest: 'assets/images', // Destination directory for optimized images.
},
fonts: {
src: 'src/fonts/**/*', // Source font files.
dest: 'assets/fonts', // Destination directory for fonts.
},
};
/**
* 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();
};
/**
* Build Fonts.
*
* @param {Function} done - Callback function to signal completion.
*/
const buildFonts = ( done ) => {
gulp.src( paths.fonts.src, { encoding: false } )
.pipe( flatten() ) // Flatten font files to avoid nested directories.
.pipe( gulp.dest( paths.fonts.dest ) ); // Output font files.
done();
};
/**
* Build Images.
*
* @param {Function} done - Callback function to signal completion.
*/
const buildImages = ( done ) => {
gulp.src( paths.images.src, { encoding: false } )
.pipe(
imagemin( {
progressive: true,
interlaced: true,
svgoPlugins: [ { removeUnknownsAndDefaults: false } ],
} )
) // Optimize images.
.pipe( gulp.dest( paths.images.dest ) ); // Output optimized images.
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.
gulp.watch( paths.src + 'fonts/**/*', gulp.series( buildFonts ) ); // Watch font files.
gulp.watch( paths.src + 'images/**/*', gulp.series( buildImages ) ); // Watch image files.
done();
};
// Define Gulp tasks.
const css = gulp.series( buildCSS );
const js = gulp.series( buildJS );
const build = gulp.series(
cleanAssets,
buildCSS,
buildJS,
buildFonts,
buildImages
);
const watcher = gulp.series( watchAssets );
// Export tasks for usage.
export { css, js, build };
export { watcher as watch };