Layout von Websiten mit Bootstrap und Foundation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gulpfile.babel.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict';
  2. import plugins from 'gulp-load-plugins';
  3. import yargs from 'yargs';
  4. import browser from 'browser-sync';
  5. import gulp from 'gulp';
  6. import panini from 'panini';
  7. import rimraf from 'rimraf';
  8. import sherpa from 'style-sherpa';
  9. import yaml from 'js-yaml';
  10. import fs from 'fs';
  11. import webpackStream from 'webpack-stream';
  12. import webpack2 from 'webpack';
  13. import named from 'vinyl-named';
  14. import uncss from 'uncss';
  15. import autoprefixer from 'autoprefixer';
  16. // Load all Gulp plugins into one variable
  17. const $ = plugins();
  18. // Check for --production flag
  19. const PRODUCTION = !!(yargs.argv.production);
  20. // Load settings from settings.yml
  21. const { PORT, UNCSS_OPTIONS, PATHS } = loadConfig();
  22. function loadConfig() {
  23. let ymlFile = fs.readFileSync('config.yml', 'utf8');
  24. return yaml.load(ymlFile);
  25. }
  26. // Build the "dist" folder by running all of the below tasks
  27. // Sass must be run later so UnCSS can search for used classes in the others assets.
  28. gulp.task('build',
  29. gulp.series(clean, gulp.parallel(pages, javascript, images, copy), sass, styleGuide));
  30. // Build the site, run the server, and watch for file changes
  31. gulp.task('default',
  32. gulp.series('build', server, watch));
  33. // Delete the "dist" folder
  34. // This happens every time a build starts
  35. function clean(done) {
  36. rimraf(PATHS.dist, done);
  37. }
  38. // Copy files out of the assets folder
  39. // This task skips over the "img", "js", and "scss" folders, which are parsed separately
  40. function copy() {
  41. return gulp.src(PATHS.assets)
  42. .pipe(gulp.dest(PATHS.dist + '/assets'));
  43. }
  44. // Copy page templates into finished HTML files
  45. function pages() {
  46. return gulp.src('src/pages/**/*.{html,hbs,handlebars}')
  47. .pipe(panini({
  48. root: 'src/pages/',
  49. layouts: 'src/layouts/',
  50. partials: 'src/partials/',
  51. data: 'src/data/',
  52. helpers: 'src/helpers/'
  53. }))
  54. .pipe(gulp.dest(PATHS.dist));
  55. }
  56. // Load updated HTML templates and partials into Panini
  57. function resetPages(done) {
  58. panini.refresh();
  59. done();
  60. }
  61. // Generate a style guide from the Markdown content and HTML template in styleguide/
  62. function styleGuide(done) {
  63. sherpa('src/styleguide/index.md', {
  64. output: PATHS.dist + '/styleguide.html',
  65. template: 'src/styleguide/template.html'
  66. }, done);
  67. }
  68. // Compile Sass into CSS
  69. // In production, the CSS is compressed
  70. function sass() {
  71. const postCssPlugins = [
  72. // Autoprefixer
  73. autoprefixer(),
  74. // UnCSS - Uncomment to remove unused styles in production
  75. // PRODUCTION && uncss.postcssPlugin(UNCSS_OPTIONS),
  76. ].filter(Boolean);
  77. return gulp.src('src/assets/scss/app.scss')
  78. .pipe($.sourcemaps.init())
  79. .pipe($.sass({
  80. includePaths: PATHS.sass
  81. })
  82. .on('error', $.sass.logError))
  83. .pipe($.postcss(postCssPlugins))
  84. .pipe($.if(PRODUCTION, $.cleanCss({ compatibility: 'ie9' })))
  85. .pipe($.if(!PRODUCTION, $.sourcemaps.write()))
  86. .pipe(gulp.dest(PATHS.dist + '/assets/css'))
  87. .pipe(browser.reload({ stream: true }));
  88. }
  89. let webpackConfig = {
  90. mode: (PRODUCTION ? 'production' : 'development'),
  91. module: {
  92. rules: [
  93. {
  94. test: /\.js$/,
  95. use: {
  96. loader: 'babel-loader',
  97. options: {
  98. presets: [ "@babel/preset-env" ],
  99. compact: false
  100. }
  101. }
  102. }
  103. ]
  104. },
  105. devtool: !PRODUCTION && 'source-map'
  106. }
  107. // Combine JavaScript into one file
  108. // In production, the file is minified
  109. function javascript() {
  110. return gulp.src(PATHS.entries)
  111. .pipe(named())
  112. .pipe($.sourcemaps.init())
  113. .pipe(webpackStream(webpackConfig, webpack2))
  114. .pipe($.if(PRODUCTION, $.terser()
  115. .on('error', e => { console.log(e); })
  116. ))
  117. .pipe($.if(!PRODUCTION, $.sourcemaps.write()))
  118. .pipe(gulp.dest(PATHS.dist + '/assets/js'));
  119. }
  120. // Copy images to the "dist" folder
  121. // In production, the images are compressed
  122. function images() {
  123. return gulp.src('src/assets/img/**/*')
  124. .pipe($.if(PRODUCTION, $.imagemin([
  125. $.imagemin.jpegtran({ progressive: true }),
  126. ])))
  127. .pipe(gulp.dest(PATHS.dist + '/assets/img'));
  128. }
  129. // Start a server with BrowserSync to preview the site in
  130. function server(done) {
  131. browser.init({
  132. server: PATHS.dist, port: PORT
  133. }, done);
  134. }
  135. // Reload the browser with BrowserSync
  136. function reload(done) {
  137. browser.reload();
  138. done();
  139. }
  140. // Watch for changes to static assets, pages, Sass, and JavaScript
  141. function watch() {
  142. gulp.watch(PATHS.assets, copy);
  143. gulp.watch('src/pages/**/*.html').on('all', gulp.series(pages, browser.reload));
  144. gulp.watch('src/{layouts,partials}/**/*.html').on('all', gulp.series(resetPages, pages, browser.reload));
  145. gulp.watch('src/data/**/*.{js,json,yml}').on('all', gulp.series(resetPages, pages, browser.reload));
  146. gulp.watch('src/helpers/**/*.js').on('all', gulp.series(resetPages, pages, browser.reload));
  147. gulp.watch('src/assets/scss/**/*.scss').on('all', sass);
  148. gulp.watch('src/assets/js/**/*.js').on('all', gulp.series(javascript, browser.reload));
  149. gulp.watch('src/assets/img/**/*').on('all', gulp.series(images, browser.reload));
  150. gulp.watch('src/styleguide/**').on('all', gulp.series(styleGuide, browser.reload));
  151. }