Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add config file options to bundle-analyzer cli, export types #174

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/eleven-deers-divide.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---
"@codecov/bundle-analyzer": minor
"@codecov/bundler-plugin-core": minor
"@codecov/nextjs-webpack-plugin": minor
"@codecov/nuxt-plugin": minor
"@codecov/remix-vite-plugin": minor
"@codecov/rollup-plugin": minor
"@codecov/solidstart-plugin": minor
"@codecov/sveltekit-plugin": minor
"@codecov/vite-plugin": minor
"@codecov/webpack-plugin": minor
---

Add support for setups without a standard bundler through new Bundle Analyzer library and CLI
7 changes: 7 additions & 0 deletions examples/bundle-analyzer-cli/baconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"gitService": "github",
"oidc": {
"useGitHubOIDC": false
},
"ignorePatterns": ["*.map"]
}
2 changes: 1 addition & 1 deletion examples/bundle-analyzer-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"type": "module",
"scripts": {
"build": "rollup -c && pnpm bundle-analyzer ./dist --bundle-name=@codecov/example-bundle-analyzer-cli --upload-token=$BUNDLE_ANALYZER_UPLOAD_TOKEN"
"build": "rollup -c && pnpm bundle-analyzer ./dist --bundle-name=@codecov/example-bundle-analyzer-cli --upload-token=$BUNDLE_ANALYZER_UPLOAD_TOKEN --config-file=./baconfig.json"
},
"devDependencies": {
"@codecov/bundle-analyzer": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion examples/bundle-analyzer-lib-cjs/analyze.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createAndUploadReport } = require("@codecov/bundle-analyzer");

const buildDirs = ["../../../examples/bundle-analyzer-lib-cjs/example-dist"];
const buildDirs = ["../../examples/bundle-analyzer-lib-cjs/example-dist"];

const coreOpts = {
dryRun: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createAndUploadReport } from "@codecov/bundle-analyzer";
import {
BundleAnalyzerOptions,
createAndUploadReport,
Options,
} from "@codecov/bundle-analyzer";

const buildDirs = ["../../../examples/bundle-analyzer-lib-esm/example-dist"];
const buildDirs = ["../../examples/bundle-analyzer-lib-esm/example-dist"];

