summaryrefslogtreecommitdiff
path: root/scripts/bundle_size_review
blob: 5067c3c3f2c5bb93623e4f37fc0ab83e019ee4bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

#
# # How does this work in general?
#
# 1. We run webpack in a production like mode and enable the BundleAnalyzerPlugin
# 2. The Plugin builds a index.html for human consumption _and_ a stats.json
# 3. This script creates a smaller analysis.json from the gargantuan stats.json
# 4. In Merge Requests:
#    - compare that smaller to analysis.json to the one from the base commit on master
#    - report the comparison results via danger

source scripts/utils.sh

# For now we only want bundle-size-review to run in CI
# Maybe we could create a "local mode"
if [[ -z "${CI:-}" ]]; then
  echo 'Not running in a CI context, skipping bundle analysis'
  exit "0"
fi

# Get the _current_ commit sha
if [[ -z "${CI_MERGE_REQUEST_IID:-}" ]]; then
  echo 'Not in a merge request, setting COMMIT_SHA to $CI_COMMIT_SHA'
  COMMIT_SHA="${CI_COMMIT_SHA}"
else
  echo 'In a merge request, setting COMMIT_SHA to $CI_MERGE_REQUEST_SOURCE_BRANCH_SHA'
  COMMIT_SHA="${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}"
fi

# Create output directory
mkdir -p bundle-size-review

# Running webpack
export WEBPACK_REPORT="true"
run_timed_command "yarn run webpack-prod > bundle-size-review/webpack-output.log"

# Copy results from stats plugin
cp webpack-report/index.html bundle-size-review/bundle-report.html

# Run comparison in danger
if [[ -z "${DANGER_GITLAB_API_TOKEN:-}" ]]; then
  echo 'No Danger token available, skipping bundle analysis'
  exit "0"
fi

# TODO: Make this a dependency of GitLab itself after a proper release
yarn global add https://gitlab.com/gitlab-org/frontend/playground/webpack-memory-metrics.git

# Create smaller analysis.json
run_timed_command "webpack-entry-point-analyser --from-file ./webpack-report/stats.json --json ./bundle-size-review/analysis.json --sha ${COMMIT_SHA}"
rm -rf webpack-report

if [[ -z "${CI_MERGE_REQUEST_IID:-}" ]]; then
  echo 'Not in a merge request, skipping comparison'
  exit "0"
fi

# Run comparison
run_timed_command "webpack-compare-reports --job ${CI_JOB_ID} --to-file ./bundle-size-review/analysis.json --html ./bundle-size-review/comparison.html --markdown ./bundle-size-review/comparison.md"

# Execute danger
danger_id=$(echo -n "${DANGER_GITLAB_API_TOKEN}" | md5sum | awk '{print $1}' | cut -c5-10)
run_timed_command "danger --dangerfile=danger/Dangerfile-bundle_size --fail-on-errors=true --verbose --danger_id=bundle-size-review-${danger_id}"

exit "0"