Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Commit 76ec7b3

Browse files
committed
feat: first stable version
1 parent 73be354 commit 76ec7b3

38 files changed

+7481
-0
lines changed

.babelrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"node": "8"
8+
},
9+
"loose": true
10+
}
11+
]
12+
],
13+
"plugins": ["@babel/plugin-proposal-class-properties"]
14+
}

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

.eslintrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"root": true,
3+
"extends": ["smooth", "smooth/project"]
4+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
package.json
3+
CHANGELOG.md

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"semi": false
5+
}

.travis.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
3+
node_js:
4+
- 8
5+
- 10
6+
- 12
7+
8+
script:
9+
- yarn ci
10+
11+
notifications:
12+
email: false
13+
14+
cache:
15+
yarn: true
16+
directories:
17+
- 'node_modules'

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Bundle analyzer JS clients
2+
3+
This repository contains all JavaScript clients for [Bundle Analyzer](https://www.bundle-analyzer.com).
4+
5+
## Complete documentation
6+
7+
👉 [See full documentation](https://docs.bundle-analyzer.com/)

babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
4+
const config = fs.readFileSync(path.join(__dirname, '.babelrc'))
5+
6+
module.exports = JSON.parse(config)

lerna.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "3.3.0",
3+
"packages": ["packages/*"],
4+
"version": "1.0.0",
5+
"npmClient": "yarn",
6+
"useWorkspaces": true
7+
}

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"private": true,
3+
"workspaces": [
4+
"packages/*"
5+
],
6+
"scripts": {
7+
"build": "lerna run build",
8+
"ci": "yarn build && yarn lint",
9+
"format": "prettier --write \"**/*.{js,json,md}\"",
10+
"lint": "eslint .",
11+
"release": "lerna publish --conventional-commits && conventional-github-releaser --preset angular"
12+
},
13+
"devDependencies": {
14+
"@babel/cli": "^7.4.4",
15+
"@babel/core": "^7.4.5",
16+
"@babel/plugin-proposal-class-properties": "^7.4.4",
17+
"@babel/preset-env": "^7.4.5",
18+
"babel-eslint": "^10.0.1",
19+
"conventional-github-releaser": "^3.1.3",
20+
"eslint": "^6.0.1",
21+
"eslint-config-airbnb": "^18.0.1",
22+
"eslint-config-prettier": "^6.2.0",
23+
"eslint-config-smooth": "^2.1.1",
24+
"eslint-plugin-import": "^2.18.2",
25+
"eslint-plugin-jsx-a11y": "^6.2.3",
26+
"eslint-plugin-react": "^7.14.3",
27+
"eslint-plugin-react-hooks": "^2.0.1",
28+
"lerna": "^3.14.1",
29+
"prettier": "^1.18.2"
30+
}
31+
}

packages/webpack-plugin/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
!/lib/**/*.js
3+
*.test.js

