Skip to content

Commit

Permalink
Track usage of swc features
Browse files Browse the repository at this point in the history
  • Loading branch information
padmaia committed Oct 26, 2021
1 parent 9101df7 commit a72dc5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
20 changes: 18 additions & 2 deletions packages/next/build/webpack/plugins/telemetry-plugin.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/next/telemetry/events/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export type EventBuildFeatureUsage = {
| 'next/script'
| 'next/dynamic'
| 'experimental/optimizeCss'
| 'swcLoader'
| 'swcMinify'
invocationCount: number
}
export function eventBuildFeatureUsage(
Expand Down

0 comments on commit a72dc5f

Please sign in to comment.