Skip to content

Commit

Permalink
Split GlobalContext and Bundler Report plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Sep 27, 2024
1 parent 3140bd4 commit 4eaed23
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { GlobalContext, Meta, Options, PluginOptions } from '@dd/core/types';
import type { GlobalContext, Options, PluginOptions } from '@dd/core/types';
import path from 'path';

// TODO: Add universal config report with list of plugins (names), loaders.
Expand All @@ -24,28 +24,7 @@ const rollupPlugin: (context: GlobalContext) => PluginOptions['rollup'] = (conte
},
});

export const getGlobalContextPlugin = (opts: Options, meta: Meta) => {
const cwd = process.cwd();
const variant =
meta.framework === 'webpack' ? (meta.webpack.compiler['webpack'] ? '5' : '4') : '';

const globalContext: GlobalContext = {
auth: opts.auth,
bundler: {
name: meta.framework,
fullName: `${meta.framework}${variant}`,
variant,
outDir: cwd,
},
build: {
errors: [],
warnings: [],
},
cwd,
start: Date.now(),
version: meta.version,
};

export const getBundlerReportPlugin = (opts: Options, globalContext: GlobalContext) => {
const globalContextPlugin: PluginOptions = {
name: PLUGIN_NAME,
enforce: 'pre',
Expand Down Expand Up @@ -75,16 +54,7 @@ export const getGlobalContextPlugin = (opts: Options, meta: Meta) => {
// Vite and Rollup have the same API.
vite: rollupPlugin(globalContext),
rollup: rollupPlugin(globalContext),
// TODO: Add support and add outputFiles to the context.
rspack(compiler) {
globalContext.bundler.rawConfig = compiler.options;
},
farm: {
configResolved(config: any) {
globalContext.bundler.rawConfig = config;
},
},
};

return { globalContext, globalContextPlugin };
return globalContextPlugin;
};
34 changes: 29 additions & 5 deletions packages/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,43 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { Meta, Options } from '../types';
import type { GlobalContext, Meta, Options, PluginOptions } from '@dd/core/types';

import { getBuildReportPlugin } from './build-report';
import { getBundlerReportPlugin } from './bundler-report';
import { getGitPlugin } from './git';
import { getGlobalContextPlugin } from './global-context';

export const getInternalPlugins = (options: Options, meta: Meta) => {
const { globalContext, globalContextPlugin } = getGlobalContextPlugin(options, meta);
export const getInternalPlugins = (
options: Options,
meta: Meta,
): { globalContext: GlobalContext; internalPlugins: PluginOptions[] } => {
const cwd = process.cwd();
const variant =
meta.framework === 'webpack' ? (meta.webpack.compiler['webpack'] ? '5' : '4') : '';

const globalContext: GlobalContext = {
auth: options.auth,
bundler: {
name: meta.framework,
fullName: `${meta.framework}${variant}`,
variant,
outDir: cwd,
},
build: {
errors: [],
warnings: [],
},
cwd,
start: Date.now(),
version: meta.version,
};

const bundlerReportPlugin = getBundlerReportPlugin(options, globalContext);
const buildReportPlugin = getBuildReportPlugin(options, globalContext);
const gitPlugin = getGitPlugin(options, globalContext);

return {
globalContext,
internalPlugins: [globalContextPlugin, buildReportPlugin, gitPlugin],
internalPlugins: [bundlerReportPlugin, buildReportPlugin, gitPlugin],
};
};
63 changes: 63 additions & 0 deletions packages/tests/src/core/plugins/bundler-report/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 { BundlerReport, Options } from '@dd/core/types';
import { defaultDestination, defaultPluginOptions } from '@dd/tests/helpers/mocks';
import type { CleanupFn } from '@dd/tests/helpers/runBundlers';
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers';

describe('Bundler Report', () => {
// Intercept contexts to verify it at the moment they're used.
const bundlerReports: Record<string, BundlerReport> = {};
let cleanup: CleanupFn;
beforeAll(async () => {
const pluginConfig: Options = {
...defaultPluginOptions,
// Use a custom plugin to intercept contexts to verify it at the moment they're used.
customPlugins: (opts, context) => {
const bundlerName = context.bundler.fullName;
return [
{
name: 'custom-plugin',
writeBundle() {
const config = context.bundler.rawConfig;
bundlerReports[bundlerName] = JSON.parse(
JSON.stringify({
...context.bundler,
// This is not safe to stringify.
rawConfig: null,
}),
);
bundlerReports[bundlerName].rawConfig = config;
},
},
];
},
};

cleanup = await runBundlers(pluginConfig);
});

afterAll(async () => {
await cleanup();
});

describe.each(BUNDLERS)('[$name|$version]', ({ name }) => {
test('Should have the right output directory.', () => {
const report = bundlerReports[name];
const outDir = report.outDir;

const expectedOutDir = new RegExp(`^${defaultDestination}/[^/]+/${name}$`);

expect(outDir).toMatch(expectedOutDir);
});

test("Should have the bundler's options object.", () => {
const report = bundlerReports[name];
const rawConfig = report.rawConfig;
expect(rawConfig).toBeDefined();
expect(rawConfig).toEqual(expect.any(Object));
});
});
});
46 changes: 46 additions & 0 deletions packages/tests/src/core/plugins/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 { GlobalContext, Options } from '@dd/core/types';
import { defaultPluginOptions } from '@dd/tests/helpers/mocks';
import type { CleanupFn } from '@dd/tests/helpers/runBundlers';
import { BUNDLERS, runBundlers } from '@dd/tests/helpers/runBundlers';

describe('Internal Plugins', () => {
describe('Global Context', () => {
// Intercept contexts to verify it at the moment they're used.
const initialContexts: Record<string, GlobalContext> = {};
let cleanup: CleanupFn;

beforeAll(async () => {
const pluginConfig: Options = {
...defaultPluginOptions,
// Use a custom plugin to intercept contexts to verify it at initialization.
customPlugins: (opts, context) => {
const bundlerName = context.bundler.fullName;
initialContexts[bundlerName] = JSON.parse(JSON.stringify(context));
return [];
},
};

cleanup = await runBundlers(pluginConfig);
});

afterAll(async () => {
await cleanup();
});

describe.each(BUNDLERS)('[$name|$version]', ({ name, version }) => {
test('Should have the right initial context.', () => {
const context = initialContexts[name];
expect(context).toBeDefined();
expect(context.auth).toEqual(defaultPluginOptions.auth);
expect(context.bundler.name).toBe(name.replace(context.bundler.variant || '', ''));
expect(context.bundler.fullName).toBe(name);
expect(context.cwd).toBe(process.cwd());
expect(context.version).toBe(version);
});
});
});
});

0 comments on commit 4eaed23

Please sign in to comment.