-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
804ca21
commit dd7328c
Showing
1 changed file
with
56 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// } | ||
// }) | ||
// }); |