Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: postCss searches folders recursively #68 #69

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 50 additions & 48 deletions scripts/build/postCss.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,64 @@ import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import comments from 'postcss-discard-comments';
import path from 'path';
import fs from 'fs';
import fs from 'fs/promises';

const __dirname = new URL('.', import.meta.url).pathname;
const directoryPath = path.join(__dirname, '../src');
const directories = [
path.join(__dirname, '../src'),
path.join(__dirname, '../components')
];

/**
* Default postCSS run
* Locates all CSS files within the directory and loop
* through the standardProcessor() function.
* Recursively process CSS files in a directory and its subdirectories
* @param {string} dir - Directory to process
*/
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
if (file.includes(".css")) {
standardProcessor(file);
async function processCssFiles(dir) {
try {
const files = await fs.readdir(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
await processCssFiles(fullPath);
} else if (path.extname(file.name).toLowerCase() === '.css') {
await processPostCss(fullPath);
}
}
});
});
} catch (err) {
console.error(`Processing failed for directory ${dir}:`, err);
}
}

/**
* The standardProcessor function applies tokens for fallback selectors
* and completes a post cleanup.
* @param {string} file
* Process CSS file with PostCSS
* Applies autoprefixer and removes comments starting with '@'
* @param {string} filePath - Full path of CSS file to process
*/
function standardProcessor(file) {
fs.readFile(`src/${file}`, (err, css) => {
postcss([autoprefixer, comments])
.use(comments({
remove: function(comment) { return comment[0] == "@"; }
}))
.process(css, { from: `src/${file}`, to: `src/${file}` })
.then(result => {
fs.writeFile(`src/${file}`, result.css, () => true)
})
});
async function processPostCss(filePath) {
try {
const css = await fs.readFile(filePath, 'utf8');
const result = await postcss([
autoprefixer,
comments({
remove: comment => comment[0] === '@'
})
]).process(css, { from: filePath, to: filePath });
await fs.writeFile(filePath, result.css);
console.log(`Processed: ${filePath}`);
} catch (err) {
console.error(`Error processing ${filePath}:`, err);
}
}

/**
* ALTERNATE script:
* The following is a static builder for rendering one
* CSS file at a time if that is required.
*/
// fs.readFile('src/style.css', (err, css) => {
// postcss([autoprefixer, comments])
// .use(comments({
// remove: function(comment) { return comment[0] == "@"; }
// }))
// .process(css, { from: 'src/style.css', to: 'src/style.css' })
// .then(result => {
// fs.writeFile('src/style.css', result.css, () => true)
// if ( result.map ) {
// fs.writeFile('src/style.map', result.map, () => true)
// }
// })
// });
// Main execution
(async () => {
for (const dir of directories) {
try {
await fs.access(dir);
console.log(`Processing CSS files in: ${dir}`);
await processCssFiles(dir);
} catch (error) {
console.log(`Directory not found or not accessible: ${dir}`);
}
}
})();