Skip to content

Commit 6ff0693

Browse files
committed
Order code
1 parent a2721fd commit 6ff0693

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

publish-size.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
const { repo, sha, event, branch, pull_request_number, ci } = require('ci-env');
1+
const { repo, sha, event, branch, ci } = require('ci-env');
22
const axios = require('axios');
3-
const SIZE_STORE_ENDPOINT = process.env.SIZE_STORE_ENDPOINT || 'https://size-plugin-store.now.sh' ;
3+
const SIZE_STORE_ENDPOINT =
4+
process.env.SIZE_STORE_ENDPOINT || 'https://size-plugin-store.now.sh';
45

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

7-
async function publishDiff(diff,filename) {
8-
if (process.env.NODE_ENV !=='test' && ci && event == 'pull_request') {
9-
try {
10-
const params = { ci,repo, branch, sha, diff,filename };
11-
await axios.post(`${SIZE_STORE_ENDPOINT}/diff`, params);
12-
}
13-
catch (error) {
14-
console.error('error: while publishing diff', error);
15-
}
16-
}
8+
async function publishDiff(diff, filename) {
9+
if (process.env.NODE_ENV !== 'test' && ci && event == 'pull_request') {
10+
try {
11+
const params = { ci, repo, branch, sha, diff, filename };
12+
await axios.post(`${SIZE_STORE_ENDPOINT}/diff`, params);
13+
} catch (error) {
14+
console.error('error: while publishing diff', error);
15+
}
16+
}
1717
}
18-
async function publishSizes(size,filename) {
19-
// TODO: read allowed branch from configuration
20-
if (process.env.NODE_ENV !=='test' && ci && event == 'push' && branch==='master') {
21-
try {
22-
const params = { ci,repo, branch, sha, size,filename };
23-
await axios.post(`${SIZE_STORE_ENDPOINT}/size`, params);
24-
}
25-
catch (error) {
26-
console.error('error: while publishing sizes', error);
27-
}
28-
}
18+
19+
async function publishSizes(size, filename) {
20+
// TODO: read allowed branch from configuration
21+
if (
22+
process.env.NODE_ENV !== 'test' &&
23+
ci &&
24+
event == 'push' &&
25+
branch === 'master'
26+
) {
27+
try {
28+
const params = { ci, repo, branch, sha, size, filename };
29+
await axios.post(`${SIZE_STORE_ENDPOINT}/size`, params);
30+
} catch (error) {
31+
console.error('error: while publishing sizes', error);
32+
}
33+
}
2934
}
30-
module.exports = { publishSizes,publishDiff };
35+
36+
module.exports = { publishSizes, publishDiff };

rollup-plugin-size.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const defaults = {
4343
brotli: false,
4444
pattern: '**/*.{mjs,js,jsx,css,html}',
4545
exclude: undefined,
46-
writeFile:true,
47-
publish:false,
46+
writeFile: true,
47+
publish: false,
4848
columnWidth: 20
4949
};
5050
/**
@@ -71,14 +71,19 @@ function bundleSize(_options) {
7171
initialSizes = await load(path.resolve(outputOptions.dir));
7272
outputSizes(bundle).catch(console.error);
7373
}
74-
function filterFiles(files) {
75-
const isMatched = minimatch.filter(pattern);
76-
const isExcluded = exclude ? minimatch.filter(exclude) : () => false;
77-
return files.filter(file => isMatched(file) && !isExcluded(file));
74+
75+
async function load(outputPath) {
76+
const data = await readFromDisk(filename);
77+
if (data.length) {
78+
const [{ files }] = data;
79+
return toFileMap(files);
80+
}
81+
return getSizes(outputPath);
7882
}
83+
7984
async function readFromDisk(filename) {
8085
try {
81-
if(options.writeFile){
86+
if (options.writeFile) {
8287
return [];
8388
}
8489
await fs.ensureFile(filename);
@@ -88,20 +93,40 @@ function bundleSize(_options) {
8893
return [];
8994
}
9095
}
96+
97+
async function getSizes(cwd) {
98+
const files = await glob(pattern, { cwd, ignore: exclude });
99+
100+
const sizes = await Promise.all(
101+
filterFiles(files).map(file =>
102+
compressionSize.file(path.join(cwd, file)).catch(() => null)
103+
)
104+
);
105+
106+
return toMap(files, sizes);
107+
}
108+
109+
function filterFiles(files) {
110+
const isMatched = minimatch.filter(pattern);
111+
const isExcluded = exclude ? minimatch.filter(exclude) : () => false;
112+
return files.filter(file => isMatched(file) && !isExcluded(file));
113+
}
114+
91115
async function writeToDisk(filename, stats) {
92116
if (
93117
process.env.NODE_ENV === 'production' &&
94118
stats.files.some(file => file.diff !== 0)
95119
) {
96120
const data = await readFromDisk(filename);
97121
data.unshift(stats);
98-
if(options.writeFile){
122+
if (options.writeFile) {
99123
await fs.ensureFile(filename);
100124
await fs.writeJSON(filename, data);
101125
}
102-
options.publish && await publishSizes(data, options.filename);
126+
options.publish && (await publishSizes(data, options.filename));
103127
}
104128
}
129+
105130
async function save(files) {
106131
const stats = {
107132
timestamp: Date.now(),
@@ -112,19 +137,11 @@ function bundleSize(_options) {
112137
diff: file.size - file.sizeBefore
113138
}))
114139
};
115-
options.publish && await publishDiff(stats, options.filename);
140+
options.publish && (await publishDiff(stats, options.filename));
116141
options.save && (await options.save(stats));
117142
await writeToDisk(filename, stats);
118143
}
119-
async function load(outputPath) {
120144

121-
const data = await readFromDisk(filename);
122-
if (data.length) {
123-
const [{ files }] = data;
124-
return toFileMap(files);
125-
}
126-
return getSizes(outputPath);
127-
}
128145
async function outputSizes(assets) {
129146
const sizesBefore = await Promise.resolve(initialSizes);
130147
const assetNames = filterFiles(Object.keys(assets));
@@ -190,18 +207,6 @@ function bundleSize(_options) {
190207
output && console.log('\n' + output);
191208
}
192209

193-
async function getSizes(cwd) {
194-
const files = await glob(pattern, { cwd, ignore: exclude });
195-
196-
const sizes = await Promise.all(
197-
filterFiles(files).map(file =>
198-
compressionSize.file(path.join(cwd, file)).catch(() => null)
199-
)
200-
);
201-
202-
return toMap(files, sizes);
203-
}
204-
205210
return {
206211
name: 'rollup-plugin-size',
207212
generateBundle

0 commit comments

Comments
 (0)