Skip to content

Latest commit

History

History
32 lines (25 loc) 路 698 Bytes

2021-11-26.md

File metadata and controls

32 lines (25 loc) 路 698 Bytes
publish_date
2021-11-26
  • Working with webpack analyser today and got a cool script working to extract the file sizes from the json output
const fs = require('fs');
const DATA = require('../stats.json');

function getFileSize(file) {
  const { size } = fs.statSync(`.next/${file}`);
  console.log(`${file} is ${size} bytes`);
  return size;
}

const fileSizes = DATA.chunks.map(element => getFileSize(element.files[0]));

const res = fileSizes.reduce((total, num) => {
  return Number(total) + Number(num);
}, 0);

fs.writeFile(
  './public/size.json',
  JSON.stringify({
    size_in_bytes: res.toFixed(2),
    size_in_kilobytes: (res * 0.001).toFixed(2),
  }),
  'utf8'
);