From 0ab8c7ada6eb27451f362d1519e6f6214ae83007 Mon Sep 17 00:00:00 2001 From: Maia Teegarden Date: Tue, 26 Oct 2021 00:37:38 -0700 Subject: [PATCH] Track usage of swc features (#30297) Co-authored-by: Tim Neutkens --- packages/next/build/webpack-config.ts | 9 ++++++++- .../build/webpack/plugins/telemetry-plugin.ts | 20 +++++++++++++++++-- packages/next/telemetry/events/build.ts | 2 ++ test/integration/telemetry/test/index.test.js | 6 ++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index f2b2260889ce9..e769d254880e9 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -1307,7 +1307,14 @@ export default async function getBaseWebpackConfig( minimized: true, }, }), - !dev && !isServer && new TelemetryPlugin(), + !dev && + !isServer && + new TelemetryPlugin( + new Map([ + ['swcLoader', useSWCLoader], + ['swcMinify', config.swcMinify], + ]) + ), ].filter(Boolean as any as ExcludesFalse), } diff --git a/packages/next/build/webpack/plugins/telemetry-plugin.ts b/packages/next/build/webpack/plugins/telemetry-plugin.ts index 7093aa91fee26..e961aacb05e6f 100644 --- a/packages/next/build/webpack/plugins/telemetry-plugin.ts +++ b/packages/next/build/webpack/plugins/telemetry-plugin.ts @@ -1,6 +1,11 @@ import type { webpack5 as webpack } from 'next/dist/compiled/webpack/webpack' -type Feature = 'next/image' | 'next/script' | 'next/dynamic' +type Feature = + | 'next/image' + | 'next/script' + | 'next/dynamic' + | 'swcLoader' + | 'swcMinify' interface FeatureUsage { featureName: Feature @@ -29,6 +34,9 @@ const FEATURE_MODULE_MAP: ReadonlyMap = new Map([ ['next/dynamic', '/next/dynamic.js'], ]) +// List of build features used in webpack configuration +const BUILD_FEATURES: Array = ['swcLoader', 'swcMinify'] + /** * Plugin that queries the ModuleGraph to look for modules that correspond to * certain features (e.g. next/image and next/script) and record how many times @@ -37,7 +45,15 @@ const FEATURE_MODULE_MAP: ReadonlyMap = new Map([ export class TelemetryPlugin implements webpack.WebpackPluginInstance { private usageTracker = new Map() - constructor() { + // Build feature usage is on/off and is known before the build starts + constructor(buildFeaturesMap: Map) { + for (const featureName of BUILD_FEATURES) { + this.usageTracker.set(featureName, { + featureName, + invocationCount: buildFeaturesMap.get(featureName) ? 1 : 0, + }) + } + for (const featureName of FEATURE_MODULE_MAP.keys()) { this.usageTracker.set(featureName, { featureName, diff --git a/packages/next/telemetry/events/build.ts b/packages/next/telemetry/events/build.ts index db354d3979e91..f4bbd962993e2 100644 --- a/packages/next/telemetry/events/build.ts +++ b/packages/next/telemetry/events/build.ts @@ -128,6 +128,8 @@ export type EventBuildFeatureUsage = { | 'next/script' | 'next/dynamic' | 'experimental/optimizeCss' + | 'swcLoader' + | 'swcMinify' invocationCount: number } export function eventBuildFeatureUsage( diff --git a/test/integration/telemetry/test/index.test.js b/test/integration/telemetry/test/index.test.js index c0784d859e309..0a01e59be41ab 100644 --- a/test/integration/telemetry/test/index.test.js +++ b/test/integration/telemetry/test/index.test.js @@ -569,6 +569,12 @@ describe('Telemetry CLI', () => { }) const regex = /NEXT_BUILD_FEATURE_USAGE[\s\S]+?{([\s\S]+?)}/g regex.exec(stderr).pop() // optimizeCss + const swcLoader = regex.exec(stderr).pop() + expect(swcLoader).toContain(`"featureName": "swcLoader"`) + expect(swcLoader).toContain(`"invocationCount": 1`) + const swcMinify = regex.exec(stderr).pop() + expect(swcMinify).toContain(`"featureName": "swcMinify"`) + expect(swcMinify).toContain(`"invocationCount": 0`) const image = regex.exec(stderr).pop() expect(image).toContain(`"featureName": "next/image"`) expect(image).toContain(`"invocationCount": 1`)