Skip to content

Commit

Permalink
perf: update to support dir looping
Browse files Browse the repository at this point in the history
This commit adds default repo support for looping through all CSS files
within a ./src dir for postCSS scripting.

The legacy single-file static process is maintained if a developer
wishes this for their element.

Changes to be committed:
modified:   template/scripts/postCss.mjs
  • Loading branch information
blackfalcon committed Jan 23, 2024
1 parent 804ca21 commit dd7328c
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions template/scripts/postCss.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import comments from 'postcss-discard-comments';
import path from 'path';
import fs from 'fs';
const __dirname = new URL('.', import.meta.url).pathname;
const directoryPath = path.join(__dirname, '../src');

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)
}
})
/**
* Default postCSS run
* Locates all CSS files within the directory and loop
* through the standardProcessor() function.
*/
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);
}
});
});

/**
* The standardProcessor function applies tokens for fallback selectors
* and completes a post cleanup.
* @param {string} file
*/
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)
})
});
}

/**
* 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)
// }
// })
// });

0 comments on commit dd7328c

Please sign in to comment.