Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·144 lines (129 loc) · 3.12 KB
/
Copy pathgulpfile.js
File metadata and controls
executable file
·144 lines (129 loc) · 3.12 KB
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
import { src, dest, parallel, series, watch } from 'gulp';
import postcss from 'gulp-postcss';
import postcssGlobImport from 'postcss-import-ext-glob';
import postcssImport from 'postcss-import';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import sourcemaps from 'gulp-sourcemaps';
import concat from 'gulp-concat';
import { deleteAsync } from 'del';
import fs from 'fs';
import path from 'path';
// Rollup plugins
import { rollup } from 'rollup';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
// File path variables
const paths = {
styles: {
src: 'src/css/**/*.css',
dest: 'web/assets/css/'
},
scripts: {
src: 'src/js/main.js',
dest: 'web/assets/js/'
},
vendor: {
src: 'src/js/vendor/**/*.js',
dest: 'web/assets/js/vendor/'
},
images: {
src: 'src/img/**/*.{jpg,jpeg,png,svg,gif,webp}',
dest: 'web/assets/images/'
},
fonts: {
src: 'src/fonts/**/*',
dest: 'web/assets/fonts/'
}
};
// Clean output folder
function clean() {
return deleteAsync(['web/assets']);
}
// Process CSS
function styles() {
return src('src/css/global.css')
.pipe(sourcemaps.init())
.pipe(postcss([
postcssGlobImport({ allowEmpty: true }),
postcssImport(),
autoprefixer(),
cssnano()
]))
.pipe(concat('main.css'))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.styles.dest));
}
// Process JavaScript
async function scripts() {
// Ensure destination directory exists
if (!fs.existsSync(paths.scripts.dest)) {
fs.mkdirSync(paths.scripts.dest, { recursive: true });
}
// Bundle with Rollup
const bundle = await rollup({
input: paths.scripts.src,
plugins: [
resolve({ browser: true }),
commonjs(),
babel({
babelHelpers: 'bundled',
presets: ['@babel/preset-env'],
exclude: 'node_modules/**'
}),
terser()
]
});
// Generate output
await bundle.write({
file: path.join(paths.scripts.dest, 'bundle.js'),
format: 'iife',
sourcemap: true,
name: 'bundle'
});
// Close the bundle
await bundle.close();
}
// Process vendor JavaScript
function vendor() {
return src(paths.vendor.src)
.pipe(dest(paths.vendor.dest));
}
// Optimise images
function images() {
return src(paths.images.src, { encoding: false })
.pipe(dest(paths.images.dest));
}
// Copy fonts
function fonts() {
return src(paths.fonts.src, { encoding: false })
.pipe(dest(paths.fonts.dest));
}
// Define watch task
function watchFiles(cb) {
watch(paths.styles.src, styles);
watch(paths.scripts.src, scripts);
watch(paths.images.src, images);
watch(paths.fonts.src, fonts);
cb();
}
// Export tasks
export { clean };
export { styles };
export { scripts };
export { vendor };
export { images };
export { fonts };
export { watchFiles };
// Create default task
export default series(
clean,
parallel(styles, scripts, vendor, images, fonts)
);
// Build task
export const build = series(
clean,
parallel(styles, scripts, vendor, images, fonts)
);