report-size.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. name: Report size
  2. on:
  3. workflow_run:
  4. workflows: ["Read size"]
  5. types:
  6. - completed
  7. # This workflow needs to be run with "pull-requests: write" permissions to
  8. # be able to comment on the pull request. We can't checkout the PR code
  9. # in this workflow.
  10. # Reference:
  11. # https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
  12. permissions:
  13. pull-requests: write
  14. jobs:
  15. report-size:
  16. name: Comment on PR
  17. runs-on: ubuntu-latest
  18. if: github.event.workflow_run.event == 'pull_request' &&
  19. github.event.workflow_run.conclusion == 'success'
  20. steps:
  21. - name: Log GitHub context
  22. env:
  23. GITHUB_CONTEXT: ${{ toJson(github) }}
  24. run: echo "$GITHUB_CONTEXT"
  25. # Using actions/download-artifact doesn't work here
  26. # https://github.com/actions/download-artifact/issues/60
  27. - name: Download artifact
  28. uses: actions/github-script@v6
  29. id: download-artifact
  30. with:
  31. result-encoding: string
  32. script: |
  33. const fs = require('fs/promises');
  34. const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
  35. owner: context.repo.owner,
  36. repo: context.repo.repo,
  37. run_id: context.payload.workflow_run.id,
  38. });
  39. const matchArtifact = artifacts.data.artifacts.find((artifact) => artifact.name === 'sizes');
  40. const download = await github.rest.actions.downloadArtifact({
  41. owner: context.repo.owner,
  42. repo: context.repo.repo,
  43. artifact_id: matchArtifact.id,
  44. archive_format: 'zip',
  45. });
  46. await fs.writeFile('sizes.zip', Buffer.from(download.data));
  47. await exec.exec('unzip sizes.zip');
  48. const json = await fs.readFile('sizes.json', 'utf8');
  49. return json;
  50. # This runs on the base branch of the PR, meaning "dev"
  51. - name: Git checkout
  52. uses: actions/checkout@v3
  53. - name: Install Node
  54. uses: actions/setup-node@v3
  55. with:
  56. node-version: 18
  57. cache: 'npm'
  58. - name: Install dependencies
  59. run: npm ci
  60. - name: Build
  61. run: npm run build-module
  62. - name: === Test tree-shaking ===
  63. run: npm run test-treeshake
  64. - name: Read sizes
  65. id: read-size
  66. run: |
  67. FILESIZE_BASE=$(stat --format=%s test/treeshake/three.module.min.js)
  68. TREESHAKEN_BASE=$(stat --format=%s test/treeshake/index.bundle.min.js)
  69. # log to console
  70. echo "FILESIZE_BASE=$FILESIZE_BASE"
  71. echo "TREESHAKEN_BASE=$TREESHAKEN_BASE"
  72. echo "FILESIZE_BASE=$FILESIZE_BASE" >> $GITHUB_OUTPUT
  73. echo "TREESHAKEN_BASE=$TREESHAKEN_BASE" >> $GITHUB_OUTPUT
  74. - name: Format sizes
  75. id: format
  76. # It's important these are passed as env variables.
  77. # https://securitylab.github.com/research/github-actions-untrusted-input/
  78. env:
  79. FILESIZE: ${{ fromJSON(steps.download-artifact.outputs.result).filesize }}
  80. FILESIZE_GZIP: ${{ fromJSON(steps.download-artifact.outputs.result).gzip }}
  81. FILESIZE_BASE: ${{ steps.read-size.outputs.FILESIZE_BASE }}
  82. TREESHAKEN: ${{ fromJSON(steps.download-artifact.outputs.result).treeshaken }}
  83. TREESHAKEN_GZIP: ${{ fromJSON(steps.download-artifact.outputs.result).treeshakenGzip }}
  84. TREESHAKEN_BASE: ${{ steps.read-size.outputs.TREESHAKEN_BASE }}
  85. run: |
  86. FILESIZE_FORM=$(node ./test/treeshake/utils/format-size.js "$FILESIZE")
  87. FILESIZE_GZIP_FORM=$(node ./test/treeshake/utils/format-size.js "$FILESIZE_GZIP")
  88. FILESIZE_DIFF=$(node ./test/treeshake/utils/format-diff.js "$FILESIZE" "$FILESIZE_BASE")
  89. TREESHAKEN_FORM=$(node ./test/treeshake/utils/format-size.js "$TREESHAKEN")
  90. TREESHAKEN_GZIP_FORM=$(node ./test/treeshake/utils/format-size.js "$TREESHAKEN_GZIP")
  91. TREESHAKEN_DIFF=$(node ./test/treeshake/utils/format-diff.js "$TREESHAKEN" "$TREESHAKEN_BASE")
  92. echo "FILESIZE=$FILESIZE_FORM" >> $GITHUB_OUTPUT
  93. echo "FILESIZE_GZIP=$FILESIZE_GZIP_FORM" >> $GITHUB_OUTPUT
  94. echo "FILESIZE_DIFF=$FILESIZE_DIFF" >> $GITHUB_OUTPUT
  95. echo "TREESHAKEN=$TREESHAKEN_FORM" >> $GITHUB_OUTPUT
  96. echo "TREESHAKEN_GZIP=$TREESHAKEN_GZIP_FORM" >> $GITHUB_OUTPUT
  97. echo "TREESHAKEN_DIFF=$TREESHAKEN_DIFF" >> $GITHUB_OUTPUT
  98. - name: Find existing comment
  99. uses: peter-evans/find-comment@v2
  100. id: find-comment
  101. with:
  102. issue-number: ${{ fromJSON(steps.download-artifact.outputs.result).pr }}
  103. comment-author: 'github-actions[bot]'
  104. body-includes: Bundle size
  105. - name: Comment on PR
  106. uses: peter-evans/create-or-update-comment@v2
  107. with:
  108. issue-number: ${{ fromJSON(steps.download-artifact.outputs.result).pr }}
  109. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  110. edit-mode: replace
  111. body: |
  112. ### 📦 Bundle size
  113. _Full ESM build, minified and gzipped._
  114. | Filesize | Gzipped | Diff from `${{ github.ref_name }}` |
  115. |----------|---------|------|
  116. | ${{ steps.format.outputs.FILESIZE }} | ${{ steps.format.outputs.FILESIZE_GZIP }} | ${{ steps.format.outputs.FILESIZE_DIFF }} |
  117. ### 🌳 Bundle size after tree-shaking
  118. _Minimal build including a renderer, camera, empty scene, and dependencies._
  119. | Filesize | Gzipped | Diff from `${{ github.ref_name }}` |
  120. |----------|---------|------|
  121. | ${{ steps.format.outputs.TREESHAKEN }} | ${{ steps.format.outputs.TREESHAKEN_GZIP }} | ${{ steps.format.outputs.TREESHAKEN_DIFF }} |