|
| 1 | +import { gzipSync } from 'zlib' |
| 2 | +import { promisify } from 'util' |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | +import axios from 'axios' |
| 6 | +import gzipSize from 'gzip-size' |
| 7 | +import brotliSize from 'brotli-size' |
| 8 | +import { detectProvider } from './provider' |
| 9 | +import { getToken, getApiUrl } from './config' |
| 10 | + |
| 11 | +const readFile = promisify(fs.readFile) |
| 12 | + |
| 13 | +async function sizeAssets(stats) { |
| 14 | + return Promise.all( |
| 15 | + stats.assets.map(async asset => { |
| 16 | + const fullPath = path.join(stats.outputPath, asset.name) |
| 17 | + const buffer = await readFile(fullPath) |
| 18 | + return { |
| 19 | + ...asset, |
| 20 | + gzipSize: await gzipSize(buffer), |
| 21 | + brotliSize: await brotliSize(buffer), |
| 22 | + } |
| 23 | + }), |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +class BundleAnalyzer { |
| 28 | + constructor({ token } = {}) { |
| 29 | + this.token = getToken(token) |
| 30 | + this.metadata = detectProvider() |
| 31 | + } |
| 32 | + |
| 33 | + apply(compiler) { |
| 34 | + const isProductionLikeMode = |
| 35 | + compiler.options.mode === 'production' || !compiler.options.mode |
| 36 | + if (!isProductionLikeMode) { |
| 37 | + return |
| 38 | + } |
| 39 | + const { token, metadata } = this |
| 40 | + compiler.hooks.afterEmit.tapAsync( |
| 41 | + '@bundle-analyzer/webpack-plugin', |
| 42 | + (hookCompiler, callback) => { |
| 43 | + const apiUrl = getApiUrl() |
| 44 | + const stats = hookCompiler.getStats().toJson({ |
| 45 | + maxModules: Infinity, |
| 46 | + source: false, |
| 47 | + }) |
| 48 | + |
| 49 | + async function sendBuild() { |
| 50 | + const assets = await sizeAssets(stats) |
| 51 | + |
| 52 | + const { data: build } = await axios.post(`${apiUrl}/builds`, { |
| 53 | + token, |
| 54 | + branch: metadata.branch, |
| 55 | + commit: metadata.commit, |
| 56 | + providerMetadata: metadata, |
| 57 | + stats: { |
| 58 | + assets, |
| 59 | + chunksNumber: stats.chunks.length, |
| 60 | + modulesNumber: stats.modules.length, |
| 61 | + assetsNumber: stats.assets.length, |
| 62 | + }, |
| 63 | + }) |
| 64 | + |
| 65 | + await axios.request({ |
| 66 | + method: 'put', |
| 67 | + url: build.webpackStatsPutUrl, |
| 68 | + data: gzipSync(Buffer.from(JSON.stringify(stats))), |
| 69 | + headers: { |
| 70 | + 'content-encoding': 'gzip', |
| 71 | + }, |
| 72 | + maxContentLength: 30 * 1024 * 1024, |
| 73 | + }) |
| 74 | + |
| 75 | + await axios.post(`${apiUrl}/builds/${build.id}/start`, { |
| 76 | + token, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + sendBuild() |
| 81 | + .then(() => callback()) |
| 82 | + .catch(error => { |
| 83 | + function getMessage() { |
| 84 | + if ( |
| 85 | + error.response && |
| 86 | + error.response.data && |
| 87 | + error.response.data.error && |
| 88 | + error.response.data.error.message |
| 89 | + ) { |
| 90 | + return error.response.data.error.message |
| 91 | + } |
| 92 | + return error.message |
| 93 | + } |
| 94 | + callback(new Error(`Bundle Analyzer - ${getMessage()}`)) |
| 95 | + }) |
| 96 | + }, |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = BundleAnalyzer |
| 102 | +module.exports.default = BundleAnalyzer |
0 commit comments