diff --git a/packages/cli/README.md b/packages/cli/README.md index 867cc1f..c7286a0 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -10,8 +10,18 @@ npm install --save-dev @bundle-analyzer/cli ## Usage -```js -webpack --json | bundle-analyzer --token +``` +Usage: bundle-analyzer [options] + +Options: + -V, --version output the version number + --token specify the repository token + --config-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 diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index bd97dc7..c2b5b8a 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -7,10 +7,11 @@ program .version(pkg.version) .usage('[options] ') .option('--token ', 'specify the repository token') + .option('--config-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" `) @@ -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 => { diff --git a/packages/core/README.md b/packages/core/README.md index 6595535..2311897 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -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/) diff --git a/packages/core/package.json b/packages/core/package.json index ceb0731..e0dbdf3 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" } diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 614e912..ca9aec8 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -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) { diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 69495f0..1927569 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -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) @@ -39,6 +39,8 @@ function getErrorMessage(error) { } export async function uploadStats({ + context = process.cwd(), + configFile, webpackStats, token: optionToken, fileSystem, @@ -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`, { @@ -79,6 +82,7 @@ export async function uploadStats({ branch: metadata.branch, commit: metadata.commit, providerMetadata: metadata, + config, }) } catch (error) { throw new Error(getErrorMessage(error)) diff --git a/packages/core/src/index.test.js b/packages/core/src/index.test.js index f2a421a..14a2190 100644 --- a/packages/core/src/index.test.js +++ b/packages/core/src/index.test.js @@ -89,6 +89,7 @@ describe('#uploadStats', () => { build_url: 'deploy-url', pr: 'review-id', }, + config: null, }) .reply(201) diff --git a/packages/webpack-plugin/README.md b/packages/webpack-plugin/README.md index 487a46a..14e78a9 100644 --- a/packages/webpack-plugin/README.md +++ b/packages/webpack-plugin/README.md @@ -21,7 +21,7 @@ module.exports = { path: __dirname + '/dist', filename: 'index_bundle.js', }, - plugins: [new BundleAnalyzerPlugin()], + plugins: [new BundleAnalyzerPlugin({ token: 'Your repository token' })], } ``` @@ -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 diff --git a/packages/webpack-plugin/src/index.js b/packages/webpack-plugin/src/index.js index 42f3aec..199f824 100644 --- a/packages/webpack-plugin/src/index.js +++ b/packages/webpack-plugin/src/index.js @@ -1,8 +1,9 @@ import { uploadStats } from '@bundle-analyzer/core' class BundleAnalyzer { - constructor({ token } = {}) { + constructor({ token, configFile } = {}) { this.token = token + this.configFile = configFile } apply(compiler) { @@ -10,7 +11,7 @@ class BundleAnalyzer { 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', @@ -23,6 +24,7 @@ class BundleAnalyzer { uploadStats({ webpackStats: stats, token, + configFile, fileSystem: compiler.outputFileSystem, }) .then(() => callback()) diff --git a/yarn.lock b/yarn.lock index 04343bf..2ed8c98 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3144,7 +3144,7 @@ core-util-is@1.0.2, 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==