Unverified Commit 0ab8c7ad authored by Maia Teegarden's avatar Maia Teegarden Committed by GitHub

Track usage of swc features (#30297)

Co-authored-by: 's avatarTim Neutkens <timneutkens@me.com>
parent 9eceb955
......@@ -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),
}
......
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<Feature, string> = new Map([
['next/dynamic', '/next/dynamic.js'],
])
// List of build features used in webpack configuration
const BUILD_FEATURES: Array<Feature> = ['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<Feature, string> = new Map([
export class TelemetryPlugin implements webpack.WebpackPluginInstance {
private usageTracker = new Map<Feature, FeatureUsage>()
constructor() {
// Build feature usage is on/off and is known before the build starts
constructor(buildFeaturesMap: Map<Feature, boolean>) {
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,
......
......@@ -128,6 +128,8 @@ export type EventBuildFeatureUsage = {
| 'next/script'
| 'next/dynamic'
| 'experimental/optimizeCss'
| 'swcLoader'
| 'swcMinify'
invocationCount: number
}
export function eventBuildFeatureUsage(
......
......@@ -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`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment