Thanks to visit codestin.com
Credit goes to github.com

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

Commit 7d66601

Browse files
committed
feat: support config file
1 parent baa5913 commit 7d66601

File tree

10 files changed

+64
-20
lines changed

10 files changed

+64
-20
lines changed

packages/cli/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@ npm install --save-dev @bundle-analyzer/cli
1010

1111
## Usage
1212

13-
```js
14-
webpack --json | bundle-analyzer --token <your-token>
13+
```
14+
Usage: bundle-analyzer [options] <stats>
15+
16+
Options:
17+
-V, --version output the version number
18+
--token <repository-token> specify the repository token
19+
--config-file <file> specify a custom config file
20+
-h, --help output usage information
21+
22+
Examples:
23+
webpack --json | bundle-analyzer --token "your-repository-token"
24+
cat webpack-stats.json | bundle-analyzer --token "your-repository-token"
1525
```
1626

1727
## Complete documentation

packages/cli/src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ program
77
.version(pkg.version)
88
.usage('[options] <stats>')
99
.option('--token <repository-token>', 'specify the repository token')
10+
.option('--config-file <file>', 'specify a custom config file')
1011

1112
program.on('--help', () => {
1213
console.log(`
13-
Example:
14+
Examples:
1415
webpack --json | bundle-analyzer --token "your-repository-token"
1516
cat webpack-stats.json | bundle-analyzer --token "your-repository-token"
1617
`)
@@ -35,7 +36,11 @@ async function readStdin() {
3536
async function run() {
3637
const rawStats = await readStdin()
3738
const stats = JSON.parse(rawStats)
38-
await uploadStats({ webpackStats: stats, token: program.token })
39+
await uploadStats({
40+
webpackStats: stats,
41+
token: program.token,
42+
configFile: program.configFile,
43+
})
3944
}
4045

4146
run().catch(error => {

packages/core/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ You can specify the token using options or environment variable `BUNDLE_ANALYZER
3434
3535
Custom filesystem.
3636
37+
### context
38+
39+
Search the config file from this repository.
40+
41+
### configFile
42+
43+
Specify a custom config file.
44+
3745
## Complete documentation
3846
3947
👉 [See full documentation](https://docs.bundle-analyzer.com/)

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"axios": "^0.19.0",
2626
"brotli-size": "^4.0.0",
27+
"cosmiconfig": "^5.2.1",
2728
"gzip-size": "^5.1.1",
2829
"omit-deep": "^0.3.0"
2930
}

packages/core/src/config.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
import cosmiconfig from 'cosmiconfig'
2+
3+
const explorer = cosmiconfig('bundle-analyzer', {
4+
sync: true,
5+
cache: true,
6+
rcExtensions: true,
7+
})
8+
9+
export async function resolveConfig(searchFrom, configFile) {
10+
if (configFile == null) {
11+
const result = await explorer.search(searchFrom)
12+
return result ? result.config : null
13+
}
14+
const result = await explorer.load(configFile)
15+
return result ? result.config : null
16+
}
17+
18+
export async function resolveConfigFile(filePath) {
19+
const result = await explorer.search(filePath)
20+
return result ? result.filepath : null
21+
}
22+
123
export function getToken(configToken) {
224
const token = configToken || process.env.BUNDLE_ANALYZER_TOKEN
325
if (!token) {

packages/core/src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import gzipSize from 'gzip-size'
77
import brotliSize from 'brotli-size'
88
import omitDeep from 'omit-deep'
99
import { detectProvider } from './provider'
10-
import { getToken, getApiUrl } from './config'
10+
import { resolveConfig, getToken, getApiUrl } from './config'
1111

1212
const gzip = promisify(zlib.gzip)
1313

@@ -39,6 +39,8 @@ function getErrorMessage(error) {
3939
}
4040

4141
export async function uploadStats({
42+
context = process.cwd(),
43+
configFile,
4244
webpackStats,
4345
token: optionToken,
4446
fileSystem,
@@ -48,6 +50,7 @@ export async function uploadStats({
4850
const apiUrl = getApiUrl()
4951
const metadata = detectProvider()
5052
const stats = omitDeep(webpackStats, 'source')
53+
const config = await resolveConfig(context, configFile)
5154
const assets = await sizeAssets(stats, { fileSystem })
5255

5356
const { data: bundle } = await axios.post(`${apiUrl}/bundles`, {
@@ -79,6 +82,7 @@ export async function uploadStats({
7982
branch: metadata.branch,
8083
commit: metadata.commit,
8184
providerMetadata: metadata,
85+
config,
8286
})
8387
} catch (error) {
8488
throw new Error(getErrorMessage(error))

packages/core/src/index.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe('#uploadStats', () => {
8989
build_url: 'deploy-url',
9090
pr: 'review-id',
9191
},
92+
config: null,
9293
})
9394
.reply(201)
9495

packages/webpack-plugin/README.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
path: __dirname + '/dist',
2222
filename: 'index_bundle.js',
2323
},
24-
plugins: [new BundleAnalyzerPlugin()],
24+
plugins: [new BundleAnalyzerPlugin({ token: 'Your repository token' })],
2525
}
2626
```
2727

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

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

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

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-
```
36+
You can specify a custom configuration file.
4637

4738
## Complete documentation
4839

packages/webpack-plugin/src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { uploadStats } from '@bundle-analyzer/core'
22

33
class BundleAnalyzer {
4-
constructor({ token } = {}) {
4+
constructor({ token, configFile } = {}) {
55
this.token = token
6+
this.configFile = configFile
67
}
78

89
apply(compiler) {
910
const isProductionLikeMode =
1011
compiler.options.mode === 'production' || !compiler.options.mode
1112
if (!isProductionLikeMode) return
1213

13-
const { token } = this
14+
const { token, configFile } = this
1415

1516
compiler.hooks.afterEmit.tapAsync(
1617
'@bundle-analyzer/webpack-plugin',
@@ -23,6 +24,7 @@ class BundleAnalyzer {
2324
uploadStats({
2425
webpackStats: stats,
2526
token,
27+
configFile,
2628
fileSystem: compiler.outputFileSystem,
2729
})
2830
.then(() => callback())

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ [email protected], core-util-is@~1.0.0:
31443144
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
31453145
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
31463146

3147-
cosmiconfig@^5.1.0:
3147+
cosmiconfig@^5.1.0, cosmiconfig@^5.2.1:
31483148
version "5.2.1"
31493149
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.2.1.tgz#040f726809c591e77a17c0a3626ca45b4f168b1a"
31503150
integrity sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA==

0 commit comments

Comments
 (0)