diff --git a/.github/workflows/ci-nx.yml b/.github/workflows/ci-nx.yml index fe9a00fb6c..afd3770870 100644 --- a/.github/workflows/ci-nx.yml +++ b/.github/workflows/ci-nx.yml @@ -80,13 +80,6 @@ jobs: package_manager: pnpm build_script: build:nx - # TODO: Move the cache saving to a job on the main branch - - name: Save Nx cache - uses: actions/cache/save@d4323d4df104b026a6aa633fdb11d772146be0bf # v4 - with: - path: .nx/cache - key: nx-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml', 'nx.json') }} - build: name: Build strategy: diff --git a/.github/workflows/publish-v2.yml b/.github/workflows/publish-v2.yml index dc06939cc8..6dfc7555b4 100644 --- a/.github/workflows/publish-v2.yml +++ b/.github/workflows/publish-v2.yml @@ -177,6 +177,14 @@ jobs: env: S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }} + # Report bundle size to Amplitude + - name: Report bundle size to Amplitude + continue-on-error: true + env: + AMPLITUDE_API_KEY: ${{ secrets.AMPLITUDE_API_KEY }} + run: | + node ./scripts/report-bundle-size.js + prerelease: name: Prerelease feature branch runs-on: ubuntu-latest diff --git a/.size-limit.js b/.size-limit.js index 428207e854..7fb5ef8ceb 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -1,19 +1,25 @@ const limits = [ { // analytics-browser bundle - path: './packages/analytics-browser/lib/scripts/amplitude-min.js.gz', + packageJsonPath: './packages/analytics-browser/package.json', + path: `./packages/analytics-browser/lib/scripts/amplitude-min.js.gz`, + name: 'analytics-browser.min.js.gz', limit: '65kb', brotli: false, }, { // session-replay standalone bundle - path: './packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz', + packageJsonPath: './packages/session-replay-browser/package.json', + path: `./packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz`, + name: 'session-replay-browser.min.js.gz', limit: '150kb', brotli: false, }, { // unified SDK bundle - path: './packages/unified/lib/scripts/amplitude-min.umd.js.gz', + packageJsonPath: './packages/unified/package.json', + path: `./packages/unified/lib/scripts/amplitude-min.umd.js.gz`, + name: 'unified.min.umd.js.gz', limit: '225kb', brotli: false, }, diff --git a/scripts/report-bundle-size.js b/scripts/report-bundle-size.js new file mode 100644 index 0000000000..22304e748b --- /dev/null +++ b/scripts/report-bundle-size.js @@ -0,0 +1,45 @@ +const sizeLimits = require('../.size-limit.js'); +const fs = require('fs'); +const path = require('path'); + +function getBundleSizeEvent(filepath, size, filename, packageJsonPath) { + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + const packageName = packageJson.name; + return { + user_id: 'sdk-bundle-size-reporter', + event_type: 'SDK Bundle Size', + event_properties: { + bundle_size: size, + bundle_path: filepath, + filename: filename || path.basename(filepath), + package_name: packageName, + package_version: packageJson.version, + }, + }; +} + +const events = []; +for (const limit of sizeLimits) { + const size = fs.statSync(limit.path).size; + events.push(getBundleSizeEvent(limit.path, size, limit.name, limit.packageJsonPath)); +} + +fetch('https://api.amplitude.com/2/httpapi', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + api_key: process.env.AMPLITUDE_API_KEY, + events: events, + }), +}).then(response => { + console.log(response.status); + response.json().then(data => { + console.log(data); + process.exit(response.status >= 400 ? 1 : 0); + }); +}).catch(error => { + console.error('Error reporting bundle size to Amplitude:', error); + process.exit(1); +}); \ No newline at end of file