generated from merc1er/gulp-tailwind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
159 lines (127 loc) · 3.6 KB
/
gulpfile.js
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
// Imports
const gulp = require('gulp'),
del = require('del'),
rename = require('gulp-rename'),
// Templating
nunjucks = require('gulp-nunjucks-render'),
htmlmin = require('gulp-htmlmin'),
// CSS processing
purgecss = require('gulp-purgecss'),
postcss = require('gulp-postcss'),
tailwindcss = require('tailwindcss'),
autoprefixer = require('autoprefixer'),
cleanCSS = require('gulp-clean-css'),
// JavaScript
webpack = require('webpack-stream'),
// BrowserSync
browserSync = require('browser-sync'),
reload = browserSync.reload;
// TASKS
// Clean previous build
gulp.task('clean', function(done) {
// Deletes all files from dist/
del.sync('dist/', {force: true});
done();
});
// Reload the browser
gulp.task('reload', function(done){
reload();
done();
});
// Nunjucks
gulp.task('nunjucks', function() {
// Get all .html files in pages
return gulp.src('src/**/*.html')
// Render template with nunjucks
.pipe(nunjucks({
path: ['src/templates/']
}))
.pipe(htmlmin({ // minify HTML
collapseWhitespace: true,
removeComments: true
}))
// Output files in dist folder
.pipe(gulp.dest('dist'))
});
// CSS processing
function TailwindExtractor(content) {
// Extract tailwindcss classes
// https://tailwindcss.com/docs/optimizing-for-production#writing-purgeable-html
return content.match(/[^<>"'`\s]*[^<>"'`\s:]/g);
}
// Process CSS to be as small as possible with postCSS and purgeCSS
// This task takes a few seconds - only use it in production
gulp.task('css-prod', function() {
return gulp.src('src/css/style.css')
// postcss
.pipe(postcss([
tailwindcss,
autoprefixer
]))
// purgecss
.pipe(purgecss({
content: ['src/**/*.html', 'src/**/*.njk', 'src/**/*.svg'],
extractors: [{
extractor: TailwindExtractor,
extensions: ['html', 'njk', 'svg']
}]
}))
// Minifies CSS
.pipe(cleanCSS({compatibility: 'ie9'}))
// output files in dist folder
.pipe(gulp.dest('dist/static/css/'));
});
// Use the complete tailwind.min.css file for development only
gulp.task('css-dev', function() {
return gulp.src('src/css/style.css')
// postcss
.pipe(postcss([
tailwindcss
]))
// output files in dist folder
.pipe(gulp.dest('dist/static/css/'));
});
// JavaScript
// Bundle and minify JavaScript
gulp.task('js', () =>
gulp.src('src/js/app.js')
.pipe(webpack({mode: 'production'}))
.pipe(gulp.dest('dist/static/js/'))
);
// Copy all static files
gulp.task('copy-static', function(done){
gulp.src('src/static/**/*').pipe(gulp.dest('dist/static/'));
done();
});
// Start browserSync
gulp.task('serve', function(done){
browserSync({
server: {
baseDir: './dist',
index: "index.html",
serveStaticOptions: {
extensions: ['html']
}
}
});
done();
});
// Watch for changes
gulp.task('watch', function(done){
// Watch HTML pages
gulp.watch('src/**/*', gulp.series('nunjucks', 'reload'));
// Watch CSS files
gulp.watch('src/css/**/*.css', gulp.series('css-dev'));
// Watch JS files
gulp.watch('src/js/**/*', gulp.series('js', 'reload'));
// Watch static files
gulp.watch('src/static/**/*.*', gulp.series('copy-static', 'reload'));
done();
});
// Series
// Default task
gulp.task('default', gulp.series('clean', 'css-dev', 'nunjucks', 'js',
'copy-static', 'serve', 'watch'));
// Deployment task
gulp.task('build', gulp.series('clean', 'css-prod', 'nunjucks', 'js',
'copy-static'));