Skip to content

Commit

Permalink
Merge branch 'master' into support-push
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jun 22, 2021
2 parents e889273 + 1704d91 commit a8de1b9
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on: [pull_request_target, push]
on: [pull_request, push]

jobs:
build_test:
Expand Down
43 changes: 39 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
```
### Customizing the Build
Expand Down Expand Up @@ -66,10 +64,39 @@ jobs:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
+ build-script: "ci"
```

#### Clean up state between builds

For repositories or custom monorepo setups where files are modified in ways that are not reset by `npm ci && npm run build`, it may be necessary to define a custom "clean" script. This script resets any file modifications after the upstream (`target`) build ends and your PR code (`HEAD`) is checked out, but before installation of npm dependencies for `HEAD`:

```diff
name: Compressed Size
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
+ clean-script: "clean"
```

```jsonc
// package.json
{
"scripts": {
// example - a simple nested node_modules setup:
"postinstall": "cd packages && npm i",
// between the two builds, we need to delete the inner node_modules:
"clean": "rm -rf packages/node_modules"
}
}
```

### Customizing the list of files

`compressed-size-action` defaults to tracking the size of all JavaScript files within `dist/` directories - anywhere in your repository, not just at the root. You can change the list of files to be tracked and reported using the `pattern` and `exclude` options, both of which are [minimatch patterns](https://github.com/motemen/minimatch-cheat-sheet/blob/master/README.md):
Expand All @@ -84,7 +111,6 @@ jobs:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
+ pattern: "./build-output/**/*.{js,css,html,json}"
+ exclude: "{./build-output/manifest.json,**/*.map,**/node_modules/**}"
```
Expand Down Expand Up @@ -127,3 +153,12 @@ By default, a file that's been changed by a single byte will be reported as chan
```

In the above example, a file with a delta of less than 100 bytes will be reported as unchanged.

### Compression

By default, files are compared after gzip compression, but it's possible to use other compression algorithms (`gzip/brotli/none`) or disable the compression.

```yaml
compression: 'none'
```

5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ branding:
inputs:
repo-token:
description: 'The GITHUB_TOKEN secret'
required: true
required: false
default: ${{ github.token }}
clean-script:
description: 'An npm-script that cleans/resets state between branch builds'
build-script:
description: 'The npm-script to run that builds your project'
default: 'build'
Expand Down
2 changes: 1 addition & 1 deletion index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compressed-size-action",
"version": "2.1.0",
"version": "2.2.0",
"main": "index.js",
"scripts": {
"build": "microbundle -f cjs --define 'navigator={}' --compress --no-sourcemap --target node src/index.js",
Expand Down
25 changes: 22 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async function run(octokit, context, token) {
const buildScript = getInput('build-script') || 'build';
const cwd = process.cwd();

const yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
const packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));
let yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
let packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));

let npm = `npm`;
let installScript = `npm install`;
Expand Down Expand Up @@ -107,7 +107,26 @@ async function run(octokit, context, token) {
}
endGroup();

const cleanScript = getInput('clean-script');
if (cleanScript) {
startGroup(`[base] Cleanup via ${npm} run ${cleanScript}`);
await exec(`${npm} run ${cleanScript}`);
endGroup();
}

startGroup(`[base] Install Dependencies`);

yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));

if (yarnLock) {
installScript = npm = `yarn --frozen-lockfile`;
}
else if (packageLock) {
installScript = `npm ci`;
}

console.log(`Installing using ${installScript}`)
await exec(installScript);
endGroup();

Expand Down Expand Up @@ -261,7 +280,7 @@ async function createCheck(octokit, context) {

(async () => {
try {
const token = getInput('repo-token', { required: true });
const token = getInput('repo-token');
const octokit = getOctokit(token);
await run(octokit, context, token);
} catch (e) {
Expand Down
13 changes: 11 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function iconForDifference(delta, originalSize) {

/**
* Create a Markdown table from text rows
* @param {string[]} rows
* @param {string[][]} rows
*/
function markdownTable(rows) {
if (rows.length == 0) {
Expand All @@ -94,7 +94,16 @@ function markdownTable(rows) {
}

const [firstRow] = rows;
const columnLength = firstRow.length;
let columnLength = firstRow.length;

// Hide `Change` column if they are all `0 B`
if (columnLength === 3 && rows.every(columns => columns[2] === '0 B')) {
columnLength -= 1;
for (const columns of rows) {
columns.pop();
}
}

if (columnLength === 0) {
return '';
}
Expand Down
30 changes: 15 additions & 15 deletions tests/__snapshots__/utils.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ exports[`diffTable 1`] = `
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
| Filename | Size | Change |
| :--- | :---: | :---: |
| \`three.js\` | 300 B | 0 B |
| Filename | Size |
| :--- | :---: |
| \`three.js\` | 300 B |
</details>
Expand All @@ -31,9 +31,9 @@ exports[`diffTable 2`] = `
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
| Filename | Size | Change |
| :--- | :---: | :---: |
| \`three.js\` | 300 B | 0 B |
| Filename | Size |
| :--- | :---: |
| \`three.js\` | 300 B |
</details>
Expand Down Expand Up @@ -96,12 +96,12 @@ exports[`diffTable 6`] = `
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
| Filename | Size | Change |
| :--- | :---: | :---: |
| \`one.js\` | 5 kB | 0 B |
| \`two.js\` | 5 kB | 0 B |
| \`three.js\` | 300 B | 0 B |
| \`four.js\` | 4.5 kB | 0 B |
| Filename | Size |
| :--- | :---: |
| \`one.js\` | 5 kB |
| \`two.js\` | 5 kB |
| \`three.js\` | 300 B |
| \`four.js\` | 4.5 kB |
</details>
Expand All @@ -117,9 +117,9 @@ exports[`diffTable 7`] = `
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
| Filename | Size | Change |
| :--- | :---: | :---: |
| \`three.js\` | 300 B | 0 B |
| Filename | Size |
| :--- | :---: |
| \`three.js\` | 300 B |
</details>
Expand Down

0 comments on commit a8de1b9

Please sign in to comment.