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

[refactor] Better integrate new internal plugins #108

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@

# Plugin owners

# Build Report
packages/plugins/build-report @yoannmoinet
packages/tests/src/plugins/build-report @yoannmoinet

# Bundler Report
packages/plugins/bundler-report @yoannmoinet
packages/tests/src/plugins/bundler-report @yoannmoinet

# Git
packages/plugins/git @yoannmoinet
packages/tests/src/plugins/git @yoannmoinet

# Injection
packages/plugins/injection @yoannmoinet
packages/tests/src/plugins/injection @yoannmoinet

# Telemetry
packages/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
packages/tests/src/plugins/telemetry @DataDog/frontend-devx @yoannmoinet

# Rum
packages/plugins/rum @DataDog/rum @yoannmoinet
packages/tests/src/plugins/rum @DataDog/rum @yoannmoinet
packages/tests/src/plugins/rum @DataDog/rum @yoannmoinet
18 changes: 2 additions & 16 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ jobs:
- run: yarn install
- run: yarn test

check-integrity:
name: Check integrity of our files
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install node
uses: actions/setup-node@v4
with:
node-version: '18.19.0'
- run: yarn install
- run: yarn cli integrity
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn cli integrity' and commit the result." && exit 1)

lint:
name: Linting
runs-on: ubuntu-latest
Expand All @@ -50,5 +36,5 @@ jobs:
- run: yarn install
- run: yarn build:all
- run: yarn typecheck:all
- run: yarn format
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn format' and commit the result." && exit 1)
- run: yarn cli integrity
- run: git diff --exit-code && git diff --cached --exit-code || (echo "Please run 'yarn cli integrity' and commit the result." && exit 1)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ datadogWebpackPlugin({
tags?: string[];
timestamp?: number;
filters?: ((metric: Metric) => Metric | null)[];
}
};
}
```
<!-- #full-configuration -->
Expand Down
12 changes: 1 addition & 11 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fs from 'fs';
import path from 'path';
import type { RequestInit } from 'undici-types';

import type { GlobalContext, RequestOpts } from './types';
import type { RequestOpts } from './types';

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

// Is the given plugin name is from our internal plugins?
export const isInternalPlugin = (pluginName: string, context: GlobalContext) => {
for (const internalPluginName of context.pluginNames) {
if (pluginName.includes(internalPluginName)) {
return true;
}
}
return false;
};

// Replacing fs-extra with local helpers.
// Delete folders recursively.
export const rm = async (dir: string) => {
Expand Down
124 changes: 122 additions & 2 deletions packages/factory/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,125 @@
# Factory
# Factory <!-- #omit in toc -->

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

We use [unplugin](https://unplugin.unjs.io/) to support many different bundlers.
> [!NOTE]
> We use [unplugin](https://unplugin.unjs.io/) to support many different bundlers.

<!-- This is auto generated with yarn cli integrity -->

<!-- #toc -->
- [Internal Plugins](#internal-plugins)
- [Build Report](#build-report)
- [Bundler Report](#bundler-report)
- [Git](#git)
- [Injection](#injection)
- [Global Context](#global-context)
<!-- #toc -->

## Internal Plugins

These are the plugins that are used internally by the factory.
Most of the time they will interact via the global context.

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

> This will populate `context.build` with a bunch of data coming from the build.

<kbd>[📝 Full documentation ➡️](/packages/plugins/build-report#readme)</kbd>

### Bundler Report

> A very basic report on the currently used bundler.<br/>
> It is useful to unify some configurations.

<kbd>[📝 Full documentation ➡️](/packages/plugins/bundler-report#readme)</kbd>

### Git

> Adds repository data to the global context from the `buildStart` hook.

<kbd>[📝 Full documentation ➡️](/packages/plugins/git#readme)</kbd>

### Injection

> This is used to prepend some code to the produced bundle.<br/>
> Particularly useful if you want to share some global context, or to automatically inject some SDK.

<kbd>[📝 Full documentation ➡️](/packages/plugins/injection#readme)</kbd>
<!-- #internal-plugins-list -->

## Global Context

A global, shared context within the build plugins ecosystem.<br/>
It is passed to your plugin's initialization, and **is mutated during the build process**.

```typescript
type GlobalContext = {
// Mirror of the user's config.
auth?: {
apiKey?: string;
};
// More details on the currently running bundler.
bundler: {
name: string;
fullName: string; // Including its variant.
outDir: string; // Output directory
// Added in `buildStart`.
rawConfig?: any;
variant: string; // Major version of the bundler (webpack 4, webpack 5), empty string otherwise.
};
// Added in `writeBundle`.
build: {
errors: string[];
warnings: string[];
// The list of entries used in the build.
entries ? : {
filepath: string;
inputs: Input[],
name: string;
outputs: Output[]
size: number;
type: string,
} [];
// The list of inputs used in the build.
inputs ? : {
filepath: string;
dependencies: Input[];
dependents: Input[]
name: string;
size: number;
type: string,
} [];
// The list of outputs generated by the build.
outputs ? : {
filepath: string;
name: string;
size: number;
type: string,
// Sourcemaps will use Outputs as their Inputs.
inputs: (Input | Output)[]
} [];
start?: number;
end?: number;
duration?: number;
writeDuration?: number;
};
cwd: string;
// Added in `buildStart`.
git?: {
hash: string;
remote: string;
trackedFilesMatcher: [TrackedFilesMatcher](/packages/plugins/git/trackedFilesMatcher.ts);
};
inject: (item: { type: 'file' | 'code'; value: string; fallback?: @self }) => void;
start: number;
version: string;
}
```

> [!NOTE]
> Some parts of the context are only available after certain hooks:
> - `context.bundler.rawConfig` is added in the `buildStart` hook.
> - `context.build.*` is populated in the `writeBundle` hook.
> - `context.git.*` is populated in the `buildStart` hook.
1 change: 0 additions & 1 deletion packages/factory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@dd/core": "workspace:*",
"@dd/internal-build-report-plugin": "workspace:*",
"@dd/internal-bundler-report-plugin": "workspace:*",
"@dd/internal-context-plugin": "workspace:*",
"@dd/internal-git-plugin": "workspace:*",
"@dd/internal-injection-plugin": "workspace:*",
"@dd/rum-plugin": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ export const getContext = ({

return context;
};

export const validateOptions = (options: Options = {}): Options => {
return {
auth: {},
disableGit: false,
logLevel: 'warn',
...options,
};
};
48 changes: 25 additions & 23 deletions packages/factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

// This file is partially generated.
// Anything between #imports-injection-marker, #types-export-injection-marker, #helpers-injection-marker and #configs-injection-marker
// This file is mostly generated.
// Anything between
// - #imports-injection-marker
// - #types-export-injection-marker
// - #internal-plugins-injection-marker
// - #helpers-injection-marker
// - #configs-injection-marker
// will be updated using the 'yarn cli integrity' command.

import type {
Expand All @@ -14,27 +19,27 @@ import type {
PluginOptions,
ToInjectItem,
} from '@dd/core/types';
import { getInternalPlugins } from '@dd/factory/internalPlugins';
// eslint-disable-next-line arca/newline-after-import-section
import { getContext } from '@dd/internal-context-plugin';
import type { UnpluginContextMeta, UnpluginInstance, UnpluginOptions } from 'unplugin';
import { createUnplugin } from 'unplugin';

/* eslint-disable arca/import-ordering */
import { getContext, validateOptions } from './helpers';

/* eslint-disable arca/import-ordering, arca/newline-after-import-section */
// #imports-injection-marker
import type { OptionsWithRum } from '@dd/rum-plugin/types';
import * as rum from '@dd/rum-plugin';
import type { OptionsWithTelemetry } from '@dd/telemetry-plugin/types';
import * as telemetry from '@dd/telemetry-plugin';
import { getBuildReportPlugins } from '@dd/internal-build-report-plugin';
import { getBundlerReportPlugins } from '@dd/internal-bundler-report-plugin';
import { getGitPlugins } from '@dd/internal-git-plugin';
import { getInjectionPlugins } from '@dd/internal-injection-plugin';
// #imports-injection-marker
/* eslint-enable arca/import-ordering */
import type { UnpluginContextMeta, UnpluginInstance, UnpluginOptions } from 'unplugin';
import { createUnplugin } from 'unplugin';

/* eslint-disable arca/import-ordering */
// #types-export-injection-marker
export type { types as RumTypes } from '@dd/rum-plugin';
export type { types as TelemetryTypes } from '@dd/telemetry-plugin';
// #types-export-injection-marker
/* eslint-enable arca/import-ordering */
/* eslint-enable arca/import-ordering, arca/newline-after-import-section */

export const helpers = {
// Each product should have a unique entry.
Expand All @@ -43,15 +48,6 @@ export const helpers = {
// #helpers-injection-marker
};

const validateOptions = (options: Options = {}): Options => {
return {
auth: {},
disableGit: false,
logLevel: 'warn',
...options,
};
};

const HOST_NAME = 'datadog-build-plugins';

export const buildPluginFactory = ({
Expand Down Expand Up @@ -83,10 +79,15 @@ export const buildPluginFactory = ({
context.pluginNames.push(HOST_NAME);

// List of plugins to be returned.
// We keep UnpluginOptions for the custom plugins.
// We keep the UnpluginOptions type for the custom plugins.
const plugins: (PluginOptions | UnpluginOptions)[] = [
// Prefill with our internal plugins.
...getInternalPlugins(options, bundler, context, injections),
// #internal-plugins-injection-marker
...getBuildReportPlugins(options, context),
...getBundlerReportPlugins(context),
...getGitPlugins(options, context),
...getInjectionPlugins(bundler, options, context, injections),
// #internal-plugins-injection-marker
];

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

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

return plugins;
Expand Down
23 changes: 0 additions & 23 deletions packages/factory/src/internalPlugins.ts

This file was deleted.

22 changes: 12 additions & 10 deletions packages/plugins/build-report/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import { getWebpackPlugin } from './webpack';

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

export const getBuildReportPlugin = (opts: Options, context: GlobalContext): PluginOptions => {
export const getBuildReportPlugins = (opts: Options, context: GlobalContext): PluginOptions[] => {
const log = getLogger(opts.logLevel, PLUGIN_NAME);
return {
name: PLUGIN_NAME,
enforce: 'post',
esbuild: getEsbuildPlugin(context, log),
webpack: getWebpackPlugin(context, PLUGIN_NAME, log),
// Vite and Rollup have the same API.
vite: getRollupPlugin(context, log),
rollup: getRollupPlugin(context, log),
};
return [
{
name: PLUGIN_NAME,
enforce: 'post',
esbuild: getEsbuildPlugin(context, log),
webpack: getWebpackPlugin(context, PLUGIN_NAME, log),
// Vite and Rollup have the same API.
vite: getRollupPlugin(context, log),
rollup: getRollupPlugin(context, log),
},
];
};
Loading