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.

161 lines
4.4 KiB

10 months ago
4 weeks ago
10 months ago
4 weeks ago
10 months ago
4 weeks ago
10 months ago
  1. import gulp from 'gulp';
  2. import gf from 'get-google-fonts';
  3. import concat from 'gulp-concat';
  4. import autoprefixer from 'gulp-autoprefixer';
  5. import cleanCSS from 'gulp-clean-css';
  6. import * as dartSass from 'sass';
  7. import gulpSass from 'gulp-sass';
  8. import {deleteSync} from 'del';
  9. import axios from 'axios';
  10. import fs from 'fs-extra';
  11. import path from 'path';
  12. import imagemin from 'imagemin';
  13. import imageminWebp from 'imagemin-webp';
  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. const files = await imagemin([config.watch.img], {
  101. plugins: [
  102. imageminWebp({ quality: 80 }),
  103. ],
  104. });
  105. // Verarbeite jede Datei und speichere sie
  106. await files.forEach(file => {
  107. const relativePath = path.relative(config.watch.img, file.sourcePath);
  108. const outputPath = config.dist + '/' + config.app + '/' + relativePath.replace(path.extname(relativePath), '.webp').replace('../../', '');
  109. console.log(outputPath);
  110. // Stelle sicher, dass das Verzeichnis existiert
  111. ensureDirectoryExistence(outputPath);
  112. // Schreibe die konvertierte Datei in das Zielverzeichnis
  113. fs.writeFileSync(outputPath, file.data);
  114. });
  115. }
  116. function ensureDirectoryExistence(filePath) {
  117. const dir = path.dirname(filePath);
  118. if (!fs.existsSync(dir)) {
  119. fs.mkdirSync(dir, { recursive: true });
  120. }
  121. }
  122. function watchImg() {
  123. gulp.watch(config.watch.img, gulp.series(buildImg))
  124. }
  125. async function cleanDist() {
  126. deleteSync([config.del.dist]);
  127. }
  128. async function copyPublic(){
  129. await gulp.src(config.watch.srcPublic)
  130. .pipe(gulp.dest(config.dist + '/' + config.app))
  131. }
  132. function publish(){
  133. deleteSync([config.publ]);
  134. gulp.src(config.watch.dist)
  135. .pipe(gulp.dest(config.publ))
  136. }
  137. export { clean, build, watch };