Skip to content

Commit

Permalink
Track usage of swc features (#30297)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Neutkens <[email protected]>
  • Loading branch information
padmaia and timneutkens authored Oct 26, 2021
1 parent 9eceb95 commit 0ab8c7a
Show file tree
Hide file tree
Showing 4 changed files with 34 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
6 changes: 6 additions & 0 deletions test/integration/telemetry/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down

0 comments on commit 0ab8c7a

Please sign in to comment.