# conditional-aggregate-webpack-plugin

> Delays webpack aggregateTimeout triggered watch-build until some custom condition meets.

Latest version **1.0.2** (published 2023-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install conditional-aggregate-webpack-plugin
pnpm add conditional-aggregate-webpack-plugin
yarn add conditional-aggregate-webpack-plugin
bun add conditional-aggregate-webpack-plugin
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-10-11 |
| First published | 2021-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Dimi Kot |
| Maintainers | dimikot |
| Keywords | webpack, aggregateTimeout, watch |

## Links

- npm: https://www.npmjs.com/package/conditional-aggregate-webpack-plugin
- Repository: https://github.com/dimikot/conditional-aggregate-webpack-plugin
- Homepage: https://github.com/dimikot/conditional-aggregate-webpack-plugin#readme
- Issues: https://github.com/dimikot/conditional-aggregate-webpack-plugin/issues
- npm.io page: https://npm.io/package/conditional-aggregate-webpack-plugin

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-10-11
- 1.0.1 — 2023-10-11
- 1.0.0 — 2021-08-20

## README

# conditional-aggregate-webpack-plugin: delays webpack aggregateTimeout triggered watch-build until some custom condition meets

This webpack plugin allows to add a custom callback which, if it returns `true`, allows webpack to proceed with the incremental build in `--watch` mode. Until the callback returns `true`, all the changes are accumulated, and webpack is not wasting CPU; instead, it waits until the condition is met.

## Motivation

Sometimes webpack is used to bundle artifacts generated by other tools. An example is TypeScript's `tsc` in a large monorepo with cross-dependencies between packages, where `tsc` generates `dist/` folders with `*.{js,map,d.ts}` files, and then webpack uses these `dist/*.js` and `dist/*.map` to build `public/` and not waste time on dependent packages rebuild from their sources.

The problem is that, when the monorepo is large, there can be multiple `tsc --watch` processes running concurrently (one per each monorepo package), and the set of `tsc` builds does not settle immediately after e.g. `git pull` affecting multiple packages. For instance, if there are `A -> B -> C` packages dependencies in the monorepo, they all start watch-building after the pull, and until e.g. `C` is fully built, `A` and `B` builds may fail. Until they are all built successfully, there is no sense to let webpack to run the incremental build, because (in a large monorepo) it will just waste CPU and flood console with useless error messages.

This plugin allows to resolve this usecase. It allows to verify that all `tsc` sub-projects are built successfully without errors. If they aren't, then there is no sense in triggering a webpack watch-build (it burns lots of CPU cycles and freezes the laptop in a large monorepo project, especially when there are multiple webpack built packages involved); instead, it's better to wait until all monorepo packages are watch-built by `tsc` successfully.

## Example

The plugin is useful together with `tsc-watch` tool which allows to hook into different `tsc` build stages:

```bash
# Running for each package in the monorepo:
tsc-watch \
  --onCompilationStarted 'bash -c "echo building > dist/status"' \
  --onSuccess 'bash -c "echo success > dist/status"' \
  --onFailure 'bash -c "echo building > dist/status"'
```

Once this command is running, there will be an always up-to-date `dist/status` file reflecting the current state of `tsc` build. Then, you can configure `ConditionalAggregateWebpackPlugin` to read these files and unfreeze webpack incremental build only when ALL the status files contain "success":

```ts
// webpack.config.ts
import { readFileSync } from "fs";
import { basename, dirname } from "path";
import ConditionalAggregateWebpackPlugin from "conditional-aggregate-webpack-plugin";

export default function() {
  return {
    // ...,
    plugins: [
      new ConditionalAggregateWebpackPlugin({
        recheckInterval: 200, // how often do we call the condition function
        condition: () => {
          const statuses: string[] = [];
          for (const file of glob.sync(`${__dirname}/packages/*/dist/status`)) {
            const content = readFileSync(file).toString();
            if (!content.match(/success/)) {
              statuses.push(
                basename(dirname(dirname(file))) + ":" + content.trim()
              );
            }
          }

          return statuses.length === 0 ? true : ["waiting for", ...statuses];
        },
      }),
      // ...
    ]
  }
}
```

---
_Source: https://npm.io/package/conditional-aggregate-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
