-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.mjs
106 lines (95 loc) · 2.53 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
import gulp from 'gulp';
import babel from 'gulp-babel';
import clean from 'gulp-clean';
import concat from 'gulp-concat';
import rename from 'gulp-rename';
import uglify from 'gulp-uglify';
import replace from 'gulp-replace';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import lightningcss from 'gulp-lightningcss';
import minifyInlineJSON from 'gulp-minify-inline-json';
import gulpHeaderComment from 'gulp-header-comment';
const sass = gulpSass(dartSass);
const destDir = 'public';
const headerComment = `
Generated on <%= moment().format('YYYY-MM-DDTHH:MM:SSZ') %>
Author: <%= pkg.author %>
`
function cleanDist() {
return gulp.src(destDir, {allowEmpty: true})
.pipe(clean())
}
function compileScss() {
return gulp
.src('src/scss/styles.scss')
.pipe(sass().on('error', sass.logError))
.pipe(lightningcss({minify: false}))
.pipe(gulpHeaderComment(headerComment))
.pipe(gulp.dest(destDir))
.pipe(lightningcss({minify: true}))
.pipe(gulpHeaderComment(headerComment))
.pipe(rename('styles.min.css'))
.pipe(gulp.dest(destDir));
}
function processHtml() {
return gulp
.src('public/**/*.html')
.pipe(replace(/(styles.css|scripts.js)/g, function handleReplace(match) {
const parts = match.split('.');
return `${parts[0]}.min.${parts[1]}`;
}))
.pipe(replace('<div class="blog__copy" data-theme="memo"></div>', ''))
.pipe(minifyInlineJSON())
.pipe(gulpHeaderComment(headerComment))
.pipe(gulp.dest(destDir));
}
function transpileMjs() {
return gulp
.src('src/scripts/**.mjs')
.pipe(babel({
presets: ['@babel/env'],
plugins: ['@babel/plugin-syntax-import-attributes'],
}))
.pipe(concat('scripts.js'))
.pipe(gulpHeaderComment(headerComment))
.pipe(gulp.dest(destDir))
.pipe(uglify())
.pipe(rename('scripts.min.js'))
.pipe(gulpHeaderComment(headerComment))
.pipe(gulp.dest(destDir));
}
export const prepare = gulp.series(
cleanDist,
);
export const prepareResources = gulp.series(
compileScss,
transpileMjs,
)
export const build = gulp.series(
processHtml,
prepareResources,
);
export const watchJs = gulp.series(
function watchJsSeries() {
return gulp.watch(
'src/scripts/**.mjs',
gulp.series(transpileMjs)
)
},
);
export const watchScss = gulp.series(
function watchScssSeries() {
return gulp.watch(
'src/scss/**/*.scss',
gulp.series(compileScss)
)
},
);
export const watch = gulp.series(
prepareResources,
gulp.parallel(
watchJs,
watchScss,
),
);