-
Notifications
You must be signed in to change notification settings - Fork 23
/
scripts.js
23 lines (19 loc) · 899 Bytes
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const fs = require('fs');
const path = require('path');
const minify = require('minify');
function minifyFiles (extension) {
const assetsPath = path.resolve(`./views/${extension}`);
const assetsFiles = fs.readdirSync(assetsPath);
const isNotMinifiedAndHasSelectedExtension = filePath => filePath.includes(`.${extension}`) && !filePath.includes('.min');
const filtredFiles = assetsFiles.filter(filePath => isNotMinifiedAndHasSelectedExtension(filePath));
filtredFiles.forEach(file => {
const filePath = path.resolve(`${assetsPath}/${file}`);
minify(filePath, { js: { ecma: 6 }, css: { compatibility: '*' } })
.then(minifiedContent => {
const newFilePathName = filePath.split(`.${extension}`)[0].concat(`.min.${extension}`);
fs.writeFileSync(newFilePathName, minifiedContent);
})
.catch(console.error);
});
}
module.exports = { minifyFiles };