diff --git a/packages/core/src/plugins/global-context/index.ts b/packages/core/src/plugins/bundler-report/index.ts similarity index 64% rename from packages/core/src/plugins/global-context/index.ts rename to packages/core/src/plugins/bundler-report/index.ts index fa23fd00..c25dc4ce 100644 --- a/packages/core/src/plugins/global-context/index.ts +++ b/packages/core/src/plugins/bundler-report/index.ts @@ -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. @@ -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', @@ -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; }; diff --git a/packages/core/src/plugins/index.ts b/packages/core/src/plugins/index.ts index 6c63b73b..1f2b78ac 100644 --- a/packages/core/src/plugins/index.ts +++ b/packages/core/src/plugins/index.ts @@ -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], }; }; diff --git a/packages/tests/src/core/plugins/bundler-report/index.test.ts b/packages/tests/src/core/plugins/bundler-report/index.test.ts new file mode 100644 index 00000000..50fa17ff --- /dev/null +++ b/packages/tests/src/core/plugins/bundler-report/index.test.ts @@ -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 = {}; + 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)); + }); + }); +}); diff --git a/packages/tests/src/core/plugins/index.test.ts b/packages/tests/src/core/plugins/index.test.ts new file mode 100644 index 00000000..29607f1a --- /dev/null +++ b/packages/tests/src/core/plugins/index.test.ts @@ -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 = {}; + 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); + }); + }); + }); +});