-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
190 lines (174 loc) · 7.64 KB
/
gulpfile.js
File metadata and controls
190 lines (174 loc) · 7.64 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
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
180
181
182
183
184
185
186
187
188
189
190
"use strict";
// Load plugins
const cleanCSS = require("gulp-clean-css");
const del = require("del");
const gulp = require("gulp");
const gulpif = require("gulp-if");
const header = require("gulp-header");
const merge = require("merge-stream");
const plumber = require("gulp-plumber");
const rename = require("gulp-rename");
const replace = require("gulp-replace");
const rev = require('gulp-rev');
const revdel = require('gulp-rev-delete-original');
const revRewrite = require('gulp-rev-rewrite');
const terser = require("gulp-terser");
const fs = require("fs");
// Load package.json for banner
const pkg = require('./package.json');
// Set the banner content
const banner = '/*!\n * <%= pkg.title %> v<%= pkg.version %> (<%= pkg.homepage %>)\n */\n';
function cleanVendor() {
return del(["public/vendor/"]);
}
function cleanDist() {
return del(["dist/"]);
}
// Bring third party dependencies from node_modules into the vendor directory.
function createVendor() {
// Bootstrap
let bootstrapJS = gulp.src(['./node_modules/bootstrap/dist/js/bootstrap.*', '!./node_modules/bootstrap/dist/js/bootstrap.bundle.*'])
.pipe(gulp.dest('./public/vendor/bootstrap/js'));
let bootstrapCSS = gulp.src('./node_modules/bootstrap/dist/css/bootstrap.*')
.pipe(gulp.dest('./public/vendor/bootstrap/css'));
// Bootstrap-table
let bootstrapTableJS = gulp.src('./node_modules/bootstrap-table/dist/bootstrap-table.*js')
.pipe(gulp.dest('./public/vendor/bootstrap-table/js'));
let bootstrapTableCSS = gulp.src('./node_modules/bootstrap-table/dist/bootstrap-table.*css')
.pipe(gulp.dest('./public/vendor/bootstrap-table/css'));
// Bootstrap-table filer control
let bootstrapTableFilterJS = gulp.src('./node_modules/bootstrap-table/dist/extensions/filter-control/bootstrap-table-filter-control*.js')
.pipe(gulp.dest('./public/vendor/bootstrap-table/js'));
let bootstrapTableFilerCSS = gulp.src('./node_modules/bootstrap-table/dist/extensions/filter-control/bootstrap-table-filter-control*.css')
.pipe(gulp.dest('./public/vendor/bootstrap-table/css'));
// Font Awesome
let fontAwesomeCSS = gulp.src('./node_modules/@fortawesome/fontawesome-free/css/all.*')
.pipe(gulp.dest('./public/vendor/fontawesome-free/css'));
let fontAwesome = gulp.src('./node_modules/@fortawesome/fontawesome-free/webfonts/*.*')
.pipe(gulp.dest('./public/vendor/fontawesome-free/webfonts'));
// jQuery
let jquery = gulp.src(['./node_modules/jquery/dist/jquery.*', '!./node_modules/jquery/dist/jquery.slim.*'])
.pipe(gulp.dest('./public/vendor/jquery'));
// Popper
let popper = gulp.src('./node_modules/popper.js/dist/umd/popper.*')
.pipe(gulp.dest('./public/vendor/popper'));
return merge(bootstrapJS, bootstrapCSS, bootstrapTableJS, bootstrapTableCSS, bootstrapTableFilterJS, bootstrapTableFilerCSS,
fontAwesome, fontAwesomeCSS, jquery, popper);
}
function copyModuleDist() {
// Bootstrap
let bootstrapJS = gulp.src('./public/vendor/bootstrap/js/*.min.js')
.pipe(gulp.dest('./dist/public/vendor/bootstrap/js'));
let bootstrapCSS = gulp.src('./public/vendor/bootstrap/css/*.min.css')
.pipe(gulp.dest('./dist/public/vendor/bootstrap/css'));
// Bootstrap-table & filter control
let bootstrapTableJS = gulp.src('./public/vendor/bootstrap-table/js/*.min.js')
.pipe(gulp.dest('./dist/public/vendor/bootstrap-table/js'));
let bootstrapTableCSS = gulp.src('./public/vendor/bootstrap-table/css/*.min.css')
.pipe(gulp.dest('./dist/public/vendor/bootstrap-table/css'));
// Font Awesome
let fontAwesomeCSS = gulp.src('./public/vendor/fontawesome-free/css/*.min.css')
.pipe(gulp.dest('./dist/public/vendor/fontawesome-free/css'));
let fontAwesome = gulp.src('./public/vendor/fontawesome-free/webfonts/*.*')
.pipe(gulp.dest('./dist/public/vendor/fontawesome-free/webfonts'));
// jQuery
let jquery = gulp.src('./public/vendor/jquery/*.min.*')
.pipe(gulp.dest('./dist/public/vendor/jquery'));
// Popper
let popper = gulp.src('./public/vendor/popper/*.min.*')
.pipe(gulp.dest('./dist/public/vendor/popper'));
return merge(bootstrapJS, bootstrapCSS, bootstrapTableJS, bootstrapTableCSS, fontAwesome, fontAwesomeCSS, jquery, popper);
}
function copyStaticDist() {
// General webroot files
let webRoot = gulp.src(['./public/*.!(html)*', './public/**/vendor/*'])
.pipe(gulp.dest('./dist/public'));
// Images
let images = gulp.src('./public/img/*.*')
.pipe(gulp.dest('./dist/public/img'));
// Static Data
let json = gulp.src('./public/json/*.json')
.pipe(gulp.dest('./dist/public/json'));
// Static HTML
let html = gulp.src('./public/**/*.html')
.pipe(gulpif(staticBuild, replace('"/template', '"https://jurassic-john.site/template')))
.pipe(gulpif(staticBuild, replace('"/react', '"https://jurassic-john.site/react')))
.pipe(gulpif(!staticBuild, replace(/\.\.?\//g, '/')))
.pipe(gulpif(!staticBuild, replace('.html', '')))
.pipe(replace('.min.js', '.js'))
.pipe(replace('.css', '.min.css'))
.pipe(replace('.js', '.min.js'))
.pipe(gulp.dest('dist/public'));
return merge(webRoot, images, json, html);
}
function copyAppDist() {
// Application JS
let appJS = gulp.src('./src/main/webapp/*.js')
.pipe(gulp.dest('dist/app'));
// Application Templates
let appTemplates = gulp.src('./src/main/webapp/templates/*.twig')
.pipe(replace('.min.js', '.js'))
.pipe(replace('.css', '.min.css'))
.pipe(replace('.js', '.min.js'))
.pipe(gulp.dest('dist/app/templates'));
return merge(appJS, appTemplates);
}
// Minify CSS task
function mincss() {
return gulp.src("public/css/*.css")
.pipe(plumber())
.pipe(header(banner, {
pkg: pkg
}))
.pipe(rename({
suffix: ".min"
}))
.pipe(cleanCSS())
.pipe(gulp.dest("dist/public/css"));
}
// Minify JS task
function minjs() {
return gulp.src('./public/js/*.js')
.pipe(terser())
.pipe(header(banner, {
pkg: pkg
}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./dist/public/js'));
}
// Cache busting.
function revision() {
return gulp.src('dist/public/**/*.{css,js}')
.pipe(rev())
.pipe(revdel())
.pipe(gulp.dest('dist/public'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist/public'));
}
// Use cache busting.
function rewrite() {
const manifest = fs.readFileSync('dist/public/rev-manifest.json');
let staticHTML = gulp.src('dist/public/**/*.html')
.pipe(revRewrite({ manifest }))
.pipe(gulp.dest('dist/public'));
let staticTwig = gulp.src('dist/app/**/*.twig')
.pipe(revRewrite({ manifest }))
.pipe(gulp.dest('dist/app'));
return merge(staticHTML, staticTwig);
}
let staticBuild = false;
// Define complex tasks
const build = gulp.series(createVendor, gulp.parallel(copyModuleDist, copyStaticDist, copyAppDist), gulp.parallel(mincss, minjs), revision, rewrite);
const rebuild = gulp.series(gulp.parallel(cleanVendor, cleanDist), build);
const ghPagesBuild = gulp.series(function (cb) { staticBuild = true; cb(); }, build);
// Document tasks
build.description = "Build a distribution ready version of the static and template files.";
rebuild.description = "Clear down vendor and distribution then run a build.";
ghPagesBuild.description = "Build for GitHub pages including hardcoded links to dynamic site.";
// Export tasks
exports.build = build;
exports.rebuild = rebuild;
exports.ghPagesBuild = ghPagesBuild;
exports.default = rebuild;