Unverified Commit d75dbd32 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Webpack: abstract away getting compilation spans (#76579)

Currently many of our plugins import a singleton mapping of webpack compilation objects to their corresponding tracing span. 

This change abstracts that away into a function called `getCompilationSpan`, and allows us to use the corresponding mapping in the rspack case. 

Previously, any plugin that used these would fail with rspack when the compilation object wouldn’t be found. This was the case when running `next build` with rspack.

Test Plan: Install and run `next build` with the rspack plugin in a test app.
parent 20f72b93
...@@ -17,11 +17,8 @@ import type { BuildManifest } from '../../../server/get-page-files' ...@@ -17,11 +17,8 @@ import type { BuildManifest } from '../../../server/get-page-files'
import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint' import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin' import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
import { getSortedRoutes } from '../../../shared/lib/router/utils' import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { spans as webpackSpans } from './profiling-plugin'
import { compilationSpans as rspackSpans } from './rspack-profiling-plugin'
import { Span } from '../../../trace' import { Span } from '../../../trace'
import { getCompilationSpan } from '../utils'
const compilationSpans = process.env.NEXT_RSPACK ? rspackSpans : webpackSpans
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> } type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
...@@ -109,9 +106,9 @@ export function generateClientManifest( ...@@ -109,9 +106,9 @@ export function generateClientManifest(
compilation?: any compilation?: any
): string | undefined { ): string | undefined {
const compilationSpan = compilation const compilationSpan = compilation
? compilationSpans.get(compilation) ? getCompilationSpan(compilation)
: compiler : compiler
? compilationSpans.get(compiler) ? getCompilationSpan(compiler)
: new Span({ name: 'client-manifest' }) : new Span({ name: 'client-manifest' })
const genClientManifestSpan = compilationSpan?.traceChild( const genClientManifestSpan = compilationSpan?.traceChild(
...@@ -205,7 +202,7 @@ export default class BuildManifestPlugin { ...@@ -205,7 +202,7 @@ export default class BuildManifestPlugin {
createAssets(compiler: any, compilation: any) { createAssets(compiler: any, compilation: any) {
const compilationSpan = const compilationSpan =
compilationSpans.get(compilation) ?? compilationSpans.get(compiler) getCompilationSpan(compilation) ?? getCompilationSpan(compiler)
if (!compilationSpan) { if (!compilationSpan) {
throw new Error('No span found for compilation') throw new Error('No span found for compilation')
} }
......
...@@ -3,7 +3,7 @@ import postcssScss from 'next/dist/compiled/postcss-scss' ...@@ -3,7 +3,7 @@ import postcssScss from 'next/dist/compiled/postcss-scss'
import postcss from 'postcss' import postcss from 'postcss'
import type { Parser } from 'postcss' import type { Parser } from 'postcss'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack' import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { spans } from './profiling-plugin' import { getCompilationSpan } from '../utils'
// https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20 // https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20
const CSS_REGEX = /\.css(\?.*)?$/i const CSS_REGEX = /\.css(\?.*)?$/i
...@@ -64,7 +64,8 @@ export class CssMinimizerPlugin { ...@@ -64,7 +64,8 @@ export class CssMinimizerPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
}, },
async (assets: any) => { async (assets: any) => {
const compilationSpan = spans.get(compilation) || spans.get(compiler) const compilationSpan =
getCompilationSpan(compilation) || getCompilationSpan(compiler)
const cssMinimizerSpan = compilationSpan!.traceChild( const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin' 'css-minimizer-plugin'
) )
......
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
type Compilation, type Compilation,
} from 'next/dist/compiled/webpack/webpack' } from 'next/dist/compiled/webpack/webpack'
import pLimit from 'next/dist/compiled/p-limit' import pLimit from 'next/dist/compiled/p-limit'
import { spans } from '../../profiling-plugin' import { getCompilationSpan } from '../../../utils'
function buildError(error: any, file: string) { function buildError(error: any, file: string) {
if (error.line) { if (error.line) {
...@@ -48,7 +48,8 @@ export class MinifyPlugin { ...@@ -48,7 +48,8 @@ export class MinifyPlugin {
} }
) { ) {
const mangle = !this.options.noMangling const mangle = !this.options.noMangling
const compilationSpan = spans.get(compilation)! || spans.get(compiler) const compilationSpan =
getCompilationSpan(compilation)! || getCompilationSpan(compiler)
const MinifierSpan = compilationSpan.traceChild( const MinifierSpan = compilationSpan.traceChild(
'minify-webpack-plugin-optimize' 'minify-webpack-plugin-optimize'
......
import nodePath from 'path' import nodePath from 'path'
import type { Span } from '../../../trace' import type { Span } from '../../../trace'
import { spans } from './profiling-plugin'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
import { nodeFileTrace } from 'next/dist/compiled/@vercel/nft' import { nodeFileTrace } from 'next/dist/compiled/@vercel/nft'
import type { NodeFileTraceReasons } from 'next/dist/compiled/@vercel/nft' import type { NodeFileTraceReasons } from 'next/dist/compiled/@vercel/nft'
...@@ -20,6 +19,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info' ...@@ -20,6 +19,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getPageFilePath } from '../../entries' import { getPageFilePath } from '../../entries'
import { resolveExternal } from '../../handle-externals' import { resolveExternal } from '../../handle-externals'
import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route' import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route'
import { getCompilationSpan } from '../utils'
const PLUGIN_NAME = 'TraceEntryPointsPlugin' const PLUGIN_NAME = 'TraceEntryPointsPlugin'
export const TRACE_IGNORES = [ export const TRACE_IGNORES = [
...@@ -576,6 +576,12 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -576,6 +576,12 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilationSpan =
getCompilationSpan(compilation) || getCompilationSpan(compiler)!
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
compilation.hooks.processAssets.tapAsync( compilation.hooks.processAssets.tapAsync(
{ {
name: PLUGIN_NAME, name: PLUGIN_NAME,
...@@ -633,10 +639,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -633,10 +639,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
} }
} }
const compilationSpan = spans.get(compilation) || spans.get(compiler)!
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
traceEntrypointsPluginSpan.traceFn(() => { traceEntrypointsPluginSpan.traceFn(() => {
compilation.hooks.processAssets.tapAsync( compilation.hooks.processAssets.tapAsync(
{ {
......
...@@ -5,9 +5,13 @@ import type { ...@@ -5,9 +5,13 @@ import type {
NormalModule, NormalModule,
Module, Module,
ModuleGraph, ModuleGraph,
Compiler,
} from 'webpack' } from 'webpack'
import type { ModuleGraphConnection } from 'webpack' import type { ModuleGraphConnection } from 'webpack'
import { getAppLoader } from '../entries' import { getAppLoader } from '../entries'
import { spans as webpackCompilationSpans } from './plugins/profiling-plugin'
import { compilationSpans as rspackCompilationSpans } from './plugins/rspack-profiling-plugin'
import type { Span } from '../../trace'
export function traverseModules( export function traverseModules(
compilation: Compilation, compilation: Compilation,
...@@ -114,3 +118,13 @@ export function getModuleReferencesInOrder( ...@@ -114,3 +118,13 @@ export function getModuleReferencesInOrder(
connections.sort((a, b) => a.index - b.index) connections.sort((a, b) => a.index - b.index)
return connections.map((c) => c.connection) return connections.map((c) => c.connection)
} }
export function getCompilationSpan(
compilation: Compiler | Compilation
): Span | undefined {
const compilationSpans = process.env.NEXT_RSPACK
? rspackCompilationSpans
: webpackCompilationSpans
return compilationSpans.get(compilation)
}
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