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

Commit

Permalink
Merge pull request #3 from smooth-code/config-file
Browse files Browse the repository at this point in the history
feat: support config file
  • Loading branch information
gregberge authored Sep 16, 2019
2 parents baa5913 + 7d66601 commit 285769b
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 20 deletions.
14 changes: 12 additions & 2 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ npm install --save-dev @bundle-analyzer/cli

## Usage

```js
webpack --json | bundle-analyzer --token <your-token>
```
Usage: bundle-analyzer [options] <stats>
Options:
-V, --version output the version number
--token <repository-token> specify the repository token
--config-file <file> specify a custom config file
-h, --help output usage information
Examples:
webpack --json | bundle-analyzer --token "your-repository-token"
cat webpack-stats.json | bundle-analyzer --token "your-repository-token"
```

## Complete documentation
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ program
.version(pkg.version)
.usage('[options] <stats>')
.option('--token <repository-token>', 'specify the repository token')
.option('--config-file <file>', 'specify a custom config file')

program.on('--help', () => {
console.log(`
Example:
Examples:
webpack --json | bundle-analyzer --token "your-repository-token"
cat webpack-stats.json | bundle-analyzer --token "your-repository-token"
`)
Expand All @@ -35,7 +36,11 @@ async function readStdin() {
async function run() {
const rawStats = await readStdin()
const stats = JSON.parse(rawStats)
await uploadStats({ webpackStats: stats, token: program.token })
await uploadStats({
webpackStats: stats,
token: program.token,
configFile: program.configFile,
})
}

run().catch(error => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ You can specify the token using options or environment variable `BUNDLE_ANALYZER
Custom filesystem.
### context
Search the config file from this repository.
### configFile
Specify a custom config file.
## Complete documentation
👉 [See full documentation](https://docs.bundle-analyzer.com/)
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"axios": "^0.19.0",
"brotli-size": "^4.0.0",
"cosmiconfig": "^5.2.1",
"gzip-size": "^5.1.1",
"omit-deep": "^0.3.0"
}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import cosmiconfig from 'cosmiconfig'

const explorer = cosmiconfig('bundle-analyzer', {
sync: true,
cache: true,
rcExtensions: true,
})

export async function resolveConfig(searchFrom, configFile) {
if (configFile == null) {
const result = await explorer.search(searchFrom)
return result ? result.config : null
}
const result = await explorer.load(configFile)
return result ? result.config : null
}

export async function resolveConfigFile(filePath) {
const result = await explorer.search(filePath)
return result ? result.filepath : null
}

export function getToken(configToken) {
const token = configToken || process.env.BUNDLE_ANALYZER_TOKEN
if (!token) {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import gzipSize from 'gzip-size'
import brotliSize from 'brotli-size'
import omitDeep from 'omit-deep'
import { detectProvider } from './provider'
import { getToken, getApiUrl } from './config'
import { resolveConfig, getToken, getApiUrl } from './config'

const gzip = promisify(zlib.gzip)

Expand Down Expand Up @@ -39,6 +39,8 @@ function getErrorMessage(error) {
}

export async function uploadStats({
context = process.cwd(),
configFile,
webpackStats,
token: optionToken,
fileSystem,
Expand All @@ -48,6 +50,7 @@ export async function uploadStats({
const apiUrl = getApiUrl()
const metadata = detectProvider()
const stats = omitDeep(webpackStats, 'source')
const config = await resolveConfig(context, configFile)
const assets = await sizeAssets(stats, { fileSystem })

const { data: bundle } = await axios.post(`${apiUrl}/bundles`, {
Expand Down Expand Up @@ -79,6 +82,7 @@ export async function uploadStats({
branch: metadata.branch,
commit: metadata.commit,
providerMetadata: metadata,
config,
})
} catch (error) {
throw new Error(getErrorMessage(error))
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('#uploadStats', () => {
build_url: 'deploy-url',
pr: 'review-id',
},
config: null,
})
.reply(201)

Expand Down
15 changes: 3 additions & 12 deletions packages/webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
path: __dirname + '/dist',
filename: 'index_bundle.js',
},
plugins: [new BundleAnalyzerPlugin()],
plugins: [new BundleAnalyzerPlugin({ token: 'Your repository token' })],
}
```

Expand All @@ -31,18 +31,9 @@ module.exports = {

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

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

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

## Complete documentation

Expand Down
6 changes: 4 additions & 2 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { uploadStats } from '@bundle-analyzer/core'

class BundleAnalyzer {
constructor({ token } = {}) {
constructor({ token, configFile } = {}) {
this.token = token
this.configFile = configFile
}

apply(compiler) {
const isProductionLikeMode =
compiler.options.mode === 'production' || !compiler.options.mode
if (!isProductionLikeMode) return

const { token } = this
const { token, configFile } = this

compiler.hooks.afterEmit.tapAsync(
'@bundle-analyzer/webpack-plugin',
Expand All @@ -23,6 +24,7 @@ class BundleAnalyzer {
uploadStats({
webpackStats: stats,
token,
configFile,
fileSystem: compiler.outputFileSystem,
})
.then(() => callback())
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ [email protected], core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

cosmiconfig@^5.1.0:
cosmiconfig@^5.1.0, cosmiconfig@^5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.2.1.tgz#040f726809c591e77a17c0a3626ca45b4f168b1a"
integrity sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA==
Expand Down

0 comments on commit 285769b

Please sign in to comment.