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

Commit

Permalink
feat: first stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 10, 2019
1 parent 73be354 commit 76ec7b3
Show file tree
Hide file tree
Showing 38 changed files with 7,481 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "8"
},
"loose": true
}
]
],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": ["smooth", "smooth/project"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
package.json
CHANGELOG.md
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"semi": false
}
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js

node_js:
- 8
- 10
- 12

script:
- yarn ci

notifications:
email: false

cache:
yarn: true
directories:
- 'node_modules'
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Bundle analyzer JS clients

This repository contains all JavaScript clients for [Bundle Analyzer](https://www.bundle-analyzer.com).

## Complete documentation

👉 [See full documentation](https://docs.bundle-analyzer.com/)
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path')
const fs = require('fs')

const config = fs.readFileSync(path.join(__dirname, '.babelrc'))

module.exports = JSON.parse(config)
7 changes: 7 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lerna": "3.3.0",
"packages": ["packages/*"],
"version": "1.0.0",
"npmClient": "yarn",
"useWorkspaces": true
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"private": true,
"workspaces": [
"packages/*"
],
"scripts": {
"build": "lerna run build",
"ci": "yarn build && yarn lint",
"format": "prettier --write \"**/*.{js,json,md}\"",
"lint": "eslint .",
"release": "lerna publish --conventional-commits && conventional-github-releaser --preset angular"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"babel-eslint": "^10.0.1",
"conventional-github-releaser": "^3.1.3",
"eslint": "^6.0.1",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.2.0",
"eslint-config-smooth": "^2.1.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.0.1",
"lerna": "^3.14.1",
"prettier": "^1.18.2"
}
}
3 changes: 3 additions & 0 deletions packages/webpack-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
!/lib/**/*.js
*.test.js
53 changes: 53 additions & 0 deletions packages/webpack-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# @bundle-analyzer/webpack-plugin

Official webpack plugin compatible with [Bundle Analyzer service](https://www.bundle-analyzer.com).

## Install

```
npm install --save-dev @bundle-analyzer/webpack-plugin
```

## Usage

**webpack.config.js**

```js
const BundleAnalyzerPlugin = require('@bundle-analyzer/webpack-plugin')

module.exports = {
entry: 'index.js',
output: {
path: __dirname + '/dist',
filename: 'index_bundle.js',
},
plugins: [new BundleAnalyzerPlugin()],
}
```

## Options

### token

You can specify the token using options or environment variable `BUNDLE_ANALYZER_TOKEN`.

```js
const BundleAnalyzerPlugin = require('@bundle-analyzer/webpack-plugin')

module.exports = {
entry: 'index.js',
output: {
path: __dirname + '/dist',
filename: 'index_bundle.js',
},
plugins: [new BundleAnalyzerPlugin({ token: 'Your repository token' })],
}
```

## Complete documentation

👉 [See full documentation](https://docs.bundle-analyzer.com/)

## License

MIT
31 changes: 31 additions & 0 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@bundle-analyzer/webpack-plugin",
"description": "Bundle Analyzer webpack plugin.",
"version": "1.0.0",
"main": "lib/index.js",
"repository": "https://github.com/smooth-code/bundle-analyzer-javascript/tree/master/packages/webpack-plugin",
"author": "Greg Bergé <berge.greg@gmail.com>",
"publishConfig": {
"access": "public"
},
"keywords": [
"webpack",
"plugin",
"bundle-analyzer",
"bundlesize"
],
"engines": {
"node": ">=8"
},
"license": "MIT",
"scripts": {
"prebuild": "rm -rf lib/",
"build": "babel --config-file ../../babel.config.js -d lib --ignore \"**/*.test.js\" src",
"prepublishOnly": "yarn run build"
},
"dependencies": {
"axios": "^0.19.0",
"brotli-size": "^4.0.0",
"gzip-size": "^5.1.1"
}
}
15 changes: 15 additions & 0 deletions packages/webpack-plugin/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getToken(configToken) {
const token = configToken || process.env.BUNDLE_ANALYZER_TOKEN
if (!token) {
throw new Error(
`Token not found, please specify a token using BUNDLE_ANALYZER_TOKEN env variable`,
)
}
return token
}

export function getApiUrl() {
return (
process.env.BUNDLE_ANALYZER_API_URL || 'https://api.bundle-analyzer.com'
)
}
13 changes: 13 additions & 0 deletions packages/webpack-plugin/src/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { execSync } from 'child_process'

export function branch() {
return execSync('git rev-parse --abbrev-ref HEAD || hg branch')
.toString()
.trim()
}

export function head() {
return execSync("git log -1 --pretty=%H || hg id -i --debug | tr -d '+'")
.toString()
.trim()
}
102 changes: 102 additions & 0 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { gzipSync } from 'zlib'
import { promisify } from 'util'
import fs from 'fs'
import path from 'path'
import axios from 'axios'
import gzipSize from 'gzip-size'
import brotliSize from 'brotli-size'
import { detectProvider } from './provider'
import { getToken, getApiUrl } from './config'

const readFile = promisify(fs.readFile)

async function sizeAssets(stats) {
return Promise.all(
stats.assets.map(async asset => {
const fullPath = path.join(stats.outputPath, asset.name)
const buffer = await readFile(fullPath)
return {
...asset,
gzipSize: await gzipSize(buffer),
brotliSize: await brotliSize(buffer),
}
}),
)
}

class BundleAnalyzer {
constructor({ token } = {}) {
this.token = getToken(token)
this.metadata = detectProvider()
}

apply(compiler) {
const isProductionLikeMode =
compiler.options.mode === 'production' || !compiler.options.mode
if (!isProductionLikeMode) {
return
}
const { token, metadata } = this
compiler.hooks.afterEmit.tapAsync(
'@bundle-analyzer/webpack-plugin',
(hookCompiler, callback) => {
const apiUrl = getApiUrl()
const stats = hookCompiler.getStats().toJson({
maxModules: Infinity,
source: false,
})

async function sendBuild() {
const assets = await sizeAssets(stats)

const { data: build } = await axios.post(`${apiUrl}/builds`, {
token,
branch: metadata.branch,
commit: metadata.commit,
providerMetadata: metadata,
stats: {
assets,
chunksNumber: stats.chunks.length,
modulesNumber: stats.modules.length,
assetsNumber: stats.assets.length,
},
})

await axios.request({
method: 'put',
url: build.webpackStatsPutUrl,
data: gzipSync(Buffer.from(JSON.stringify(stats))),
headers: {
'content-encoding': 'gzip',
},
maxContentLength: 30 * 1024 * 1024,
})

await axios.post(`${apiUrl}/builds/${build.id}/start`, {
token,
})
}

sendBuild()
.then(() => callback())
.catch(error => {
function getMessage() {
if (
error.response &&
error.response.data &&
error.response.data.error &&
error.response.data.error.message
) {
return error.response.data.error.message
}
return error.message
}
callback(new Error(`Bundle Analyzer - ${getMessage()}`))
})
},
)
}
}

module.exports = BundleAnalyzer
module.exports.default = BundleAnalyzer
19 changes: 19 additions & 0 deletions packages/webpack-plugin/src/provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-console */
import services from './services'
import * as localGit from './services/localGit'

export function detectProvider() {
const service = services.find(service => service.detect())
if (service) {
const config = service.config()
// console.log(`${config.name} detected`)
return config
}

// console.log(`No CI service detected, use local git`)
const config = localGit.config()
if (!config) {
throw new Error('Unknown CI service provider. Unable to upload stats.')
}
return config
}
16 changes: 16 additions & 0 deletions packages/webpack-plugin/src/services/appveyor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function detect() {
return !!process.env.APPVEYOR
}

export function config() {
return {
name: 'App Veyor',
service: 'appveyor',
commit: process.env.APPVEYOR_REPO_COMMIT,
branch: process.env.APPVEYOR_REPO_BRANCH,
pr: process.env.APPVEYOR_PULL_REQUEST_NUMBER,
job: `${process.env.APPVEYOR_ACCOUNT_NAME}/${process.env.APPVEYOR_PROJECT_SLUG}/${process.env.APPVEYOR_BUILD_VERSION}`,
build: process.env.APPVEYOR_JOB_ID,
slug: process.env.APPVEYOR_REPO_NAME,
}
}
20 changes: 20 additions & 0 deletions packages/webpack-plugin/src/services/azurePipelines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function detect() {
return !!process.env.TF_BUILD
}

export function config() {
return {
name: 'Azure Pipelines',
service: 'azure_pipelines',
commit: process.env.BUILD_SOURCEVERSION,
branch: process.env.BUILD_SOURCEBRANCH,
pr: process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER,
job: process.env.SYSTEM_JOBID,
build: process.env.BUILD_BUILDID,
build_url: `${process.env.SYSTEM_TEAMFOUNDATIONSERVERURI +
process.env.SYSTEM_TEAMPROJECT}/_build/results?buildId=${
process.env.BUILD_BUILDID
}`,
slug: process.env.BUILD_REPOSITORY_ID,
}
}
Loading

0 comments on commit 76ec7b3

Please sign in to comment.