Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions publish-size.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
const { repo, sha, event, branch, pull_request_number, ci } = require('ci-env');
const { repo, sha, event, branch, ci } = require('ci-env');
const axios = require('axios');
const SIZE_STORE_ENDPOINT = process.env.SIZE_STORE_ENDPOINT || 'https://size-plugin-store.now.sh' ;
const SIZE_STORE_ENDPOINT =
process.env.SIZE_STORE_ENDPOINT || 'https://size-plugin-store.now.sh';

// TODO: add option to turn off publishing of sizes.

async function publishDiff(diff,filename) {
if (process.env.NODE_ENV !=='test' && ci && event == 'pull_request') {
try {
const params = { ci,repo, branch, sha, diff,filename };
await axios.post(`${SIZE_STORE_ENDPOINT}/diff`, params);
}
catch (error) {
console.error('error: while publishing diff', error);
}
}
async function publishDiff(diff, filename) {
if (process.env.NODE_ENV !== 'test' && ci && event == 'pull_request') {
try {
const params = { ci, repo, branch, sha, diff, filename };
await axios.post(`${SIZE_STORE_ENDPOINT}/diff`, params);
} catch (error) {
console.error('error: while publishing diff', error);
}
}
}
async function publishSizes(size,filename) {
// TODO: read allowed branch from configuration
if (process.env.NODE_ENV !=='test' && ci && event == 'push' && branch==='master') {
try {
const params = { ci,repo, branch, sha, size,filename };
await axios.post(`${SIZE_STORE_ENDPOINT}/size`, params);
}
catch (error) {
console.error('error: while publishing sizes', error);
}
}

async function publishSizes(size, filename) {
// TODO: read allowed branch from configuration
if (
process.env.NODE_ENV !== 'test' &&
ci &&
event == 'push' &&
branch === 'master'
) {
try {
const params = { ci, repo, branch, sha, size, filename };
await axios.post(`${SIZE_STORE_ENDPOINT}/size`, params);
} catch (error) {
console.error('error: while publishing sizes', error);
}
}
}
module.exports = { publishSizes,publishDiff };

module.exports = { publishSizes, publishDiff };
54 changes: 30 additions & 24 deletions rollup-plugin-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ function bundleSize(_options) {
initialSizes = await load(_path);
outputSizes(bundle).catch(console.error);
}
function filterFiles(files) {
const isMatched = minimatch.filter(pattern);
const isExcluded = exclude ? minimatch.filter(exclude) : () => false;
return files.filter(file => isMatched(file) && !isExcluded(file));

async function load(outputPath) {
const data = await readFromDisk(filename);
if (data.length) {
const [{ files }] = data;
return toFileMap(files);
}
return getSizes(outputPath);
}

async function readFromDisk(filename) {
try {
if (!options.writeFile) {
Expand All @@ -91,6 +96,25 @@ function bundleSize(_options) {
return [];
}
}

async function getSizes(cwd) {
const files = await glob(pattern, { cwd, ignore: exclude });

const sizes = await Promise.all(
filterFiles(files).map(file =>
compressionSize.file(path.join(cwd, file)).catch(() => null)
)
);

return toMap(files, sizes);
}

function filterFiles(files) {
const isMatched = minimatch.filter(pattern);
const isExcluded = exclude ? minimatch.filter(exclude) : () => false;
return files.filter(file => isMatched(file) && !isExcluded(file));
}

async function writeToDisk(filename, stats) {
if (
process.env.NODE_ENV === 'production' &&
Expand All @@ -105,6 +129,7 @@ function bundleSize(_options) {
options.publish && (await publishSizes(data, options.filename));
}
}

async function save(files) {
const stats = {
timestamp: Date.now(),
Expand All @@ -119,14 +144,7 @@ function bundleSize(_options) {
options.save && (await options.save(stats));
await writeToDisk(filename, stats);
}
async function load(outputPath) {
const data = await readFromDisk(filename);
if (data.length) {
const [{ files }] = data;
return toFileMap(files);
}
return getSizes(outputPath);
}

async function outputSizes(assets) {
const sizesBefore = await Promise.resolve(initialSizes);
const assetNames = filterFiles(Object.keys(assets));
Expand Down Expand Up @@ -192,18 +210,6 @@ function bundleSize(_options) {
output && console.log('\n' + output);
}

async function getSizes(cwd) {
const files = await glob(pattern, { cwd, ignore: exclude });

const sizes = await Promise.all(
filterFiles(files).map(file =>
compressionSize.file(path.join(cwd, file)).catch(() => null)
)
);

return toMap(files, sizes);
}

return {
name: 'rollup-plugin-size',
generateBundle
Expand Down