const coreOpts = {
const coreOpts: Options = {
dryRun: false,
uploadToken: process.env.BUNDLE_ANALYZER_UPLOAD_TOKEN,
retryCount: 3,
Expand All @@ -12,7 +16,7 @@ const coreOpts = {
debug: true,
};

const bundleAnalyzerOpts = {
const bundleAnalyzerOpts: BundleAnalyzerOptions = {
beforeReportUpload: async (original) => original,
ignorePatterns: ["*.map"],
normalizeAssetsPattern: "[name]-[hash].js",
Expand Down
4 changes: 2 additions & 2 deletions examples/bundle-analyzer-lib-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "@codecov/example-bundle-analyzer-library-mjs",
"version": "1.0.0",
"private": true,
"main": "analyze.js",
"main": "analyze.ts",
"type": "module",
"scripts": {
"build": "node analyze.js"
"build": "npx tsx analyze.ts"
},
"devDependencies": {
"@codecov/bundle-analyzer": "workspace:^"
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/test-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx",
},
"jsxImportSource": "hono/jsx"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createAndUploadReport } from "@codecov/bundle-analyzer";

const buildDirs = [
"../../../integration-tests/test-apps/bundle-analyzer/dotenv-vercel/dist",
];
const buildDirs = ["./dist"];

const apiUrl = process.env.API_URL || "https://api.codecov.io";

Expand Down
29 changes: 23 additions & 6 deletions packages/bundle-analyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

# Codecov Bundle Analyzer

> [!WARNING]
> This bundle-analyzer package is subject to change.
>
> An importable library + CLI for Codecov bundle analysis support.
>
The bundle analyzer is an importable library and CLI for Codecov bundle analysis. It is designed for users who operate without a standard bundler that is otherwise supported by Codecov's bundler plugins.

> [!NOTE]
> The plugin does not support code coverage, see our [docs](https://docs.codecov.com/docs/quick-start) to set up coverage today!

## Installation
Expand All @@ -33,7 +31,7 @@ Using pnpm:
pnpm add @codecov/bundle-analyzer --save-dev
```

## Example
## Example - Custom Script

This example shows how the package can be imported as a library.

Expand Down Expand Up @@ -68,12 +66,31 @@ createAndUploadReport(buildDirs, coreOpts, bundleAnalyzerOpts)
);
```

## Example - CLI

This example shows how the package can be used as a CLI.

```
npx @codecov/bundle-analyzer ./dist --bundle-name=my-identifier --upload-token=abcd --dry-run
```

[OPTIONAL] - A config file can be passed for any extended options matching those described [here](https://codecov.github.io/codecov-javascript-bundler-plugins/interfaces/_codecov_bundler_plugin_core.Options.html).

```
npx @codecov/bundle-analyzer ./dist --bundle-name=my-identifier --upload-token=abcd --dry-run --config-file=./config.json
```

```
// config.json
{
"gitService": "github",
"oidc": {
"useGitHubOIDC": false
}
}

```

## Supported Platforms

The CLI tool supports the following operating systems:
Expand Down
6 changes: 4 additions & 2 deletions packages/bundle-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "@codecov/bundle-analyzer",
"version": "0.0.1-beta.12",
"version": "1.0.1",
"description": "Official Codecov Bundle Analyzer",
"author": "Codecov",
"license": "MIT",
"keywords": [
"Codecov",
"bundle-analyzer",
"bundler",
"analyzer"
"analyzer",
"cli",
"script"
],
"type": "module",
"bin": {
Expand Down
50 changes: 1 addition & 49 deletions packages/bundle-analyzer/src/assets.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
describe,
it,
expect,
vi,
beforeEach,
type Mock,
afterEach,
} from "vitest";
import { describe, it, expect, vi, beforeEach, type Mock } from "vitest";
import path from "node:path";
import fs from "node:fs/promises";
import { getAssets, getAsset, listChildFilePaths } from "./assets";
Expand All @@ -15,7 +7,6 @@ import {
getCompressedSize,
normalizePath,
} from "@codecov/bundler-plugin-core";
import { fileURLToPath } from "url";

vi.mock("node:fs/promises");
vi.mock("@codecov/bundler-plugin-core", () => ({
Expand Down Expand Up @@ -255,42 +246,3 @@ describe("getAllAssets", () => {
]);
});
});

describe("Module System Compatibility", () => {
let fileName: string;
let __dirname: string;

const setCommonJSContext = () => {
// @ts-expect-error - ignore ts error for module cleanup
global.module = { exports: {} };
fileName = __filename;
__dirname = path.dirname(fileName);
};

const setESModulesContext = () => {
// @ts-expect-error - ignore ts error for module cleanup
delete global.module;
fileName = fileURLToPath(import.meta.url);
__dirname = path.dirname(fileName);
};

afterEach(() => {
// clean up the global context after each test
// @ts-expect-error - ignore ts error for module cleanup
delete global.module;
});

it("should correctly set fileName and __dirname in CommonJS environment", () => {
setCommonJSContext();

expect(fileName).toBe(__filename);
expect(__dirname).toBe(path.dirname(__filename));
});

it("should correctly set fileName and __dirname in ESModules environment", () => {
setESModulesContext();

expect(fileName).toBe(fileURLToPath(import.meta.url));
expect(__dirname).toBe(path.dirname(fileURLToPath(import.meta.url)));
});
});
15 changes: 1 addition & 14 deletions packages/bundle-analyzer/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ import {
normalizePath,
type Asset,
} from "@codecov/bundler-plugin-core";
import { fileURLToPath } from "node:url";
import micromatch from "micromatch";

let fileName: string;
let __dirname: string;
const isCommonJSEnvironment =
typeof module !== "undefined" && module.exports !== undefined;
if (isCommonJSEnvironment) {
fileName = __filename;
__dirname = path.dirname(fileName);
} else {
fileName = fileURLToPath(import.meta.url);
__dirname = path.dirname(fileName);
}

/* getAssets gets assets from the starting paths to include in a bundle stats report */
export const getAssets = async (
buildDirectoryPaths: string[],
Expand All @@ -28,7 +15,7 @@ export const getAssets = async (
): Promise<Asset[]> => {
const allAssets = await Promise.all(
buildDirectoryPaths.map(async (buildDirectoryPath) => {
const absoluteAssetsDir = path.resolve(__dirname, buildDirectoryPath);
const absoluteAssetsDir = path.resolve(buildDirectoryPath);
const files = await listChildFilePaths(absoluteAssetsDir);

// apply filtering only if ignorePatterns contains patterns
Expand Down
Loading
Loading