Skip to content

Commit b7f0408

Browse files
authored
Merge pull request #108 from DataDog/yoann/new-internal-plugins
[refactor] Better integrate new internal plugins
2 parents 36acb24 + b9472da commit b7f0408

File tree

33 files changed

+836
-550
lines changed

33 files changed

+836
-550
lines changed

.github/CODEOWNERS

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33

44
# Plugin owners
55

6+
# Build Report
7+
packages/plugins/build-report @yoannmoinet
8+
packages/tests/src/plugins/build-report @yoannmoinet
9+
10+
# Bundler Report
11+
packages/plugins/bundler-report @yoannmoinet
12+
packages/tests/src/plugins/bundler-report @yoannmoinet
13+
14+
# Git
15+
packages/plugins/git @yoannmoinet
16+
packages/tests/src/plugins/git @yoannmoinet
17+
18+
# Injection
19+
packages/plugins/injection @yoannmoinet
20+
packages/tests/src/plugins/injection @yoannmoinet
21+
622
# Telemetry
723
packages/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
824
packages/tests/src/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
925

1026
# Rum
1127
packages/plugins/rum @DataDog/rum @yoannmoinet
12-
packages/tests/src/plugins/rum @DataDog/rum @yoannmoinet
28+
packages/tests/src/plugins/rum @DataDog/rum @yoannmoinet

.github/workflows/ci.yaml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@ jobs:
2323
- run: yarn install
2424
- run: yarn test
2525

