|
| 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/sass/*.{scss,sass}', |
| 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 | +var sourcemaps = require('gulp-sourcemaps') |
| 100 | + |
| 101 | +// SVGs |
| 102 | +var svgmin = require('gulp-svgmin') |
| 103 | + |
| 104 | +/** |
| 105 | + * Gulp Tasks |
| 106 | + */ |
| 107 | + |
| 108 | +// Remove pre-existing content from output folders |
| 109 | +var cleanDist = function(done) { |
| 110 | + // Make sure this feature is activated before running |
| 111 | + if (!settings.clean) return done() |
| 112 | + |
| 113 | + // Clean the dist folder |
| 114 | + del.sync([ |
| 115 | + paths.output |
| 116 | + ]) |
| 117 | + |
| 118 | + // Signal completion |
| 119 | + return done() |
| 120 | +} |
| 121 | + |
| 122 | +// Repeated JavaScript tasks |
| 123 | +var jsTasks = lazypipe() |
| 124 | + .pipe(header, banner.full, { |
| 125 | + package: pkg |
| 126 | + }) |
| 127 | + .pipe(optimizejs) |
| 128 | + .pipe(dest, paths.scripts.output) |
| 129 | + .pipe(rename, { |
| 130 | + suffix: '.min' |
| 131 | + }) |
| 132 | + .pipe(uglify) |
| 133 | + .pipe(optimizejs) |
| 134 | + .pipe(header, banner.min, { |
| 135 | + package: pkg |
| 136 | + }) |
| 137 | + .pipe(dest, paths.scripts.output) |
| 138 | + |
| 139 | +// Lint, minify, and concatenate scripts |
| 140 | +var buildScripts = function(done) { |
| 141 | + // Make sure this feature is activated before running |
| 142 | + if (!settings.scripts) return done() |
| 143 | + |
| 144 | + // Run tasks on script files |
| 145 | + src(paths.scripts.input) |
| 146 | + .pipe(flatmap(function(stream, file) { |
| 147 | + // If the file is a directory |
| 148 | + if (file.isDirectory()) { |
| 149 | + // Setup a suffix variable |
| 150 | + var suffix = '' |
| 151 | + |
| 152 | + // If separate polyfill files enabled |
| 153 | + if (settings.polyfills) { |
| 154 | + // Update the suffix |
| 155 | + suffix = '.polyfills' |
| 156 | + |
| 157 | + // Grab files that aren't polyfills, concatenate them, and process them |
| 158 | + src([file.path + '/*.js', '!' + file.path + '/*' + paths.scripts.polyfills]) |
| 159 | + .pipe(concat(file.relative + '.js')) |
| 160 | + .pipe(jsTasks()) |
| 161 | + } |
| 162 | + |
| 163 | + // Grab all files and concatenate them |
| 164 | + // If separate polyfills enabled, this will have .polyfills in the filename |
| 165 | + src(file.path + '/*.js') |
| 166 | + .pipe(concat(file.relative + suffix + '.js')) |
| 167 | + .pipe(jsTasks()) |
| 168 | + |
| 169 | + return stream |
| 170 | + } |
| 171 | + |
| 172 | + // Otherwise, process the file |
| 173 | + return stream.pipe(jsTasks()) |
| 174 | + })) |
| 175 | + |
| 176 | + // Signal completion |
| 177 | + done() |
| 178 | +} |
| 179 | + |
| 180 | +// Lint scripts |
| 181 | +var lintScripts = function(done) { |
| 182 | + // Make sure this feature is activated before running |
| 183 | + if (!settings.scripts) return done() |
| 184 | + |
| 185 | + // Lint scripts |
| 186 | + src(paths.scripts.input) |
| 187 | + .pipe(standard({ |
| 188 | + fix: true |
| 189 | + })) |
| 190 | + .pipe(standard.reporter('default')) |
| 191 | + |
| 192 | + // Signal completion |
| 193 | + done() |
| 194 | +} |
| 195 | + |
| 196 | +// Process, lint, and minify Sass files |
| 197 | +var buildStyles = function(done) { |
| 198 | + // Make sure this feature is activated before running |
| 199 | + if (!settings.styles) return done() |
| 200 | + |
| 201 | + // Run tasks on all Sass files |
| 202 | + src(paths.styles.input) |
| 203 | + .pipe(sourcemaps.init()) |
| 204 | + .pipe(sass({ |
| 205 | + outputStyle: 'expanded', |
| 206 | + sourceComments: true |
| 207 | + })) |
| 208 | + .pipe(prefix({ |
| 209 | + browsers: ['last 2 version', '> 0.25%'], |
| 210 | + cascade: true, |
| 211 | + remove: true |
| 212 | + })) |
| 213 | + // Uncomment if you want the non minified files |
| 214 | + // .pipe(header(banner.full, { |
| 215 | + // package: pkg |
| 216 | + // })) |
| 217 | + // .pipe(dest(paths.styles.output)) |
| 218 | + .pipe(rename({ |
| 219 | + suffix: '.min' |
| 220 | + })) |
| 221 | + .pipe(minify({ |
| 222 | + discardComments: { |
| 223 | + removeAll: true |
| 224 | + } |
| 225 | + })) |
| 226 | + .pipe(header(banner.min, { |
| 227 | + package: pkg |
| 228 | + })) |
| 229 | + .pipe(sourcemaps.write('.')) |
| 230 | + .pipe(dest(paths.styles.output)) |
| 231 | + |
| 232 | + // Signal completion |
| 233 | + done() |
| 234 | +} |
| 235 | + |
| 236 | +// Optimize SVG files |
| 237 | +var buildSVGs = function(done) { |
| 238 | + // Make sure this feature is activated before running |
| 239 | + if (!settings.svgs) return done() |
| 240 | + |
| 241 | + // Optimize SVG files |
| 242 | + src(paths.svgs.input) |
| 243 | + .pipe(svgmin()) |
| 244 | + .pipe(dest(paths.svgs.output)) |
| 245 | + |
| 246 | + // Signal completion |
| 247 | + done() |
| 248 | +} |
| 249 | + |
| 250 | +// Copy third-party dependencies from node_modules into resources |
| 251 | +var vendorFiles = function(done) { |
| 252 | + // Make sure this feature is activated before running |
| 253 | + if (!settings.vendor) return done() |
| 254 | + |
| 255 | + // TODO ensure each declared third-parrty dep has a corresponding command below |
| 256 | + // TODO modernizr@2 needs refactor via npm or gulp-modernizr |
| 257 | + var deps = pkg.dependencies.length |
| 258 | + |
| 259 | + |
| 260 | + // copy vendor scripts |
| 261 | + src(['node_modules/bootstrap/dist/js/bootstrap.min.*', 'node_modules/jquery/dist/jquery.slim.min.*']) |
| 262 | + .pipe(dest(paths.scripts.output)) |
| 263 | + |
| 264 | + // copy vendor Styles |
| 265 | + src(['node_modules/bootstrap/dist/css/bootstrap.min.*', 'node_modules/highlight.js/styles/atom-one-dark.css']) |
| 266 | + .pipe(dest(paths.styles.output)) |
| 267 | + |
| 268 | + // copy vendor fonts |
| 269 | + src('node_modules/@neos21/bootstrap3-glyphicons/dist/fonts/*') |
| 270 | + .pipe(dest(paths.fonts.output)) |
| 271 | + // Signal completion |
| 272 | + done() |
| 273 | +} |
| 274 | + |
| 275 | + |
| 276 | +// Copy static files into output folder |
| 277 | +var copyFiles = function(done) { |
| 278 | + // Make sure this feature is activated before running |
| 279 | + if (!settings.copy) return done() |
| 280 | + |
| 281 | + // Copy static files |
| 282 | + src(paths.copy.input) |
| 283 | + .pipe(dest(paths.copy.output)) |
| 284 | + |
| 285 | + // Signal completion |
| 286 | + done() |
| 287 | +} |
| 288 | + |
| 289 | +// Build and copy highlight.js |
| 290 | +var buildPack = function(done) { |
| 291 | + // Make sure this feature is activated before running |
| 292 | + if (!settings.hjs) return done() |
| 293 | + |
| 294 | + // build highlight pack |
| 295 | + // see https://highlightjs.readthedocs.io/en/latest/building-testing.html |
| 296 | + // TODO currently building is bugged when using npm |
| 297 | + let command = 'cd node_modules/highlight.js' |
| 298 | + + ' && npm install' |
| 299 | + + ' && node tools/build Apache CSS HTTP JavaScript Bash Makefile PHP Diff JSON Markdown Perl SQL HTML Java Nginx Shell Properties Less SCSS Puppet Dockerfile xquery' |
| 300 | + |
| 301 | + exec(command, (err, stdout, stderr)=> { |
| 302 | + console.log(stderr) |
| 303 | + console.log(stdout) |
| 304 | + |
| 305 | + callback(err) |
| 306 | + }) |
| 307 | + |
| 308 | + src('node_modules/highlight.js/build/*pack.js') |
| 309 | + .pipe(dest(paths.scripts.output)) |
| 310 | + |
| 311 | + // Signal completion |
| 312 | + done() |
| 313 | +} |
| 314 | + |
| 315 | +/** |
| 316 | + * Export Tasks |
| 317 | + */ |
| 318 | + |
| 319 | +// Default task |
| 320 | +// gulp |
| 321 | +exports.default = series( |
| 322 | + cleanDist, |
| 323 | + vendorFiles, |
| 324 | + parallel( |
| 325 | + buildScripts, |
| 326 | + lintScripts, |
| 327 | + buildStyles, |
| 328 | + buildSVGs, |
| 329 | + copyFiles, |
| 330 | + buildPack |
| 331 | + ) |
| 332 | +) |
0 commit comments