Guest User

Untitled

a guest
Nov 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import plugins from 'gulp-load-plugins';
  4. import yargs from 'yargs';
  5. import browser from 'browser-sync';
  6. import gulp from 'gulp';
  7. import panini from 'panini';
  8. import rimraf from 'rimraf';
  9. import sherpa from 'style-sherpa';
  10. import yaml from 'js-yaml';
  11. import fs from 'fs';
  12. import webpackStream from 'webpack-stream';
  13. import webpack2 from 'webpack';
  14. import named from 'vinyl-named';
  15. import sassLint from 'gulp-sass-lint';
  16.  
  17.  
  18. // Load all Gulp plugins into one variable
  19. const $ = plugins();
  20.  
  21. // Check for --production flag
  22. const PRODUCTION = !!(yargs.argv.production);
  23.  
  24. // Load settings from settings.yml
  25. const { COMPATIBILITY, PORT, UNCSS_OPTIONS, PATHS } = loadConfig();
  26.  
  27. function loadConfig() {
  28. let ymlFile = fs.readFileSync('config.yml', 'utf8');
  29. return yaml.load(ymlFile);
  30. }
  31.  
  32. // Lint the Sass
  33. gulp.task('lint', function () {
  34. return gulp.src([
  35. 'src/assets/scss/**/*.s+(a|c)ss',
  36. '!src/assets/scss/pages/_csp-search-results.scss',
  37. '!src/assets/scss/_settings.scss',
  38. '!src/assets/scss/app.scss',
  39. '!src/assets/scss/coveocomponents.scss',
  40. '!src/assets/scss/global/_colors.scss',
  41. '!src/assets/scss/kendo.common.min.scss',
  42. '!src/assets/scss/kendo.default.min.scss',
  43. '!src/assets/scss/kendo.default.mobile.min.scss',
  44. '!src/assets/scss/vendor/slick-theme.scss',
  45. '!src/assets/scss/vendor/slick.scss'
  46. ])
  47. .pipe(sassLint())
  48. .pipe(sassLint.format())
  49. .pipe(sassLint.failOnError())
  50. });
  51.  
  52. // Build the "dist" folder by running all of the below tasks
  53. gulp.task('build',
  54. gulp.series(clean, gulp.parallel(pages, 'lint', sass, javascript, images, copy), styleGuide));
  55.  
  56. // Build the site, run the server, and watch for file changes
  57. gulp.task('default',
  58. gulp.series('build', server, watch));
  59.  
  60. // Delete the "dist" folder
  61. // This happens every time a build starts
  62. function clean(done) {
  63. rimraf(PATHS.dist, done);
  64. }
  65.  
  66. // Copy files out of the assets folder
  67. // This task skips over the "img", "js", and "scss" folders, which are parsed separately
  68. function copy() {
  69. return gulp.src(PATHS.assets)
  70. .pipe(gulp.dest(PATHS.dist + '/assets'));
  71. }
  72.  
  73. // Copy page templates into finished HTML files
  74. function pages() {
  75. return gulp.src('src/pages/**/*.{html,hbs,handlebars}')
  76. .pipe(panini({
  77. root: 'src/pages/',
  78. layouts: 'src/layouts/',
  79. partials: 'src/partials/',
  80. data: 'src/data/',
  81. helpers: 'src/helpers/'
  82. }))
  83. .pipe(gulp.dest(PATHS.dist));
  84. }
  85.  
  86. // Load updated HTML templates and partials into Panini
  87. function resetPages(done) {
  88. panini.refresh();
  89. done();
  90. }
  91.  
  92. // Generate a style guide from the Markdown content and HTML template in styleguide/
  93. function styleGuide(done) {
  94. sherpa('src/styleguide/index.md', {
  95. output: PATHS.dist + '/styleguide.html',
  96. template: 'src/styleguide/template.html'
  97. }, done);
  98. }
  99.  
  100. // Compile Sass into CSS
  101. // In production, the CSS is compressed
  102. function sass() {
  103. return gulp.src('src/assets/scss/app.scss')
  104. .pipe($.sourcemaps.init())
  105. .pipe($.sass({
  106. includePaths: PATHS.sass
  107. })
  108. .on('error', $.sass.logError))
  109. .pipe($.autoprefixer({
  110. browsers: COMPATIBILITY
  111. }))
  112. // Comment in the pipe below to run UnCSS in production
  113. //.pipe($.if(PRODUCTION, $.uncss(UNCSS_OPTIONS)))
  114. .pipe($.if(PRODUCTION, $.cleanCss({ compatibility: 'ie9' })))
  115. .pipe($.if(!PRODUCTION, $.sourcemaps.write()))
  116. .pipe(gulp.dest(PATHS.dist + '/assets/css'))
  117. .pipe(browser.reload({ stream: true }));
  118. }
  119.  
  120. let webpackConfig = {
  121. rules: [
  122. {
  123. test: /.js$/,
  124. use: [
  125. {
  126. loader: 'babel-loader'
  127. }
  128. ]
  129. }
  130. ]
  131. }
  132. // Combine JavaScript into one file
  133. // In production, the file is minified
  134. function javascript() {
  135. return gulp.src(PATHS.entries)
  136. .pipe(named())
  137. .pipe($.sourcemaps.init())
  138. .pipe(webpackStream({module: webpackConfig}, webpack2))
  139. .pipe($.if(PRODUCTION, $.uglify()
  140. .on('error', e => { console.log(e); })
  141. ))
  142. .pipe($.if(!PRODUCTION, $.sourcemaps.write()))
  143. .pipe(gulp.dest(PATHS.dist + '/assets/js'));
  144. }
  145.  
  146. // Copy images to the "dist" folder
  147. // In production, the images are compressed
  148. function images() {
  149. return gulp.src('src/assets/img/**/*')
  150. .pipe($.if(PRODUCTION, $.imagemin({
  151. progressive: true
  152. })))
  153. .pipe(gulp.dest(PATHS.dist + '/assets/img'));
  154. }
  155.  
  156. // Start a server with BrowserSync to preview the site in
  157. function server(done) {
  158. browser.init({
  159. server: PATHS.dist, port: PORT
  160. });
  161. done();
  162. }
  163.  
  164. // Reload the browser with BrowserSync
  165. function reload(done) {
  166. browser.reload();
  167. done();
  168. }
  169.  
  170. // Watch for changes to static assets, pages, Sass, and JavaScript
  171. function watch() {
  172. gulp.watch(PATHS.assets, copy);
  173. gulp.watch('src/pages/**/*.html').on('all', gulp.series(pages, browser.reload));
  174. gulp.watch('src/{layouts,partials}/**/*.html').on('all', gulp.series(resetPages, pages, browser.reload));
  175. gulp.watch('src/assets/scss/**/*.scss').on('all', sass);
  176. gulp.watch('src/assets/js/**/*.js').on('all', gulp.series(javascript, browser.reload));
  177. gulp.watch('src/assets/img/**/*').on('all', gulp.series(images, browser.reload));
  178. gulp.watch('src/styleguide/**').on('all', gulp.series(styleGuide, browser.reload));
  179. }
Add Comment
Please, Sign In to add comment