Skip to content

Commit d75dbd3

Browse files
authored
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.
1 parent 20f72b9 commit d75dbd3

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

Diff for: packages/next/src/build/webpack/plugins/build-manifest-plugin.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ import type { BuildManifest } from '../../../server/get-page-files'
1717
import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'
1818
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
1919
import { getSortedRoutes } from '../../../shared/lib/router/utils'
20-
import { spans as webpackSpans } from './profiling-plugin'
21-
import { compilationSpans as rspackSpans } from './rspack-profiling-plugin'
2220
import { Span } from '../../../trace'
23-
24-
const compilationSpans = process.env.NEXT_RSPACK ? rspackSpans : webpackSpans
21+
import { getCompilationSpan } from '../utils'
2522

2623
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
2724

@@ -109,9 +106,9 @@ export function generateClientManifest(
109106
compilation?: any
110107
): string | undefined {
111108
const compilationSpan = compilation
112-
? compilationSpans.get(compilation)
109+
? getCompilationSpan(compilation)
113110
: compiler
114-
? compilationSpans.get(compiler)
111+
? getCompilationSpan(compiler)
115112
: new Span({ name: 'client-manifest' })
116113

117114
const genClientManifestSpan = compilationSpan?.traceChild(
@@ -205,7 +202,7 @@ export default class BuildManifestPlugin {
205202

206203
createAssets(compiler: any, compilation: any) {
207204
const compilationSpan =
208-
compilationSpans.get(compilation) ?? compilationSpans.get(compiler)
205+
getCompilationSpan(compilation) ?? getCompilationSpan(compiler)
209206
if (!compilationSpan) {
210207
throw new Error('No span found for compilation')
211208
}

Diff for: packages/next/src/build/webpack/plugins/css-minimizer-plugin.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import postcssScss from 'next/dist/compiled/postcss-scss'
33
import postcss from 'postcss'
44
import type { Parser } from 'postcss'
55
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
6-
import { spans } from './profiling-plugin'
6+
import { getCompilationSpan } from '../utils'
77

88
// https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20
99
const CSS_REGEX = /\.css(\?.*)?$/i
@@ -64,7 +64,8 @@ export class CssMinimizerPlugin {
6464
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
6565
},
6666
async (assets: any) => {
67-
const compilationSpan = spans.get(compilation) || spans.get(compiler)
67+
const compilationSpan =
68+
getCompilationSpan(compilation) || getCompilationSpan(compiler)
6869
const cssMinimizerSpan = compilationSpan!.traceChild(
6970
'css-minimizer-plugin'
7071
)

Diff for: packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type Compilation,
88
} from 'next/dist/compiled/webpack/webpack'
99
import pLimit from 'next/dist/compiled/p-limit'
10-
import { spans } from '../../profiling-plugin'
10+
import { getCompilationSpan } from '../../../utils'
1111

1212
function buildError(error: any, file: string) {
1313
if (error.line) {
@@ -48,7 +48,8 @@ export class MinifyPlugin {
4848
}
4949
) {
5050
const mangle = !this.options.noMangling
51-
const compilationSpan = spans.get(compilation)! || spans.get(compiler)
51+
const compilationSpan =
52+
getCompilationSpan(compilation)! || getCompilationSpan(compiler)
5253

5354
const MinifierSpan = compilationSpan.traceChild(
5455
'minify-webpack-plugin-optimize'

Diff for: packages/next/src/build/webpack/plugins/next-trace-entrypoints-plugin.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import nodePath from 'path'
22
import type { Span } from '../../../trace'
3-
import { spans } from './profiling-plugin'
43
import isError from '../../../lib/is-error'
54
import { nodeFileTrace } from 'next/dist/compiled/@vercel/nft'
65
import type { NodeFileTraceReasons } from 'next/dist/compiled/@vercel/nft'
@@ -20,6 +19,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info'
2019
import { getPageFilePath } from '../../entries'
2120
import { resolveExternal } from '../../handle-externals'
2221
import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route'
22+
import { getCompilationSpan } from '../utils'
2323

2424
const PLUGIN_NAME = 'TraceEntryPointsPlugin'
2525
export const TRACE_IGNORES = [
@@ -576,6 +576,12 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
576576

577577
apply(compiler: webpack.Compiler) {
578578
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
579+
const compilationSpan =
580+
getCompilationSpan(compilation) || getCompilationSpan(compiler)!
581+
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
582+
'next-trace-entrypoint-plugin'
583+
)
584+
579585
compilation.hooks.processAssets.tapAsync(
580586
{
581587
name: PLUGIN_NAME,
@@ -633,10 +639,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
633639
}
634640
}
635641

636-
const compilationSpan = spans.get(compilation) || spans.get(compiler)!
637-
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
638-
'next-trace-entrypoint-plugin'
639-
)
640642
traceEntrypointsPluginSpan.traceFn(() => {
641643
compilation.hooks.processAssets.tapAsync(
642644
{

Diff for: packages/next/src/build/webpack/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import type {
55
NormalModule,
66
Module,
77
ModuleGraph,
8+
Compiler,
89
} from 'webpack'
910
import type { ModuleGraphConnection } from 'webpack'
1011
import { getAppLoader } from '../entries'
12+
import { spans as webpackCompilationSpans } from './plugins/profiling-plugin'
13+
import { compilationSpans as rspackCompilationSpans } from './plugins/rspack-profiling-plugin'
14+
import type { Span } from '../../trace'
1115

1216
export function traverseModules(
1317
compilation: Compilation,
@@ -114,3 +118,13 @@ export function getModuleReferencesInOrder(
114118
connections.sort((a, b) => a.index - b.index)
115119
return connections.map((c) => c.connection)
116120
}
121+
122+
export function getCompilationSpan(
123+
compilation: Compiler | Compilation
124+
): Span | undefined {
125+
const compilationSpans = process.env.NEXT_RSPACK
126+
? rspackCompilationSpans
127+
: webpackCompilationSpans
128+
129+
return compilationSpans.get(compilation)
130+
}

0 commit comments

Comments
 (0)