Skip to content

Commit

Permalink
Use raw requests to detect feature usage in telemetry
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eps1lon committed Feb 12, 2025
1 parent 83b64e9 commit 0ee9728
Showing 1 changed file with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,20 @@ interface FeatureUsage {
invocationCount: number
}

/**
* A vertex in the module graph.
*/
interface Module {
type: string
identifier(): string
}

/**
* An edge in the module graph.
*/
interface Connection {
originModule: unknown
}

// Map of a feature module to the file it belongs in the next package.
// Map of a feature module to the request it belongs to
const FEATURE_MODULE_MAP: ReadonlyMap<Feature, string> = new Map([
['next/image', '/next/image.js'],
['next/future/image', '/next/future/image.js'],
['next/legacy/image', '/next/legacy/image.js'],
['next/script', '/next/script.js'],
['next/dynamic', '/next/dynamic.js'],
['next/image', 'next/image'],
['next/future/image', 'next/future/image'],
['next/legacy/image', 'next/legacy/image'],
['next/script', 'next/script'],
['next/dynamic', 'next/dynamic'],
])
const FEATURE_MODULE_REGEXP_MAP: ReadonlyMap<Feature, RegExp> = new Map([
['@next/font/google', /\/@next\/font\/google\/target.css?.+$/],
Expand Down Expand Up @@ -127,16 +119,16 @@ const useCacheTracker = createUseCacheTracker()
/**
* Determine if there is a feature of interest in the specified 'module'.
*/
function findFeatureInModule(module: Module): Feature | undefined {
function findFeatureInModule(module: webpack.Module): Feature | undefined {
if (module.type !== 'javascript/auto') {
return
}
const normalizedIdentifier = module.identifier().replace(/\\/g, '/')
for (const [feature, path] of FEATURE_MODULE_MAP) {
if (normalizedIdentifier.endsWith(path)) {
for (const [feature, rawRequest] of FEATURE_MODULE_MAP) {
if ((module as webpack.NormalModule).rawRequest === rawRequest) {
return feature
}
}
const normalizedIdentifier = module.identifier().replace(/\\/g, '/')
for (const [feature, regexp] of FEATURE_MODULE_REGEXP_MAP) {
if (regexp.test(normalizedIdentifier)) {
return feature
Expand All @@ -151,7 +143,7 @@ function findFeatureInModule(module: Module): Feature | undefined {
*/
function findUniqueOriginModulesInConnections(
connections: Connection[],
originModule: Module
originModule: webpack.Module
): Set<unknown> {
const originModules = new Set()
for (const connection of connections) {
Expand Down Expand Up @@ -206,7 +198,7 @@ export class TelemetryPlugin implements webpack.WebpackPluginInstance {
async (compilation: webpack.Compilation, callback: () => void) => {
compilation.hooks.finishModules.tapAsync(
TelemetryPlugin.name,
async (modules: Iterable<Module>, modulesFinish: () => void) => {
async (modules, modulesFinish) => {
for (const module of modules) {
const feature = findFeatureInModule(module)
if (!feature) {
Expand Down

0 comments on commit 0ee9728

Please sign in to comment.