Skip to content

Commit b7f9c58

Browse files
committed
feat(build): switch to gulp for frontend stuff
1 parent 25947fa commit b7f9c58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+10635
-1364
lines changed

Gulpfile.js

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
/**
2+
* Settings
3+
* Turn on/off build features
4+
*/
5+
6+
var settings = {
7+
clean: true,
8+
scripts: true,
9+
hjs: false,
10+
polyfills: false,
11+
styles: true,
12+
svgs: true,
13+
copy: true,
14+
vendor: true
15+
}
16+
17+
/**
18+
* Paths to project folders
19+
*/
20+
21+
var paths = {
22+
input: 'src/main/frontend/',
23+
output: 'src/main/xar-resources/resources/',
24+
scripts: {
25+
input: 'src/main/frontend/javascript/*',
26+
polyfills: '.polyfill.js',
27+
output: 'src/main/xar-resources/resources/scripts/'
28+
},
29+
styles: {
30+
input: 'src/main/frontend/css/**',
31+
output: 'src/main/xar-resources/resources/styles/'
32+
},
33+
svgs: {
34+
input: 'src/main/frontend/img/*.svg',
35+
output: 'src/main/xar-resources/resources/images/'
36+
},
37+
copy: {
38+
input: 'src/main/frontend/copy/**',
39+
output: 'src/main/xar-resources/resources/'
40+
},
41+
vendor: {
42+
input: 'node_modules/',
43+
output: 'src/main/xar-resources/resources/'
44+
},
45+
fonts: {
46+
output: 'src/main/xar-resources/resources/fonts/',
47+
}
48+
}
49+
50+
/**
51+
* Template for banner to add to file headers
52+
*/
53+
54+
var banner = {
55+
full: '/*!\n' +
56+
' * <%= package.name %> v<%= package.version %>\n' +
57+
' * <%= package.description %>\n' +
58+
' * (c) ' + new Date().getFullYear() + ' <%= package.author.name %>\n' +
59+
' * <%= package.license %> License\n' +
60+
' * <%= package.repository.url %>\n' +
61+
' */\n\n',
62+
min: '/*!' +
63+
' <%= package.name %> v<%= package.version %>' +
64+
' | (c) ' + new Date().getFullYear() + ' <%= package.author.name %>' +
65+
' | <%= package.license %> License' +
66+
' | <%= package.repository.url %>' +
67+
' */\n'
68+
}
69+
70+
/**
71+
* Gulp Packages
72+
*/
73+
74+
// General
75+
var {
76+
gulp,
77+
src,
78+
dest,
79+
series,
80+
parallel
81+
} = require('gulp')
82+
var del = require('del')
83+
var flatmap = require('gulp-flatmap')
84+
var lazypipe = require('lazypipe')
85+
var rename = require('gulp-rename')
86+
var header = require('gulp-header')
87+
var pkg = require('./package.json')
88+
89+
// Scripts
90+
var standard = require('gulp-standard')
91+
var concat = require('gulp-concat')
92+
var uglify = require('gulp-uglify')
93+
var optimizejs = require('gulp-optimize-js')
94+
95+
// Styles
96+
var sass = require('gulp-sass')
97+
var prefix = require('gulp-autoprefixer')
98+
var minify = require('gulp-cssnano')
99+
100+
// SVGs
101+
var svgmin = require('gulp-svgmin')
102+
103+
/**
104+
* Gulp Tasks
105+
*/
106+
107+
// Remove pre-existing content from output folders
108+
var cleanDist = function(done) {
109+
// Make sure this feature is activated before running
110+
if (!settings.clean) return done()
111+
112+
// Clean the dist folder
113+
del.sync([
114+
paths.output
115+
])
116+
117+
// Signal completion
118+
return done()
119+
}
120+
121+
// Repeated JavaScript tasks
122+
var jsTasks = lazypipe()
123+
.pipe(header, banner.full, {
124+
package: pkg
125+
})
126+
.pipe(optimizejs)
127+
.pipe(dest, paths.scripts.output)
128+
.pipe(rename, {
129+
suffix: '.min'
130+
})
131+
.pipe(uglify)
132+
.pipe(optimizejs)
133+
.pipe(header, banner.min, {
134+
package: pkg
135+
})
136+
.pipe(dest, paths.scripts.output)
137+
138+
// Lint, minify, and concatenate scripts
139+
var buildScripts = function(done) {
140+
// Make sure this feature is activated before running
141+
if (!settings.scripts) return done()
142+
143+
// Run tasks on script files
144+
src(paths.scripts.input)
145+
.pipe(flatmap(function(stream, file) {
146+
// If the file is a directory
147+
if (file.isDirectory()) {
148+
// Setup a suffix variable
149+
var suffix = ''
150+
151+
// If separate polyfill files enabled
152+
if (settings.polyfills) {
153+
// Update the suffix
154+
suffix = '.polyfills'
155+
156+
// Grab files that aren't polyfills, concatenate them, and process them
157+
src([file.path + '/*.js', '!' + file.path + '/*' + paths.scripts.polyfills])
158+
.pipe(concat(file.relative + '.js'))
159+
.pipe(jsTasks())
160+
}
161+
162+
// Grab all files and concatenate them
163+
// If separate polyfills enabled, this will have .polyfills in the filename
164+
src(file.path + '/*.js')
165+
.pipe(concat(file.relative + suffix + '.js'))
166+
.pipe(jsTasks())
167+
168+
return stream
169+
}
170+
171+
// Otherwise, process the file
172+
return stream.pipe(jsTasks())
173+
}))
174+
175+
// Signal completion
176+
done()
177+
}
178+
179+
// Lint scripts
180+
var lintScripts = function(done) {
181+
// Make sure this feature is activated before running
182+
if (!settings.scripts) return done()
183+
184+
// Lint scripts
185+
src(paths.scripts.input)
186+
.pipe(standard({
187+
fix: true
188+
}))
189+
.pipe(standard.reporter('default'))
190+
191+
// Signal completion
192+
done()
193+
}
194+
195+
// Process, lint, and minify Sass files
196+
var buildStyles = function(done) {
197+
// Make sure this feature is activated before running
198+
if (!settings.styles) return done()
199+
200+
// Run tasks on all Sass files
201+
src(paths.styles.input)
202+
.pipe(sass({
203+
outputStyle: 'expanded',
204+
sourceComments: true
205+
}))
206+
.pipe(prefix({
207+
browsers: ['last 2 version', '> 0.25%'],
208+
cascade: true,
209+
remove: true
210+
}))
211+
.pipe(header(banner.full, {
212+
package: pkg
213+
}))
214+
.pipe(dest(paths.styles.output))
215+
.pipe(rename({
216+
suffix: '.min'
217+
}))
218+
.pipe(minify({
219+
discardComments: {
220+
removeAll: true
221+
}
222+
}))
223+
.pipe(header(banner.min, {
224+
package: pkg
225+
}))
226+
.pipe(dest(paths.styles.output))
227+
228+
// Signal completion
229+
done()
230+
}
231+
232+
// Optimize SVG files
233+
var buildSVGs = function(done) {
234+
// Make sure this feature is activated before running
235+
if (!settings.svgs) return done()
236+
237+
// Optimize SVG files
238+
src(paths.svgs.input)
239+
.pipe(svgmin())
240+
.pipe(dest(paths.svgs.output))
241+
242+
// Signal completion
243+
done()
244+
}
245+
246+
// Copy third-party dependencies from node_modules into resources
247+
var vendorFiles = function(done) {
248+
// Make sure this feature is activated before running
249+
if (!settings.vendor) return done()
250+
251+
// TODO ensure each declared third-parrty dep has a corresponding command below
252+
// TODO modernizr@2 needs refactor via npm or gulp-modernizr
253+
var deps = pkg.dependencies.length
254+
255+
256+
// copy vendor scripts
257+
src(['node_modules/bootstrap/dist/js/bootstrap.min.*', 'node_modules/jquery/dist/jquery.min.*'])
258+
.pipe(dest(paths.scripts.output))
259+
260+
// copy vendor Styles
261+
src(['node_modules/bootstrap/dist/css/bootstrap.min.*', 'node_modules/highlight.js/styles/atom-one-dark.css'])
262+
.pipe(dest(paths.styles.output))
263+
264+
// TODO copy and install vendor fonts
265+
// Signal completion
266+
done()
267+
}
268+
269+
270+
// Copy static files into output folder
271+
var copyFiles = function(done) {
272+
// Make sure this feature is activated before running
273+
if (!settings.copy) return done()
274+
275+
// Copy static files
276+
src(paths.copy.input)
277+
.pipe(dest(paths.copy.output))
278+
279+
// Signal completion
280+
done()
281+
}
282+
283+
// Build and copy highlight.js
284+
var buildPack = function(done) {
285+
// Make sure this feature is activated before running
286+
if (!settings.hjs) return done()
287+
288+
// build highlight pack
289+
// see https://highlightjs.readthedocs.io/en/latest/building-testing.html
290+
// TODO currently building is bugged
291+
let command = 'cd node_modules/highlight.js'
292+
+ ' && npm install'
293+
+ ' && node tools/build :common xquery'
294+
295+
exec(command, (err, stdout, stderr)=> {
296+
console.log(stderr)
297+
console.log(stdout)
298+
299+
callback(err)
300+
})
301+
302+
src('node_modules/highlight.js/build/*pack.js')
303+
.pipe(dest(paths.scripts.output))
304+
305+
// Signal completion
306+
done()
307+
}
308+
309+
/**
310+
* Export Tasks
311+
*/
312+
313+
// Default task
314+
// gulp
315+
exports.default = series(
316+
cleanDist,
317+
vendorFiles,
318+
parallel(
319+
buildScripts,
320+
lintScripts,
321+
buildStyles,
322+
buildSVGs,
323+
copyFiles,
324+
buildPack
325+
)
326+
)

0 commit comments

Comments
 (0)