Skip to content

Commit f4e9a44

Browse files
authored
Merge pull request #151 from DataDog/yoann/change-plugin-init
[internal] Change how we initialise plugins
2 parents eefb11f + 399dea5 commit f4e9a44

File tree

23 files changed

+153
-194
lines changed

23 files changed

+153
-194
lines changed

.github/CODEOWNERS

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,27 @@
55

66
# Build Report
77
packages/plugins/build-report @yoannmoinet
8-
packages/tests/src/plugins/build-report @yoannmoinet
98

109
# Bundler Report
1110
packages/plugins/bundler-report @yoannmoinet
12-
packages/tests/src/plugins/bundler-report @yoannmoinet
1311

1412
# Git
1513
packages/plugins/git @yoannmoinet
16-
packages/tests/src/plugins/git @yoannmoinet
1714

1815
# Injection
1916
packages/plugins/injection @yoannmoinet
20-
packages/tests/src/plugins/injection @yoannmoinet
2117

2218
# Telemetry
2319
packages/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
24-
packages/tests/src/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
2520

2621
# Error Tracking
2722
packages/plugins/error-tracking @yoannmoinet
28-
packages/tests/src/plugins/error-tracking @yoannmoinet
2923

3024
# Rum
3125
packages/plugins/rum @yoannmoinet
32-
packages/tests/src/plugins/rum @yoannmoinet
3326

3427
# Analytics
3528
packages/plugins/analytics @yoannmoinet
36-
packages/tests/src/plugins/analytics @yoannmoinet
3729

3830
# Custom Hooks
3931
packages/plugins/custom-hooks @yoannmoinet
40-
packages/tests/src/plugins/custom-hooks @yoannmoinet

global.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ declare global {
4141
* To skip the cleanup of the temporary working dirs where we build `runBundlers()`.
4242
*/
4343
NO_CLEANUP?: '1';
44-
/**
45-
* The list of bundlers to use in our tests.
46-
*/
47-
REQUESTED_BUNDLERS?: string;
4844
/**
4945
* Defined by yarn and targets the root of the project.
5046
*/
5147
PROJECT_CWD?: string;
48+
/**
49+
* The list of bundlers to use in our tests.
50+
*/
51+
REQUESTED_BUNDLERS?: string;
5252
}
5353
}
5454
// Extend Jest's expect with custom matchers defined

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export type CustomPluginOptions = Assign<
176176
}
177177
>;
178178

179-
export type GetPlugins<T> = (options: T, context: GlobalContext) => PluginOptions[];
179+
export type GetPlugins = (options: Options, context: GlobalContext) => PluginOptions[];
180180
export type GetCustomPlugins = (options: Options, context: GlobalContext) => CustomPluginOptions[];
181181

182182
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';

packages/factory/src/index.test.ts

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

5-
import { getPlugins } from '@dd/telemetry-plugin';
6-
import { BUNDLERS, runBundlers } from '@dd/tests/_jest/helpers/runBundlers';
7-
8-
jest.mock('@dd/telemetry-plugin', () => {
9-
const originalModule = jest.requireActual('@dd/telemetry-plugin');
10-
return {
11-
...originalModule,
12-
getPlugins: jest.fn(() => []),
13-
};
14-
});
15-
16-
const getPluginsMocked = jest.mocked(getPlugins);
17-
185
describe('Factory', () => {
196
test('Should not throw with no options', async () => {
207
const { buildPluginFactory } = await import('@dd/factory');
@@ -25,14 +12,4 @@ describe('Factory', () => {
2512
factory.vite();
2613
}).not.toThrow();
2714
});
28-
29-
test('Should not call a disabled plugin', async () => {
30-
await runBundlers({ telemetry: { disabled: true } });
31-
expect(getPluginsMocked).not.toHaveBeenCalled();
32-
});
33-
34-
test('Should call an enabled plugin', async () => {
35-
await runBundlers({ telemetry: { disabled: false } });
36-
expect(getPluginsMocked).toHaveBeenCalledTimes(BUNDLERS.length);
37-
});
3815
});

packages/factory/src/index.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ import chalk from 'chalk';
2626
import { getContext, validateOptions } from './helpers';
2727

