2020-04-27 17:42:40 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
|
|
|
const gzip = require('gzip-size');
|
|
|
|
const path = require('path');
|
|
|
|
|
2022-03-14 22:44:15 +00:00
|
|
|
// load pure package metadata
|
|
|
|
const pureDir = path.dirname(require.resolve('purecss'));
|
|
|
|
const { version } = require(path.resolve(pureDir, 'package.json'));
|
|
|
|
const pureFiles = path.resolve(pureDir, 'build');
|
|
|
|
const pureMin = fs.readFileSync(path.resolve(pureFiles, 'pure-min.css'), 'utf8');
|
2020-04-27 17:42:40 +00:00
|
|
|
|
|
|
|
// use pure-min.css to determine site integrity hash
|
|
|
|
const sriHash = crypto.createHash('sha384').update(pureMin, 'utf8').digest('base64');
|
|
|
|
|
|
|
|
// copy/pastable code snippet for users
|
|
|
|
module.exports.PURE_DOWNLOAD_SNIPPET = `<link rel="stylesheet" href="https://unpkg.com/purecss@${version}/build/pure-min.css" integrity="sha384-${sriHash}" crossorigin="anonymous">`;
|
|
|
|
|
|
|
|
// calculate each pure module size
|
|
|
|
module.exports.moduleSizes = function moduleSizes() {
|
2022-03-14 22:44:15 +00:00
|
|
|
const files = fs.readdirSync(pureFiles);
|
2020-04-27 17:42:40 +00:00
|
|
|
|
|
|
|
// collect all minified module files
|
2020-05-16 18:09:14 +00:00
|
|
|
const modules = files.filter(file => (/-min\.css$/).test(file))
|
|
|
|
.map(file => file.replace(/-min\.css$/, ''));
|
2020-04-27 17:42:40 +00:00
|
|
|
|
|
|
|
// get sizes across all modules
|
|
|
|
const moduleSizes = modules.map(module => {
|
2022-03-14 22:44:15 +00:00
|
|
|
const filePath = path.join(pureFiles, module + '-min.css');
|
2020-04-27 17:42:40 +00:00
|
|
|
const contents = fs.readFileSync(filePath, { encoding: 'utf-8' });
|
|
|
|
return gzip.sync(contents);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return final list of sizes
|
|
|
|
return moduleSizes.reduce((map, size, i) => {
|
|
|
|
map[modules[i]] = size;
|
|
|
|
return map;
|
|
|
|
}, {});
|
2020-05-16 18:09:14 +00:00
|
|
|
};
|