packages/webpack-plugin/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# @bundle-analyzer/webpack-plugin
2+
3+
Official webpack plugin compatible with [Bundle Analyzer service](https://www.bundle-analyzer.com).
4+
5+
## Install
6+
7+
```
8+
npm install --save-dev @bundle-analyzer/webpack-plugin
9+
```
10+
11+
## Usage
12+
13+
**webpack.config.js**
14+
15+
```js
16+
const BundleAnalyzerPlugin = require('@bundle-analyzer/webpack-plugin')
17+
18+
module.exports = {
19+
entry: 'index.js',
20+
output: {
21+
path: __dirname + '/dist',
22+
filename: 'index_bundle.js',
23+
},
24+
plugins: [new BundleAnalyzerPlugin()],
25+
}
26+
```
27+
28+
## Options
29+
30+
### token
31+
32+
You can specify the token using options or environment variable `BUNDLE_ANALYZER_TOKEN`.
33+
34+
```js
35+
const BundleAnalyzerPlugin = require('@bundle-analyzer/webpack-plugin')
36+
37+
module.exports = {
38+
entry: 'index.js',
39+
output: {
40+
path: __dirname + '/dist',
41+
filename: 'index_bundle.js',
42+
},
43+
plugins: [new BundleAnalyzerPlugin({ token: 'Your repository token' })],
44+
}
45+
```
46+
47+
## Complete documentation
48+
49+
👉 [See full documentation](https://docs.bundle-analyzer.com/)
50+
51+
## License
52+
53+
MIT

packages/webpack-plugin/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@bundle-analyzer/webpack-plugin",
3+
"description": "Bundle Analyzer webpack plugin.",
4+
"version": "1.0.0",
5+
"main": "lib/index.js",
6+
"repository": "https://github.com/smooth-code/bundle-analyzer-javascript/tree/master/packages/webpack-plugin",
7+
"author": "Greg Bergé <[email protected]>",
8+
"publishConfig": {
9+
"access": "public"
10+
},
11+
"keywords": [
12+
"webpack",
13+
"plugin",
14+
"bundle-analyzer",
15+
"bundlesize"
16+
],
17+
"engines": {
18+
"node": ">=8"
19+
},
20+
"license": "MIT",
21+
"scripts": {
22+
"prebuild": "rm -rf lib/",
23+
"build": "babel --config-file ../../babel.config.js -d lib --ignore \"**/*.test.js\" src",
24+
"prepublishOnly": "yarn run build"
25+
},
26+
"dependencies": {
27+
"axios": "^0.19.0",
28+
"brotli-size": "^4.0.0",
29+
"gzip-size": "^5.1.1"
30+
}
31+
}

packages/webpack-plugin/src/config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function getToken(configToken) {
2+
const token = configToken || process.env.BUNDLE_ANALYZER_TOKEN
3+
if (!token) {
4+
throw new Error(
5+
`Token not found, please specify a token using BUNDLE_ANALYZER_TOKEN env variable`,
6+
)
7+
}
8+
return token
9+
}
10+
11+
export function getApiUrl() {
12+
return (
13+
process.env.BUNDLE_ANALYZER_API_URL || 'https://api.bundle-analyzer.com'
14+
)
15+
}

packages/webpack-plugin/src/git.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { execSync } from 'child_process'
2+
3+
export function branch() {
4+
return execSync('git rev-parse --abbrev-ref HEAD || hg branch')
5+
.toString()
6+
.trim()
7+
}
8+
9+
export function head() {
10+
return execSync("git log -1 --pretty=%H || hg id -i --debug | tr -d '+'")
11+
.toString()
12+
.trim()
13+
}

packages/webpack-plugin/src/index.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-console */
2+
import services from './services'
3+
import * as localGit from './services/localGit'
4+
5+
export function detectProvider() {
6+
const service = services.find(service => service.detect())
7+
if (service) {
8+
const config = service.config()
9+
// console.log(`${config.name} detected`)
10+
return config
11+
}
12+
13+
// console.log(`No CI service detected, use local git`)
14+
const config = localGit.config()
15+
if (!config) {
16+
throw new Error('Unknown CI service provider. Unable to upload stats.')
17+
}
18+
return config
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function detect() {
2+
return !!process.env.APPVEYOR
3+
}
4+
5+
export function config() {
6+
return {
7+
name: 'App Veyor',
8+
service: 'appveyor',
9+
commit: process.env.APPVEYOR_REPO_COMMIT,
10+
branch: process.env.APPVEYOR_REPO_BRANCH,
11+
pr: process.env.APPVEYOR_PULL_REQUEST_NUMBER,
12+
job: `${process.env.APPVEYOR_ACCOUNT_NAME}/${process.env.APPVEYOR_PROJECT_SLUG}/${process.env.APPVEYOR_BUILD_VERSION}`,
13+
build: process.env.APPVEYOR_JOB_ID,
14+
slug: process.env.APPVEYOR_REPO_NAME,
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function detect() {
2+
return !!process.env.TF_BUILD
3+
}
4+
5+
export function config() {
6+
return {
7+
name: 'Azure Pipelines',
8+
service: 'azure_pipelines',
9+
commit: process.env.BUILD_SOURCEVERSION,
10+
branch: process.env.BUILD_SOURCEBRANCH,
11+
pr: process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER,
12+
job: process.env.SYSTEM_JOBID,
13+
build: process.env.BUILD_BUILDID,
14+
build_url: `${process.env.SYSTEM_TEAMFOUNDATIONSERVERURI +
15+
process.env.SYSTEM_TEAMPROJECT}/_build/results?buildId=${
16+
process.env.BUILD_BUILDID
17+
}`,
18+
slug: process.env.BUILD_REPOSITORY_ID,
19+
}
20+
}

0 commit comments

Comments
 (0)