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.

142 lines
3.7 KiB

7 months ago
  1. import yargs from 'yargs';
  2. import gulp from 'gulp';
  3. import gf from 'get-google-fonts';
  4. import sharp from 'gulp-sharp-responsive';
  5. import concat from 'gulp-concat';
  6. import autoprefixer from 'gulp-autoprefixer';
  7. import cleanCSS from 'gulp-clean-css';
  8. import * as dartSass from 'sass';
  9. import gulpSass from 'gulp-sass';
  10. import {deleteSync} from 'del';
  11. import axios from 'axios';
  12. import fs from 'fs-extra';
  13. import path from 'path';
  14. import {config} from "./config.mjs"
  15. const sass = gulpSass(dartSass);
  16. async function prepareSrc(mixedSources) {
  17. await fs.ensureDir(config.tempDir); // Erstellt das temporäre Verzeichnis, falls es noch nicht existiert
  18. const localFilePaths = await Promise.all(mixedSources.map(async (source, index) => {
  19. const filename = `resource_${index}${path.extname(source)}`;
  20. const localPath = path.join(config.tempDir, filename);
  21. if (/^https?:\/\//i.test(source)) {
  22. // Source ist eine URL, also herunterladen
  23. const response = await axios({ url: source, responseType: 'arraybuffer' });
  24. await fs.writeFile(localPath, response.data);
  25. } else {
  26. // Source ist ein lokaler Pfad, also kopieren
  27. return source;
  28. }
  29. return localPath;
  30. }));
  31. return localFilePaths;
  32. }
  33. function clean() {
  34. cleanDist();
  35. }
  36. async function build() {
  37. await cleanDist();
  38. await copyPublic();
  39. await buildCss();
  40. await buildJs();
  41. await buildImg();
  42. await buildFont();
  43. publish();
  44. }
  45. async function watch() {
  46. await watchCss();
  47. await watchJs();
  48. await watchImg();
  49. }
  50. function cleanCss() {
  51. deleteSync([config.del.css]);
  52. }
  53. async function buildCss() {
  54. let sources;
  55. for (let name in config.css) {
  56. sources = await prepareSrc(Object.values(config.css[name]));
  57. gulp.src(sources)
  58. .pipe(sass().on('error',sass.logError))
  59. .pipe(autoprefixer({cascade: true}))
  60. .pipe(cleanCSS({compatibility: 'ie8'}))
  61. .pipe(concat(name + '.css'))
  62. .pipe(gulp.dest(config.dist + '/' + config.app))
  63. }
  64. }
  65. function watchCss() {
  66. gulp.watch(config.watch.css, gulp.series(buildCss))
  67. }
  68. function cleanJs() {
  69. deleteSync([config.del.js]);
  70. }
  71. async function buildJs() {
  72. let sources;
  73. for (let name in config.js) {
  74. sources = await prepareSrc(Object.values(config.js[name]));
  75. gulp.src(sources)
  76. .pipe(concat(name + '.js'))
  77. .pipe(gulp.dest(config.dist + '/' + config.app))
  78. }
  79. }
  80. function watchJs() {
  81. gulp.watch(config.watch.js, gulp.series(buildJs));
  82. }
  83. function cleanFont() {
  84. deleteSync([config.del.font]);
  85. }
  86. async function buildFont(){
  87. for (let name in config.font) {
  88. await new gf({
  89. outputDir: config.dist + '/' + config.app + '/fonts/' + name,
  90. cssFile: name + '.css',
  91. userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
  92. overwriting: true
  93. }).download(config.font[name]);
  94. }
  95. }
  96. function cleanImg() {
  97. deleteSync([config.del.img]);
  98. }
  99. async function buildImg(){
  100. await gulp.src(config.watch.img)
  101. .pipe(sharp({
  102. formats: [{format: 'webp'}]
  103. }))
  104. .pipe(gulp.dest(config.dist + '/' + config.app));
  105. }
  106. function watchImg() {
  107. gulp.watch(config.watch.img, gulp.series(buildImg))
  108. }
  109. async function cleanDist() {
  110. deleteSync([config.del.dist]);
  111. }
  112. async function copyPublic(){
  113. await gulp.src(config.watch.srcPublic)
  114. .pipe(gulp.dest(config.dist + '/' + config.app))
  115. }
  116. function publish(){
  117. deleteSync([config.publ]);
  118. gulp.src(config.watch.dist)
  119. .pipe(gulp.dest(config.publ))
  120. }
  121. export { clean, build, watch };