2828
// #imports-injection-marker
29-
import type { OptionsWithErrorTracking } from '@dd/error-tracking-plugin/types';
3029
import * as errorTracking from '@dd/error-tracking-plugin';
31-
import type { OptionsWithRum } from '@dd/rum-plugin/types';
3230
import * as rum from '@dd/rum-plugin';
33-
import type { OptionsWithTelemetry } from '@dd/telemetry-plugin/types';
3431
import * as telemetry from '@dd/telemetry-plugin';
3532
import { getAnalyticsPlugins } from '@dd/internal-analytics-plugin';
3633
import { getBuildReportPlugins } from '@dd/internal-build-report-plugin';
@@ -100,23 +97,18 @@ export const buildPluginFactory = ({
10097
context.plugins.push(...customPlugins);
10198
}
10299

103-
// Based on configuration add corresponding plugin.
104-
// #configs-injection-marker
105-
if (
106-
options[errorTracking.CONFIG_KEY] &&
107-
options[errorTracking.CONFIG_KEY].disabled !== true
108-
) {
109-
context.plugins.push(
110-
...errorTracking.getPlugins(options as OptionsWithErrorTracking, context),
111-
);
112-
}
113-
if (options[rum.CONFIG_KEY] && options[rum.CONFIG_KEY].disabled !== true) {
114-
context.plugins.push(...rum.getPlugins(options as OptionsWithRum, context));
115-
}
116-
if (options[telemetry.CONFIG_KEY] && options[telemetry.CONFIG_KEY].disabled !== true) {
117-
context.plugins.push(...telemetry.getPlugins(options as OptionsWithTelemetry, context));
100+
// Add the customer facing plugins.
101+
const productPlugins = [
102+
// #configs-injection-marker
103+
errorTracking,
104+
rum,
105+
telemetry,
106+
// #configs-injection-marker
107+
];
108+
109+
for (const plugin of productPlugins) {
110+
context.plugins.push(...plugin.getPlugins(options, context));
118111
}
119-
// #configs-injection-marker
120112

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

packages/plugins/error-tracking/src/index.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// Copyright 2019-Present Datadog, Inc.
44

55
import { uploadSourcemaps } from '@dd/error-tracking-plugin/sourcemaps/index';
6-
import { getSourcemapsConfiguration } from '@dd/tests/_jest/helpers/mocks';
6+
import { getPlugins } from '@dd/error-tracking-plugin';
7+
import { getContextMock, getSourcemapsConfiguration } from '@dd/tests/_jest/helpers/mocks';
78
import { BUNDLERS, runBundlers } from '@dd/tests/_jest/helpers/runBundlers';
89

910
jest.mock('@dd/error-tracking-plugin/sourcemaps/index', () => {
@@ -15,6 +16,21 @@ jest.mock('@dd/error-tracking-plugin/sourcemaps/index', () => {
1516
const uploadSourcemapsMock = jest.mocked(uploadSourcemaps);
1617

1718
describe('Error Tracking Plugin', () => {
19+
describe('getPlugins', () => {
20+
test('Should not initialize the plugin if disabled', async () => {
21+
expect(
22+
getPlugins({ errorTracking: { disabled: true } }, getContextMock()),
23+
).toHaveLength(0);
24+
expect(getPlugins({}, getContextMock())).toHaveLength(0);
25+
});
26+
27+
test('Should initialize the plugin if enabled', async () => {
28+
expect(
29+
getPlugins({ errorTracking: { disabled: false } }, getContextMock()).length,
30+
).toBeGreaterThan(0);
31+
});
32+
});
33+
1834
test('Should process the sourcemaps if enabled.', async () => {
1935
await runBundlers({
2036
disableGit: true,

packages/plugins/error-tracking/src/index.ts

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

5-
import type { GlobalContext, GetPlugins } from '@dd/core/types';
5+
import type { GlobalContext, GetPlugins, Options } from '@dd/core/types';
66

77
import { PLUGIN_NAME } from './constants';
88
import { uploadSourcemaps } from './sourcemaps';
9-
import type {
10-
OptionsWithErrorTracking,
11-
ErrorTrackingOptions,
12-
ErrorTrackingOptionsWithSourcemaps,
13-
} from './types';
9+
import type { ErrorTrackingOptions, ErrorTrackingOptionsWithSourcemaps } from './types';
1410
import { validateOptions } from './validate';
1511

1612
export { CONFIG_KEY, PLUGIN_NAME } from './constants';
1713

1814
export type types = {
1915
// Add the types you'd like to expose here.
2016
ErrorTrackingOptions: ErrorTrackingOptions;
21-
OptionsWithErrorTracking: OptionsWithErrorTracking;
2217
};
2318

24-
export const getPlugins: GetPlugins<OptionsWithErrorTracking> = (
25-
opts: OptionsWithErrorTracking,
26-
context: GlobalContext,
27-
) => {
19+
export const getPlugins: GetPlugins = (opts: Options, context: GlobalContext) => {
2820
const log = context.getLogger(PLUGIN_NAME);
2921
// Verify configuration.
3022
const timeOptions = log.time('validate options');
3123
const options = validateOptions(opts, log);
3224
timeOptions.end();
25+
26+
// If the plugin is disabled, return an empty array.
27+
if (options.disabled) {
28+
return [];
29+
}
30+
3331
return [
3432
{
3533
name: PLUGIN_NAME,

packages/plugins/error-tracking/src/types.ts

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

5-
import type { GetPluginsOptions } from '@dd/core/types';
6-
7-
import type { CONFIG_KEY } from './constants';
8-
95
export type MinifiedPathPrefix = `http://${string}` | `https://${string}` | `/${string}`;
106

117
export type SourcemapsOptions = {
@@ -36,10 +32,6 @@ export type ErrorTrackingOptionsWithSourcemaps = {
3632
sourcemaps: SourcemapsOptionsWithDefaults;
3733
};
3834

39-
export interface OptionsWithErrorTracking extends GetPluginsOptions {
40-
[CONFIG_KEY]: ErrorTrackingOptions;
41-
}
42-
4335
export type Sourcemap = {
4436
minifiedFilePath: string;
4537
minifiedPathPrefix: MinifiedPathPrefix;

packages/plugins/error-tracking/src/validate.ts

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

5-
import type { Logger } from '@dd/core/types';
5+
import type { Logger, Options } from '@dd/core/types';
66
import chalk from 'chalk';
77

88
import { CONFIG_KEY, PLUGIN_NAME } from './constants';
99
import type {
10-
OptionsWithErrorTracking,
1110
ErrorTrackingOptions,
1211
ErrorTrackingOptionsWithDefaults,
1312
SourcemapsOptionsWithDefaults,
@@ -16,10 +15,7 @@ import type {
1615
export const defaultIntakeUrl = `https://sourcemap-intake.${process.env.DATADOG_SITE || 'datadoghq.com'}/api/v2/srcmap`;
1716

1817
// Deal with validation and defaults here.
19-
export const validateOptions = (
20-
config: Partial<OptionsWithErrorTracking>,
21-
log: Logger,
22-
): ErrorTrackingOptionsWithDefaults => {
18+
export const validateOptions = (config: Options, log: Logger): ErrorTrackingOptionsWithDefaults => {
2319
const errors: string[] = [];
2420

2521
// Validate and add defaults sub-options.
@@ -34,6 +30,7 @@ export const validateOptions = (
3430

3531
// Build the final configuration.
3632
const toReturn: ErrorTrackingOptionsWithDefaults = {
33+
disabled: !config[CONFIG_KEY],
3734
...config[CONFIG_KEY],
3835
sourcemaps: undefined,
3936
};
@@ -68,7 +65,7 @@ const validateMinifiedPathPrefix = (minifiedPathPrefix: string): boolean => {
6865
};
6966

7067
export const validateSourcemapsOptions = (
71-
config: Partial<OptionsWithErrorTracking>,
68+
config: Options,
7269
): ToReturn<SourcemapsOptionsWithDefaults> => {
7370
const red = chalk.bold.red;
7471
const validatedOptions: ErrorTrackingOptions = config[CONFIG_KEY] || {};

packages/plugins/rum/src/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ describe('RUM Plugin', () => {
3535
should: { inject: ['browser-sdk', 'sdk-init'] },
3636
},
3737
];
38+
describe('getPlugins', () => {
39+
const injectMock = jest.fn();
40+
test('Should not initialize the plugin if disabled', async () => {
41+
getPlugins(
42+
{ rum: { disabled: true, sdk: { applicationId: 'app-id', clientToken: '123' } } },
43+
getContextMock({ inject: injectMock }),
44+
);
45+
getPlugins({}, getContextMock({ inject: injectMock }));
46+
expect(injectMock).not.toHaveBeenCalled();
47+
});
48+
49+
test('Should initialize the plugin if enabled', async () => {
50+
getPlugins(
51+
{ rum: { disabled: false, sdk: { applicationId: 'app-id', clientToken: '123' } } },
52+
getContextMock({ inject: injectMock }),
53+
);
54+
expect(injectMock).toHaveBeenCalled();
55+
});
56+
});
3857

3958
test.each(expectations)(
4059
'Should inject the necessary files with "$type".',

0 commit comments

Comments
 (0)