Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compilation relationship #29174

Merged
merged 7 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export type ClientBuildManifest = Record<string, string[]>
// reduced version to send to the client.
function generateClientManifest(
compiler: any,
compilation: any,
assetMap: BuildManifest,
rewrites: CustomRoutes['rewrites']
): string {
const compilerSpan = spans.get(compiler)
const genClientManifestSpan = compilerSpan?.traceChild(
const compilationSpan = spans.get(compilation) || spans.get(compiler)
const genClientManifestSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest'
)

Expand Down Expand Up @@ -115,8 +116,8 @@ export default class BuildManifestPlugin {
}

createAssets(compiler: any, compilation: any, assets: any) {
const compilerSpan = spans.get(compiler)
const createAssetsSpan = compilerSpan?.traceChild(
const compilationSpan = spans.get(compilation) || spans.get(compiler)
const createAssetsSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-createassets'
)
return createAssetsSpan?.traceFn(() => {
Expand Down Expand Up @@ -230,6 +231,7 @@ export default class BuildManifestPlugin {
assets[clientManifestPath] = new sources.RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest(
compiler,
compilation,
assetMap,
this.rewrites
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
Expand Down
5 changes: 3 additions & 2 deletions packages/next/build/webpack/plugins/build-stats-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync(
'NextJsBuildStats',
async (stats, callback) => {
const compilerSpan = spans.get(compiler)
const compilationSpan =
spans.get(stats.compilation) || spans.get(compiler)
try {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats')
const writeStatsSpan = compilationSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => {
const statsJson = reduceSize(
Expand Down
9 changes: 5 additions & 4 deletions packages/next/build/webpack/plugins/css-minimizer-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export class CssMinimizerPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
async (assets: any) => {
const compilerSpan = spans.get(compiler)
const cssMinimizerSpan = compilerSpan!.traceChild(
const compilationSpan =
spans.get(compilation) || spans.get(compiler)
const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin'
)
cssMinimizerSpan.setAttribute('webpackVersion', 5)
Expand Down Expand Up @@ -114,8 +115,8 @@ export class CssMinimizerPlugin {
compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => {
const compilerSpan = spans.get(compiler)
const cssMinimizerSpan = compilerSpan!.traceChild(
const compilationSpan = spans.get(compilation) || spans.get(compiler)
const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin'
)
cssMinimizerSpan.setAttribute('webpackVersion', 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
apply(compiler: webpack.Compiler) {
if (isWebpack5) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilerSpan = spans.get(compiler)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
const compilationSpan = spans.get(compilation) || spans.get(compiler)!
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
traceEntrypointsPluginSpan.traceFn(() => {
Expand All @@ -327,8 +327,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
})
} else {
compiler.hooks.emit.tap(PLUGIN_NAME, (compilation: any) => {
const compilerSpan = spans.get(compiler)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
const compilationSpan = spans.get(compilation)! || spans.get(compiler)
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
traceEntrypointsPluginSpan.traceFn(() => {
Expand All @@ -341,8 +341,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
})

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilerSpan = spans.get(compiler)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
const compilationSpan = spans.get(compilation)! || spans.get(compiler)
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
traceEntrypointsPluginSpan.traceFn(() =>
Expand Down
75 changes: 46 additions & 29 deletions packages/next/build/webpack/plugins/profiling-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { Span } from '../../../trace'

const pluginName = 'ProfilingPlugin'
export const spans = new WeakMap<any, Span>()
export const spans = new WeakMap<
webpack.compilation.Compilation | webpack.Compiler,
Span
>()
const moduleSpansByCompilation = new WeakMap<
webpack.compilation.Compilation,
WeakMap<webpack.Module, Span>
>()
export const webpackInvalidSpans = new WeakMap<any, Span>()

function getNormalModuleLoaderHook(compilation: any) {
Expand Down Expand Up @@ -39,21 +46,24 @@ export class ProfilingPlugin {
}: {
parentSpan?: () => Span
attrs?: any
onStart?: (span: Span) => void
onStart?: (span: Span, ...params: any[]) => void
onStop?: () => void
} = {}
) {
let span: Span | undefined
startHook.tap(pluginName, (...params: any[]) => {
const name = typeof spanName === 'function' ? spanName() : spanName
const attributes = attrs ? attrs(...params) : attrs
span = parentSpan
? parentSpan().traceChild(name, attributes)
: this.runWebpackSpan.traceChild(name, attributes)

if (onStart) onStart(span)
})
stopHook.tap(pluginName, () => {
startHook.tap(
{ name: pluginName, stage: -Infinity },
(...params: any[]) => {
const name = typeof spanName === 'function' ? spanName() : spanName
const attributes = attrs ? attrs(...params) : attrs
span = parentSpan
? parentSpan().traceChild(name, attributes)
: this.runWebpackSpan.traceChild(name, attributes)

if (onStart) onStart(span, ...params)
}
)
stopHook.tap({ name: pluginName, stage: Infinity }, () => {
// `stopHook` may be triggered when `startHook` has not in cases
// where `stopHook` is used as the terminating event for more
// than one pair of hooks.
Expand All @@ -69,13 +79,17 @@ export class ProfilingPlugin {
traceTopLevelHooks(compiler: any) {
this.traceHookPair(
'webpack-compilation',
isWebpack5 ? compiler.hooks.beforeCompile : compiler.hooks.compile,
compiler.hooks.compilation,
isWebpack5 ? compiler.hooks.afterCompile : compiler.hooks.done,
{
parentSpan: () =>
webpackInvalidSpans.get(compiler) || this.runWebpackSpan,
attrs: () => ({ name: compiler.name }),
onStart: (span) => spans.set(compiler, span),
onStart: (span, compilation) => {
spans.set(compilation, span)
spans.set(compiler, span)
moduleSpansByCompilation.set(compilation, new WeakMap())
},
}
)

Expand Down Expand Up @@ -115,8 +129,8 @@ export class ProfilingPlugin {

compiler.hooks.compilation.tap(pluginName, (compilation: any) => {
compilation.hooks.buildModule.tap(pluginName, (module: any) => {
const compilerSpan = spans.get(compiler)
if (!compilerSpan) {
const compilationSpan = spans.get(compilation)
if (!compilationSpan) {
return
}

Expand All @@ -132,36 +146,39 @@ export class ProfilingPlugin {

let span: Span

const moduleSpans = moduleSpansByCompilation.get(compilation)
const spanName = `build-module${moduleType ? `-${moduleType}` : ''}`
const issuerSpan: Span | undefined =
issuerModule && spans.get(issuerModule)
issuerModule && moduleSpans?.get(issuerModule)
if (issuerSpan) {
span = issuerSpan.traceChild(spanName)
} else {
span = compilerSpan.traceChild(spanName)
span = compilationSpan.traceChild(spanName)
}
span.setAttribute('name', module.userRequest)
spans.set(module, span)
moduleSpans!.set(module, span)
})

getNormalModuleLoaderHook(compilation).tap(
pluginName,
(loaderContext: any, module: any) => {
const moduleSpan = spans.get(module)
const moduleSpan = moduleSpansByCompilation
.get(compilation)
?.get(module)
loaderContext.currentTraceSpan = moduleSpan
}
)

compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
spans.get(module)?.stop()
moduleSpansByCompilation?.get(compilation)?.get(module)?.stop()
})

if (isWebpack5) {
this.traceHookPair(
'webpack-compilation-seal',
compilation.hooks.seal,
compilation.hooks.afterSeal,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)

this.traceHookPair(
Expand All @@ -174,7 +191,7 @@ export class ProfilingPlugin {
request: entry.request,
}
},
parentSpan: () => spans.get(compiler)!,
parentSpan: () => spans.get(compilation)!,
}
)
}
Expand All @@ -183,37 +200,37 @@ export class ProfilingPlugin {
'webpack-compilation-chunk-graph',
compilation.hooks.beforeChunks,
compilation.hooks.afterChunks,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
this.traceHookPair(
'webpack-compilation-optimize',
compilation.hooks.optimize,
compilation.hooks.reviveModules,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
this.traceHookPair(
'webpack-compilation-optimize-modules',
compilation.hooks.optimizeModules,
compilation.hooks.afterOptimizeModules,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
this.traceHookPair(
'webpack-compilation-optimize-chunks',
compilation.hooks.optimizeChunks,
compilation.hooks.afterOptimizeChunks,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
this.traceHookPair(
'webpack-compilation-optimize-tree',
compilation.hooks.optimizeTree,
compilation.hooks.afterOptimizeTree,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
this.traceHookPair(
'webpack-compilation-hash',
compilation.hooks.beforeHash,
compilation.hooks.afterHash,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export class TerserPlugin {
cache,
{ SourceMapSource, RawSource }
) {
const compilerSpan = spans.get(compiler)
const terserSpan = compilerSpan.traceChild('terser-webpack-plugin-optimize')
const compilationSpan = spans.get(compilation) || spans.get(compiler)
const terserSpan = compilationSpan.traceChild(
'terser-webpack-plugin-optimize'
)
terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4)
terserSpan.setAttribute('compilationName', compilation.name)

Expand Down