26-
check-integrity:
27-
name: Check integrity of our files
28-
runs-on: ubuntu-latest
29-
30-
steps:
31-
- uses: actions/checkout@v4
32-
- name: Install node
33-
uses: actions/setup-node@v4
34-
with:
35-
node-version: '18.19.0'
36-
- run: yarn install
37-
- run: yarn cli integrity
38-
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn cli integrity' and commit the result." && exit 1)
39-
4026
lint:
4127
name: Linting
4228
runs-on: ubuntu-latest
@@ -50,5 +36,5 @@ jobs:
5036
- run: yarn install
5137
- run: yarn build:all
5238
- run: yarn typecheck:all
53-
- run: yarn format
54-
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn format' and commit the result." && exit 1)
39+
- run: yarn cli integrity
40+
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn cli integrity' and commit the result." && exit 1)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ datadogWebpackPlugin({
276276
tags?: string[];
277277
timestamp?: number;
278278
filters?: ((metric: Metric) => Metric | null)[];
279-
}
279+
};
280280
}
281281
```
282282
<!-- #full-configuration -->

packages/core/src/helpers.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'fs';
99
import path from 'path';
1010
import type { RequestInit } from 'undici-types';
1111

12-
import type { GlobalContext, RequestOpts } from './types';
12+
import type { RequestOpts } from './types';
1313

1414
// Format a duration 0h 0m 0s 0ms
1515
export const formatDuration = (duration: number) => {
@@ -125,16 +125,6 @@ export const truncateString = (
125125
// Is the file coming from the injection plugin?
126126
export const isInjectionFile = (filename: string) => filename.includes(INJECTED_FILE);
127127

128-
// Is the given plugin name is from our internal plugins?
129-
export const isInternalPlugin = (pluginName: string, context: GlobalContext) => {
130-
for (const internalPluginName of context.pluginNames) {
131-
if (pluginName.includes(internalPluginName)) {
132-
return true;
133-
}
134-
}
135-
return false;
136-
};
137-
138128
// Replacing fs-extra with local helpers.
139129
// Delete folders recursively.
140130
export const rm = async (dir: string) => {

packages/factory/README.md

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,125 @@
1-
# Factory
1+
# Factory <!-- #omit in toc -->
22

33
This is used to aggregate all the plugins and expose them to the bundler.
44

5-
We use [unplugin](https://unplugin.unjs.io/) to support many different bundlers.
5+
> [!NOTE]
6+
> We use [unplugin](https://unplugin.unjs.io/) to support many different bundlers.
7+
8+
<!-- This is auto generated with yarn cli integrity -->
9+
10+
<!-- #toc -->
11+
- [Internal Plugins](#internal-plugins)
12+
- [Build Report](#build-report)
13+
- [Bundler Report](#bundler-report)
14+
- [Git](#git)
15+
- [Injection](#injection)
16+
- [Global Context](#global-context)
17+
<!-- #toc -->
18+
19+
## Internal Plugins
20+
21+
These are the plugins that are used internally by the factory.
22+
Most of the time they will interact via the global context.
23+
24+
<!-- #internal-plugins-list -->
25+
### Build Report
26+
27+
> This will populate `context.build` with a bunch of data coming from the build.
28+
29+
<kbd>[📝 Full documentation ➡️](/packages/plugins/build-report#readme)</kbd>
30+
31+
### Bundler Report
32+
33+
> A very basic report on the currently used bundler.<br/>
34+
> It is useful to unify some configurations.
35+
36+
<kbd>[📝 Full documentation ➡️](/packages/plugins/bundler-report#readme)</kbd>
37+
38+
### Git
39+
40+
> Adds repository data to the global context from the `buildStart` hook.
41+
42+
<kbd>[📝 Full documentation ➡️](/packages/plugins/git#readme)</kbd>
43+
44+
### Injection
45+
46+
> This is used to prepend some code to the produced bundle.<br/>
47+
> Particularly useful if you want to share some global context, or to automatically inject some SDK.
48+
49+
<kbd>[📝 Full documentation ➡️](/packages/plugins/injection#readme)</kbd>
50+
<!-- #internal-plugins-list -->
51+
52+
## Global Context
53+
54+
A global, shared context within the build plugins ecosystem.<br/>
55+
It is passed to your plugin's initialization, and **is mutated during the build process**.
56+
57+
```typescript
58+
type GlobalContext = {
59+
// Mirror of the user's config.
60+
auth?: {
61+
apiKey?: string;
62+
};
63+
// More details on the currently running bundler.
64+
bundler: {
65+
name: string;
66+
fullName: string; // Including its variant.
67+
outDir: string; // Output directory
68+
// Added in `buildStart`.
69+
rawConfig?: any;
70+
variant: string; // Major version of the bundler (webpack 4, webpack 5), empty string otherwise.
71+
};
72+
// Added in `writeBundle`.
73+
build: {
74+
errors: string[];
75+
warnings: string[];
76+
// The list of entries used in the build.
77+
entries ? : {
78+
filepath: string;
79+
inputs: Input[],
80+
name: string;
81+
outputs: Output[]
82+
size: number;
83+
type: string,
84+
} [];
85+
// The list of inputs used in the build.
86+
inputs ? : {
87+
filepath: string;
88+
dependencies: Input[];
89+
dependents: Input[]
90+
name: string;
91+
size: number;
92+
type: string,
93+
} [];
94+
// The list of outputs generated by the build.
95+
outputs ? : {
96+
filepath: string;
97+
name: string;
98+
size: number;
99+
type: string,
100+
// Sourcemaps will use Outputs as their Inputs.
101+
inputs: (Input | Output)[]
102+
} [];
103+
start?: number;
104+
end?: number;
105+
duration?: number;
106+
writeDuration?: number;
107+
};
108+
cwd: string;
109+
// Added in `buildStart`.
110+
git?: {
111+
hash: string;
112+
remote: string;
113+
trackedFilesMatcher: [TrackedFilesMatcher](/packages/plugins/git/trackedFilesMatcher.ts);
114+
};
115+
inject: (item: { type: 'file' | 'code'; value: string; fallback?: @self }) => void;
116+
start: number;
117+
version: string;
118+
}
119+
```
120+
121+
> [!NOTE]
122+
> Some parts of the context are only available after certain hooks:
123+
> - `context.bundler.rawConfig` is added in the `buildStart` hook.
124+
> - `context.build.*` is populated in the `writeBundle` hook.
125+
> - `context.git.*` is populated in the `buildStart` hook.

packages/factory/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@dd/core": "workspace:*",
2020
"@dd/internal-build-report-plugin": "workspace:*",
2121
"@dd/internal-bundler-report-plugin": "workspace:*",
22-
"@dd/internal-context-plugin": "workspace:*",
2322
"@dd/internal-git-plugin": "workspace:*",
2423
"@dd/internal-injection-plugin": "workspace:*",
2524
"@dd/rum-plugin": "workspace:*",

packages/plugins/context/src/index.ts renamed to packages/factory/src/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ export const getContext = ({
5151

5252
return context;
5353
};
54+
55+
export const validateOptions = (options: Options = {}): Options => {
56+
return {
57+
auth: {},
58+
disableGit: false,
59+
logLevel: 'warn',
60+
...options,
61+
};
62+
};

packages/factory/src/index.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5-
// This file is partially generated.
6-
// Anything between #imports-injection-marker, #types-export-injection-marker, #helpers-injection-marker and #configs-injection-marker
5+
// This file is mostly generated.
6+
// Anything between
7+
// - #imports-injection-marker
8+
// - #types-export-injection-marker
9+
// - #internal-plugins-injection-marker
10+
// - #helpers-injection-marker
11+
// - #configs-injection-marker
712
// will be updated using the 'yarn cli integrity' command.
813

914
import type {
@@ -14,27 +19,27 @@ import type {
1419
PluginOptions,
1520
ToInjectItem,
1621
} from '@dd/core/types';
17-
import { getInternalPlugins } from '@dd/factory/internalPlugins';
18-
// eslint-disable-next-line arca/newline-after-import-section
19-
import { getContext } from '@dd/internal-context-plugin';
22+
import type { UnpluginContextMeta, UnpluginInstance, UnpluginOptions } from 'unplugin';
23+
import { createUnplugin } from 'unplugin';
2024

21-
/* eslint-disable arca/import-ordering */
25+
import { getContext, validateOptions } from './helpers';
26+
27+
/* eslint-disable arca/import-ordering, arca/newline-after-import-section */
2228
// #imports-injection-marker
2329
import type { OptionsWithRum } from '@dd/rum-plugin/types';
2430
import * as rum from '@dd/rum-plugin';
2531
import type { OptionsWithTelemetry } from '@dd/telemetry-plugin/types';
2632
import * as telemetry from '@dd/telemetry-plugin';
33+
import { getBuildReportPlugins } from '@dd/internal-build-report-plugin';
34+
import { getBundlerReportPlugins } from '@dd/internal-bundler-report-plugin';
35+
import { getGitPlugins } from '@dd/internal-git-plugin';
36+
import { getInjectionPlugins } from '@dd/internal-injection-plugin';
2737
// #imports-injection-marker
28-
/* eslint-enable arca/import-ordering */
29-
import type { UnpluginContextMeta, UnpluginInstance, UnpluginOptions } from 'unplugin';
30-
import { createUnplugin } from 'unplugin';
31-
32-
/* eslint-disable arca/import-ordering */
3338
// #types-export-injection-marker
3439
export type { types as RumTypes } from '@dd/rum-plugin';
3540
export type { types as TelemetryTypes } from '@dd/telemetry-plugin';
3641
// #types-export-injection-marker
37-
/* eslint-enable arca/import-ordering */
42+
/* eslint-enable arca/import-ordering, arca/newline-after-import-section */
3843

3944
export const helpers = {
4045
// Each product should have a unique entry.
@@ -43,15 +48,6 @@ export const helpers = {
4348
// #helpers-injection-marker
4449
};
4550

46-
const validateOptions = (options: Options = {}): Options => {
47-
return {
48-
auth: {},
49-
disableGit: false,
50-
logLevel: 'warn',
51-
...options,
52-
};
53-
};
54-
5551
const HOST_NAME = 'datadog-build-plugins';
5652

5753
export const buildPluginFactory = ({
@@ -83,10 +79,15 @@ export const buildPluginFactory = ({
8379
context.pluginNames.push(HOST_NAME);
8480

8581
// List of plugins to be returned.
86-
// We keep UnpluginOptions for the custom plugins.
82+
// We keep the UnpluginOptions type for the custom plugins.
8783
const plugins: (PluginOptions | UnpluginOptions)[] = [
8884
// Prefill with our internal plugins.
89-
...getInternalPlugins(options, bundler, context, injections),
85+
// #internal-plugins-injection-marker
86+
...getBuildReportPlugins(options, context),
87+
...getBundlerReportPlugins(context),
88+
...getGitPlugins(options, context),
89+
...getInjectionPlugins(bundler, options, context, injections),
90+
// #internal-plugins-injection-marker
9091
];
9192

9293
// Add custom, on the fly plugins, if any.
@@ -105,6 +106,7 @@ export const buildPluginFactory = ({
105106
}
106107
// #configs-injection-marker
107108

109+
// List all our plugins in the context.
108110
context.pluginNames.push(...plugins.map((plugin) => plugin.name));
109111

110112
return plugins;

packages/factory/src/internalPlugins.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/plugins/build-report/src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ import { getWebpackPlugin } from './webpack';
1111

1212
const PLUGIN_NAME = 'datadog-build-report-plugin';
1313

14-
export const getBuildReportPlugin = (opts: Options, context: GlobalContext): PluginOptions => {
14+
export const getBuildReportPlugins = (opts: Options, context: GlobalContext): PluginOptions[] => {
1515
const log = getLogger(opts.logLevel, PLUGIN_NAME);
16-
return {
17-
name: PLUGIN_NAME,
18-
enforce: 'post',
19-
esbuild: getEsbuildPlugin(context, log),
20-
webpack: getWebpackPlugin(context, PLUGIN_NAME, log),
21-
// Vite and Rollup have the same API.
22-
vite: getRollupPlugin(context, log),
23-
rollup: getRollupPlugin(context, log),
24-
};
16+
return [
17+
{
18+
name: PLUGIN_NAME,
19+
enforce: 'post',
20+
esbuild: getEsbuildPlugin(context, log),
21+
webpack: getWebpackPlugin(context, PLUGIN_NAME, log),
22+
// Vite and Rollup have the same API.
23+
vite: getRollupPlugin(context, log),
24+
rollup: getRollupPlugin(context, log),
25+
},
26+
];
2527
};

0 commit comments

Comments
 (0)