|
| 1 | +const gulp = require('gulp'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const uglifycss = require('gulp-uglifycss'); |
| 5 | +const rename = require('gulp-rename'); |
| 6 | + |
| 7 | +// directories |
| 8 | +const rootDir = path.resolve(__dirname, '../'); |
| 9 | +const distDir = path.resolve(rootDir, 'dist/resources'); |
| 10 | +const libDir = path.resolve(rootDir, 'components/lib'); |
| 11 | + |
| 12 | +function findFilesWithExtension(directory, extension) { |
| 13 | + if (!fs.existsSync(directory)) { |
| 14 | + // eslint-disable-next-line no-console |
| 15 | + console.error(`Directory "${directory}" does not exist.`); |
| 16 | + |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + const filesAndSubdirs = fs.readdirSync(directory); |
| 21 | + |
| 22 | + filesAndSubdirs.forEach((item) => { |
| 23 | + const itemPath = path.join(directory, item); |
| 24 | + |
| 25 | + if (fs.statSync(itemPath).isFile()) { |
| 26 | + if (item.endsWith(extension)) { |
| 27 | + // Process the file and extract styles |
| 28 | + processFile(itemPath); |
| 29 | + } |
| 30 | + } else if (fs.statSync(itemPath).isDirectory()) { |
| 31 | + findFilesWithExtension(itemPath, extension); |
| 32 | + } |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function removeLayerAndLastBrace(inputString) { |
| 37 | + // Remove the text "@layer primereact {" from the string |
| 38 | + const withoutLayer = inputString.replace(/@layer primereact {/, ''); |
| 39 | + |
| 40 | + // Find the last occurrence of '}' and remove it |
| 41 | + const lastIndex = withoutLayer.lastIndexOf('}'); |
| 42 | + |
| 43 | + if (lastIndex !== -1) { |
| 44 | + return withoutLayer.substring(0, lastIndex) + withoutLayer.substring(lastIndex + 1); |
| 45 | + } |
| 46 | + |
| 47 | + return withoutLayer; |
| 48 | +} |
| 49 | + |
| 50 | +function processFile(filePath) { |
| 51 | + try { |
| 52 | + const regexes = [ |
| 53 | + /const styles = `([\s\S]*?)`/s, |
| 54 | + /const baseStyle = `([\s\S]*?)`/s, |
| 55 | + /const buttonStyles = `([\s\S]*?)`/s, |
| 56 | + /const checkboxStyles = `([\s\S]*?)`/s, |
| 57 | + /const inputTextStyles = `([\s\S]*?)`/s, |
| 58 | + /const radioButtonStyles = `([\s\S]*?)`/s, |
| 59 | + /const iconStyles = `([\s\S]*?)`/s, |
| 60 | + /const commonStyles = `([\s\S]*?)`/s |
| 61 | + ]; |
| 62 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 63 | + |
| 64 | + for (let index = 0; index < regexes.length; index++) { |
| 65 | + const regex = regexes[index]; |
| 66 | + const matches = regex.exec(fileContent); |
| 67 | + |
| 68 | + if (matches && matches[1]) { |
| 69 | + let styles = matches[1]; |
| 70 | + const hasLayer = styles.includes('@layer primereact'); |
| 71 | + |
| 72 | + if (hasLayer) { |
| 73 | + styles = removeLayerAndLastBrace(styles); |
| 74 | + // Append the styles to the primereact.css file |
| 75 | + fs.appendFileSync(path.resolve(distDir, 'primereact.css'), styles); |
| 76 | + |
| 77 | + // eslint-disable-next-line no-console |
| 78 | + console.log(`Styles from ${filePath} added to primereact.css`); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (err) { |
| 83 | + // eslint-disable-next-line no-console |
| 84 | + console.error(`Error processing file ${filePath}: ${err.message}`); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const fileExtensionToFind = 'Base.js'; |
| 89 | + |
| 90 | +// Clear the existing primereact.css file |
| 91 | +!fs.existsSync(distDir) && fs.mkdirSync(distDir); |
| 92 | + |
| 93 | +// start the layer |
| 94 | +fs.writeFileSync(path.resolve(distDir, 'primereact.css'), `@layer primereact {\n`); |
| 95 | + |
| 96 | +findFilesWithExtension(libDir, fileExtensionToFind); |
| 97 | + |
| 98 | +// close the layer |
| 99 | +fs.appendFileSync(path.resolve(distDir, 'primereact.css'), `}\n`); |
| 100 | + |
| 101 | +// Create a Gulp task to minimize the generated CSS |
| 102 | +gulp.task('minify-css', function () { |
| 103 | + return gulp |
| 104 | + .src(path.resolve(distDir, 'primereact.css')) |
| 105 | + .pipe(uglifycss()) |
| 106 | + .pipe(rename('primereact.min.css')) // Renaming the file |
| 107 | + .pipe(gulp.dest('./dist/resources/')) |
| 108 | + .on('end', function () { |
| 109 | + // eslint-disable-next-line no-console |
| 110 | + console.log('CSS file minimized and saved as primereact.css'); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +// Run the Gulp 'minify-css' task |
| 115 | +gulp.series('minify-css')(); |
0 commit comments