Skip to content

Commit 0ee9728

Browse files
committed
Use raw requests to detect feature usage in telemetry
This is required for when we start using layers in Pages Router. It's also required to fix telemetry of certain features in App Router which makes extensive usage of layers. That isn't working just yet though a test was kept in place.
1 parent 83b64e9 commit 0ee9728

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

packages/next/src/build/webpack/plugins/telemetry-plugin/telemetry-plugin.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,20 @@ interface FeatureUsage {
5454
invocationCount: number
5555
}
5656

57-
/**
58-
* A vertex in the module graph.
59-
*/
60-
interface Module {
61-
type: string
62-
identifier(): string
63-
}
64-
6557
/**
6658
* An edge in the module graph.
6759
*/
6860
interface Connection {
6961
originModule: unknown
7062
}
7163

72-
// Map of a feature module to the file it belongs in the next package.
64+
// Map of a feature module to the request it belongs to
7365
const FEATURE_MODULE_MAP: ReadonlyMap<Feature, string> = new Map([
74-
['next/image', '/next/image.js'],
75-
['next/future/image', '/next/future/image.js'],
76-
['next/legacy/image', '/next/legacy/image.js'],
77-
['next/script', '/next/script.js'],
78-
['next/dynamic', '/next/dynamic.js'],
66+
['next/image', 'next/image'],
67+
['next/future/image', 'next/future/image'],
68+
['next/legacy/image', 'next/legacy/image'],
69+
['next/script', 'next/script'],
70+
['next/dynamic', 'next/dynamic'],
7971
])
8072
const FEATURE_MODULE_REGEXP_MAP: ReadonlyMap<Feature, RegExp> = new Map([
8173
['@next/font/google', /\/@next\/font\/google\/target.css?.+$/],
@@ -127,16 +119,16 @@ const useCacheTracker = createUseCacheTracker()
127119
/**
128120
* Determine if there is a feature of interest in the specified 'module'.
129121
*/
130-
function findFeatureInModule(module: Module): Feature | undefined {
122+
function findFeatureInModule(module: webpack.Module): Feature | undefined {
131123
if (module.type !== 'javascript/auto') {
132124
return
133125
}
134-
const normalizedIdentifier = module.identifier().replace(/\\/g, '/')
135-
for (const [feature, path] of FEATURE_MODULE_MAP) {
136-
if (normalizedIdentifier.endsWith(path)) {
126+
for (const [feature, rawRequest] of FEATURE_MODULE_MAP) {
127+
if ((module as webpack.NormalModule).rawRequest === rawRequest) {
137128
return feature
138129
}
139130
}
131+
const normalizedIdentifier = module.identifier().replace(/\\/g, '/')
140132
for (const [feature, regexp] of FEATURE_MODULE_REGEXP_MAP) {
141133
if (regexp.test(normalizedIdentifier)) {
142134
return feature
@@ -151,7 +143,7 @@ function findFeatureInModule(module: Module): Feature | undefined {
151143
*/
152144
function findUniqueOriginModulesInConnections(
153145
connections: Connection[],
154-
originModule: Module
146+
originModule: webpack.Module
155147
): Set<unknown> {
156148
const originModules = new Set()
157149
for (const connection of connections) {
@@ -206,7 +198,7 @@ export class TelemetryPlugin implements webpack.WebpackPluginInstance {
206198
async (compilation: webpack.Compilation, callback: () => void) => {
207199
compilation.hooks.finishModules.tapAsync(
208200
TelemetryPlugin.name,
209-
async (modules: Iterable<Module>, modulesFinish: () => void) => {
201+
async (modules, modulesFinish) => {
210202
for (const module of modules) {
211203
const feature = findFeatureInModule(module)
212204
if (!feature) {

0 commit comments

Comments
 (0)