-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathgulpfile.mjs
More file actions
151 lines (129 loc) · 4.89 KB
/
gulpfile.mjs
File metadata and controls
151 lines (129 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import autoprefixer from 'autoprefixer';
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import filters from 'gulp-filter';
import gulp from 'gulp';
import nodeSassImporter from 'node-sass-importer';
import postcss from 'postcss';
import rename from 'gulp-rename';
import sass from 'gulp-dart-sass';
import { promisify } from 'util';
import { compileAsync } from 'sass'
import tailwindcss from '@tailwindcss/postcss';
import twAdminConfig from './config/tailwind.admin.config.mjs';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
const writeFile = promisify(fs.writeFile);
gulp.task('sass', async function () {
try {
const scssDir = './assets/sass';
const outputDir = './resources/css';
const privateScssDir = './private/sass';
// Ensure the private sass directory exists
fs.mkdirSync(privateScssDir, { recursive: true });
// Create an optional _custom.scss file if it doesn't exist
const optionalCustomCss = path.join(privateScssDir, '_custom.scss');
if (!fs.existsSync(optionalCustomCss)) {
fs.writeFileSync(
optionalCustomCss,
'// auto-generated stub for optional custom overrides\n'
);
}
const files = fs.readdirSync(scssDir);
const scssFiles = files.filter(file => path.extname(file) === '.scss' && file !== 'tailwind.admin.scss');
for (const file of scssFiles) {
const inputPath = path.join(scssDir, file);
const outputPath = path.join(outputDir, path.basename(file, '.scss') + '.css');
const result = await compileAsync(inputPath, {
loadPaths: [scssDir],
});
await writeFile(outputPath, result.css);
console.log(`Compiled ${file} to ${outputPath}`);
}
} catch (error) {
console.error('Error compiling Sass:', error);
}
});
gulp.task('tailwind-admin', async function () {
try {
const inputPath = './assets/sass/tailwind.admin.scss';
const outputPath = './resources/css/tailwind.admin.css';
const result = await compileAsync(inputPath, {
loadPaths: ['./assets/sass'],
importer: nodeSassImporter,
});
const processedCss = await postcss([tailwindcss(twAdminConfig), autoprefixer()]).process(result.css, {
from: inputPath,
to: outputPath,
});
await writeFile(outputPath, processedCss.css);
console.log(`Compiled and processed Tailwind Admin SCSS to ${outputPath}`);
} catch (error) {
console.error('Error compiling Tailwind Admin:', error);
}
});
gulp.task('js', function () {
return gulp
.src('./assets/js/**/*.js')
.pipe(babel({
presets: ['@babel/env'],
ignore: ['assets/js/sync-to-drive.js', 'assets/js/remotebuzzer-server.js']
}))
.pipe(gulp.dest('./resources/js'));
});
gulp.task('js-admin', function () {
return gulp
.src([
'./assets/js/tools.js',
'./assets/js/admin/index.js',
'./assets/js/admin/buttons.js',
'./assets/js/admin/navi.js',
'./assets/js/admin/keypad.js',
'./assets/js/admin/imageSelect.js',
'./assets/js/admin/fontSelect.js',
'./assets/js/admin/videoSelect.js',
'./assets/js/admin/themes.js',
'./assets/js/admin/toast.js',
])
.pipe(concat('main.admin.js'))
.pipe(babel({
presets: ['@babel/env'],
ignore: ['assets/js/sync-to-drive.js', 'assets/js/remotebuzzer-server.js']
}))
.pipe(gulp.dest('./resources/js'));
});
async function generateAssetRevisions() {
const resourcesFolder = 'resources';
const revisionsManifest = 'resources/revisions.json';
const manifest = {};
const processFile = async (filePath) => {
const content = fs.readFileSync(filePath);
const sha1Hash = crypto.createHash('sha1').update(content).digest('hex');
const relativePath = path.relative(resourcesFolder, filePath).replace(/\\/g, '/');
manifest[resourcesFolder + '/' + relativePath] = sha1Hash;
};
const processFolder = async (folderPath) => {
const files = fs.readdirSync(folderPath);
for (const file of files) {
const filePath = path.join(folderPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
await processFolder(filePath);
} else if (stats.isFile() && !filePath.endsWith('revisions.json')) {
await processFile(filePath);
}
}
};
await processFolder(resourcesFolder);
const manifestJSON = JSON.stringify(manifest, null, 2);
fs.writeFileSync(revisionsManifest, manifestJSON);
}
gulp.task('default', gulp.series(
gulp.parallel('sass', 'js', 'js-admin', 'tailwind-admin'),
generateAssetRevisions
));
gulp.task('watch', function () {
gulp.watch(['assets/js/**/*.js'], gulp.series('js', 'js-admin', generateAssetRevisions));
gulp.watch(['assets/sass/**/*.scss', 'private/sass/**/*.scss', 'config/tailwind.admin.config.mjs'], gulp.series('sass', 'tailwind-admin', generateAssetRevisions));
});