Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(css): add hash to build files when setting addHashes to true
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrsskls committed Oct 6, 2020
1 parent bb3f6c6 commit 25adbe0
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/css/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const cssnano = require("cssnano");
* @param {Array} obj.cssFiles
* @param {string} obj.distFolder
* @param {object} obj.targets
* @param {boolean} obj.addHashes
* @returns {Promise} - Gets resolved when building is done
*/
module.exports = function buildCSS({ cssFiles, distFolder, targets }) {
module.exports = function buildCSS({
cssFiles,
distFolder,
targets,
addHashes,
}) {
if (cssFiles.length > 0) {
const plugins = [
postcssImport(),
Expand All @@ -43,12 +49,22 @@ module.exports = function buildCSS({ cssFiles, distFolder, targets }) {
new Promise((resolve, reject) => {
const fullPath = path.join(process.cwd(), file);
const basename = path.basename(file);
const hash = Date.now().toString();
const fileName = addHashes
? path.join(
distFolder,
`${path.basename(
file,
path.extname(file)
)}.${hash}${path.extname(file)}`
)
: path.join(distFolder, basename);

fs.readFile(fullPath, (err, css) => {
postcss(plugins)
.process(css, {
from: fullPath,
to: path.join(distFolder, basename),
to: fileName,
map: {
inline: false,
},
Expand All @@ -58,19 +74,15 @@ module.exports = function buildCSS({ cssFiles, distFolder, targets }) {

proms.push(
new Promise((res) => {
fs.writeFile(
path.join(distFolder, basename),
result.css,
() => res()
);
fs.writeFile(fileName, result.css, () => res());
})
);

if (result.map) {
proms.push(
new Promise((res) => {
fs.writeFile(
path.join(distFolder, `${basename}.map`),
`${fileName}.map`,
JSON.stringify(result.map),
() => res()
);
Expand Down

0 comments on commit 25adbe0

Please sign in to comment.