Skip to content
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
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ packages/plugins/analytics @yoannmoin

# Custom Hooks
packages/plugins/custom-hooks @yoannmoinet

# True End
packages/plugins/true-end @yoannmoinet
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ export type FactoryMeta = {
export type HookFn<T extends Array<any>> = (...args: T) => void;
export type AsyncHookFn<T extends Array<any>> = (...args: T) => Promise<void> | void;
export type CustomHooks = {
asyncTrueEnd?: () => Promise<void> | void;
cwd?: HookFn<[string]>;
init?: HookFn<[GlobalContext]>;
buildReport?: HookFn<[BuildReport]>;
bundlerReport?: HookFn<[BundlerReport]>;
git?: AsyncHookFn<[RepositoryData]>;
syncTrueEnd?: () => void;
};

export type PluginOptions = Assign<
Expand Down
8 changes: 8 additions & 0 deletions packages/factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This is used to aggregate all the plugins and expose them to the bundler.
- [Custom Hooks](#custom-hooks)
- [Git](#git)
- [Injection](#injection)
- [True End](#true-end)
- [Logger](#logger)
- [Time Logger](#time-logger)
- [Options](#options)
Expand Down Expand Up @@ -82,6 +83,13 @@ Most of the time they will interact via the global context.

#### [📝 Full documentation ➡️](/packages/plugins/injection#readme)


### True End
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the name a bit weird (but I don't have a better suggestion :/)

what about onFullBuild?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find onFullBuild a bit misleading too, for instance in rollup the concept of buildEnd happens before the writeBundle, as-in, rollup (and vite) separate both the build from the write process.

Can you elaborate on what you find weird with "true end"?
Maybe we could go with just end?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"true" doesn't really have a definition. And it sounds a bit more like something you'd say when speaking to someone vs something defined in a spec / technical document

Copy link
Member Author

@yoannmoinet yoannmoinet May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some discussion offline, it's difficult to find a term that would really define this hook, cross bundler, so for now, until we get better consensus, we'll stick with true-end.


> A custom hook for the true end of a build, cross bundlers.

#### [📝 Full documentation ➡️](/packages/plugins/true-end#readme)

<!-- #internal-plugins-list -->

## Logger
Expand Down
1 change: 1 addition & 0 deletions packages/factory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@dd/internal-custom-hooks-plugin": "workspace:*",
"@dd/internal-git-plugin": "workspace:*",
"@dd/internal-injection-plugin": "workspace:*",
"@dd/internal-true-end-plugin": "workspace:*",
"@dd/rum-plugin": "workspace:*",
"@dd/telemetry-plugin": "workspace:*",
"chalk": "2.3.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getBundlerReportPlugins } from '@dd/internal-bundler-report-plugin';
import { getCustomHooksPlugins } from '@dd/internal-custom-hooks-plugin';
import { getGitPlugins } from '@dd/internal-git-plugin';
import { getInjectionPlugins } from '@dd/internal-injection-plugin';
import { getTrueEndPlugins } from '@dd/internal-true-end-plugin';
// #imports-injection-marker
// #types-export-injection-marker
export type { types as ErrorTrackingTypes } from '@dd/error-tracking-plugin';
Expand Down Expand Up @@ -94,6 +95,7 @@ export const buildPluginFactory = ({
['custom-hooks', getCustomHooksPlugins],
['git', getGitPlugins],
['injection', getInjectionPlugins],
['true-end', getTrueEndPlugins],
// #internal-plugins-injection-marker
);

Expand Down
39 changes: 39 additions & 0 deletions packages/plugins/custom-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ you should create a custom hook to let other plugins use it as soon as it is ava
- [Build Report](#build-report)
- [Bundler Report](#bundler-report)
- [Git](#git)
- [True End](#true-end)
<!-- #toc -->

## Create a custom hook
Expand Down Expand Up @@ -148,4 +149,42 @@ This hook is called when the git repository data is computed.
}
```

### True End

> [📝 Full documentation ➡️](/packages/plugins/true-end#hooks)

#### `asyncTrueEnd`

This hook is called at the very end of the build asynchronously.

It may execute sooner than `syncTrueEnd` in some contexts:

- `esbuild` will call `asyncTrueEnd` before `syncTrueEnd`.
- We use `build.onDispose`, for the latest hook possible in the build. The issue is, it's synchronous only. So we have to use `build.onEnd` for the asynchronous `asyncTrueEnd`, but it's called well before `build.onDispose`.
- `webpack 4` will only call `syncTrueEnd` if the build has an error. All good otherwise.

```typescript
{
name: 'my-plugin',
async asyncTrueEnd() {
// Do something asynchronous on closure
await someAsyncOperation();
}
}
```

#### `syncTrueEnd`

This hook is called at the very end of the build synchronously.

```typescript
{
name: 'my-plugin',
syncTrueEnd() {
// Do something synchronous on closure
someSyncOperation();
}
}
```

<!-- #list-of-hooks -->
39 changes: 39 additions & 0 deletions packages/plugins/true-end/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# True End Plugin <!-- #omit in toc -->

A custom hook for the true end of a build, cross bundlers.

## Hooks

### `asyncTrueEnd`

This hook is called at the very end of the build asynchronously.

It may execute sooner than `syncTrueEnd` in some contexts:

- `esbuild` will call `asyncTrueEnd` before `syncTrueEnd`.
- We use `build.onDispose`, for the latest hook possible in the build. The issue is, it's synchronous only. So we have to use `build.onEnd` for the asynchronous `asyncTrueEnd`, but it's called well before `build.onDispose`.
- `webpack 4` will only call `syncTrueEnd` if the build has an error. All good otherwise.

```typescript
{
name: 'my-plugin',
async asyncTrueEnd() {
// Do something asynchronous on closure
await someAsyncOperation();
}
}
```

### `syncTrueEnd`

This hook is called at the very end of the build synchronously.

```typescript
{
name: 'my-plugin',
syncTrueEnd() {
// Do something synchronous on closure
someSyncOperation();
}
}
```
27 changes: 27 additions & 0 deletions packages/plugins/true-end/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@dd/internal-true-end-plugin",
"packageManager": "yarn@4.0.2",
"license": "MIT",
"private": true,
"author": "Datadog",
"description": "A custom hook for the true end of a build, cross bundlers.",
"homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/true-end#readme",
"repository": {
"type": "git",
"url": "https://github.com/DataDog/build-plugins",
"directory": "packages/plugins/true-end"
},
"exports": {
".": "./src/index.ts",
"./*": "./src/*.ts"
},
"scripts": {
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@dd/core": "workspace:*"
},
"devDependencies": {
"typescript": "5.4.3"
}
}
45 changes: 45 additions & 0 deletions packages/plugins/true-end/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { BUNDLERS, runBundlers } from '@dd/tests/_jest/helpers/runBundlers';

describe('True End', () => {
// TODO test for multi outputs, failing builds, async hook in esbuild.
test('Should call true end hook.', async () => {
const asyncBundlers: string[] = [];
const syncBundlers: string[] = [];

const asyncTrueEndHookFn = jest.fn(async (bundler: string) => {
asyncBundlers.push(bundler);
});
const syncTrueEndHookFn = jest.fn((bundler: string) => {
syncBundlers.push(bundler);
});

await runBundlers({
logLevel: 'none',
customPlugins: ({ context }) => {
return [
{
name: 'true-end-plugin',
async asyncTrueEnd() {
await asyncTrueEndHookFn(context.bundler.fullName);
},
syncTrueEnd() {
syncTrueEndHookFn(context.bundler.fullName);
},
},
];
},
});

const bundlerNames = BUNDLERS.map((b) => b.name);

expect(asyncBundlers).toEqual(bundlerNames);
expect(syncBundlers).toEqual(bundlerNames);

expect(asyncTrueEndHookFn).toHaveBeenCalledTimes(BUNDLERS.length);
expect(syncTrueEndHookFn).toHaveBeenCalledTimes(BUNDLERS.length);
});
});
64 changes: 64 additions & 0 deletions packages/plugins/true-end/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { GetInternalPlugins, GetPluginsArg, PluginOptions, PluginName } from '@dd/core/types';

export const PLUGIN_NAME: PluginName = 'datadog-true-end-plugin' as const;

export const getTrueEndPlugins: GetInternalPlugins = (arg: GetPluginsArg) => {
const { context } = arg;
const asyncHookFn = async () => {
await context.asyncHook('asyncTrueEnd');
};
const syncHookFn = () => {
context.hook('syncTrueEnd');
};
const bothHookFns = async () => {
syncHookFn();
await asyncHookFn();
};

const xpackPlugin: PluginOptions['rspack'] & PluginOptions['webpack'] = (compiler) => {
if (compiler.hooks.shutdown) {
// NOTE: rspack prior to 1.2.* will randomly crash on shutdown.tapPromise.
compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, bothHookFns);
} else {
// Webpack 4 only.
compiler.hooks.done.tapPromise(PLUGIN_NAME, bothHookFns);
compiler.hooks.failed.tap(PLUGIN_NAME, syncHookFn);
}
};

const rollupPlugin: PluginOptions['rollup'] & PluginOptions['vite'] = {
async writeBundle() {
// TODO: Need to fallback here in case the closeBundle isn't called.
},
async closeBundle() {
await bothHookFns();
},
};

return [
{
name: PLUGIN_NAME,
enforce: 'post',
webpack: xpackPlugin,
esbuild: {
setup(build) {
// NOTE: "onEnd" is the best we can do for esbuild, but it's very far from being the "true end" of the build.
build.onEnd(async () => {
await asyncHookFn();
});
// NOTE: "onDispose" is strictly synchronous.
build.onDispose(() => {
syncHookFn();
});
},
},
vite: rollupPlugin,
rollup: rollupPlugin,
rspack: xpackPlugin,
},
];
};
10 changes: 10 additions & 0 deletions packages/plugins/true-end/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "./",
"outDir": "./dist"
},
"include": ["**/*"],
"exclude": ["dist", "node_modules"]
}
13 changes: 11 additions & 2 deletions packages/tools/src/bundlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export const buildWithEsbuild: BundlerRunFn = async (bundlerConfigs: BuildOption
errors.push(`[ESBUILD] : ${e.message}`);
}

// There's a slight delay to fully exit esbuild and trigger the onDispose hook.
await new Promise<void>((resolve) => setTimeout(resolve, 1));

return { errors, result };
};

Expand Down Expand Up @@ -168,12 +171,18 @@ export const buildWithRollup: BundlerRunFn = async (bundlerConfig: RollupOptions

// Write out the results.
if (bundlerConfig.output) {
const outputProms = [];
const outputProms: Promise<RollupOutput>[] = [];
const outputOptions = Array.isArray(bundlerConfig.output)
? bundlerConfig.output
: [bundlerConfig.output];
for (const outputOption of outputOptions) {
outputProms.push(result.write(outputOption));
outputProms.push(
(async () => {
const bundleResult = await result.write(outputOption);
await result.close();
return bundleResult;
})(),
);
}

results = await Promise.all(outputProms);
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,7 @@ __metadata:
"@dd/internal-custom-hooks-plugin": "workspace:*"
"@dd/internal-git-plugin": "workspace:*"
"@dd/internal-injection-plugin": "workspace:*"
"@dd/internal-true-end-plugin": "workspace:*"
"@dd/rum-plugin": "workspace:*"
"@dd/telemetry-plugin": "workspace:*"
chalk: "npm:2.3.1"
Expand Down Expand Up @@ -1754,6 +1755,15 @@ __metadata:
languageName: unknown
linkType: soft

"@dd/internal-true-end-plugin@workspace:*, @dd/internal-true-end-plugin@workspace:packages/plugins/true-end":
version: 0.0.0-use.local
resolution: "@dd/internal-true-end-plugin@workspace:packages/plugins/true-end"
dependencies:
"@dd/core": "workspace:*"
typescript: "npm:5.4.3"
languageName: unknown
linkType: soft

"@dd/rum-plugin@workspace:*, @dd/rum-plugin@workspace:packages/plugins/rum":
version: 0.0.0-use.local
resolution: "@dd/rum-plugin@workspace:packages/plugins/rum"
Expand Down