Skip to content

Commit

Permalink
fix compilation relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 17, 2021
1 parent 367f8d6 commit 0895d33
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
12 changes: 6 additions & 6 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export type ClientBuildManifest = Record<string, string[]>
// This function takes the asset map generated in BuildManifestPlugin and creates a
// 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)
const genClientManifestSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest'
)

Expand Down Expand Up @@ -115,8 +115,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)
const createAssetsSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-createassets'
)
return createAssetsSpan?.traceFn(() => {
Expand Down Expand Up @@ -229,7 +229,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
4 changes: 2 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,9 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync(
'NextJsBuildStats',
async (stats, callback) => {
const compilerSpan = spans.get(compiler)
const compilationSpan = spans.get(stats.compilation)
try {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats')
const writeStatsSpan = compilationSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => {
const statsJson = reduceSize(
Expand Down
8 changes: 4 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,8 @@ 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)
const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin'
)
cssMinimizerSpan.setAttribute('webpackVersion', 5)
Expand Down Expand Up @@ -114,8 +114,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)
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)!
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)!
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)!
const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin'
)
traceEntrypointsPluginSpan.traceFn(() =>
Expand Down
67 changes: 40 additions & 27 deletions packages/next/build/webpack/plugins/profiling-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ 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, 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 +43,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 +76,16 @@ export class ProfilingPlugin {
traceTopLevelHooks(compiler: any) {
this.traceHookPair(
'webpack-compilation',
isWebpack5 ? compiler.hooks.beforeCompile : compiler.hooks.compile,
compiler.hooks.make,
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)
moduleSpansByCompilation.set(compilation, new WeakMap())
},
}
)

Expand Down Expand Up @@ -115,8 +125,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 @@ -134,11 +144,12 @@ export class ProfilingPlugin {

const spanName = `build-module${moduleType ? `-${moduleType}` : ''}`
const issuerSpan: Span | undefined =
issuerModule && spans.get(issuerModule)
issuerModule &&
moduleSpansByCompilation.get(compilation)?.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)
Expand All @@ -147,7 +158,9 @@ export class ProfilingPlugin {
getNormalModuleLoaderHook(compilation).tap(
pluginName,
(loaderContext: any, module: any) => {
const moduleSpan = spans.get(module)
const moduleSpan = moduleSpansByCompilation
.get(compilation)
?.get(module)
loaderContext.currentTraceSpan = moduleSpan
}
)
Expand All @@ -161,7 +174,7 @@ export class ProfilingPlugin {
'webpack-compilation-seal',
compilation.hooks.seal,
compilation.hooks.afterSeal,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)

this.traceHookPair(
Expand All @@ -174,7 +187,7 @@ export class ProfilingPlugin {
request: entry.request,
}
},
parentSpan: () => spans.get(compiler)!,
parentSpan: () => spans.get(compilation)!,
}
)
}
Expand All @@ -183,37 +196,37 @@ export class ProfilingPlugin {
'webpack-compilation-chunk-graph',
compilation.hooks.beforeChunks,
compilation.hooks.afterChunks,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
this.traceHookPair(
'webpack-compilation-optimize',
compilation.hooks.optimize,
compilation.hooks.reviveModules,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
this.traceHookPair(
'webpack-compilation-optimize-modules',
compilation.hooks.optimizeModules,
compilation.hooks.afterOptimizeModules,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
this.traceHookPair(
'webpack-compilation-optimize-chunks',
compilation.hooks.optimizeChunks,
compilation.hooks.afterOptimizeChunks,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
this.traceHookPair(
'webpack-compilation-optimize-tree',
compilation.hooks.optimizeTree,
compilation.hooks.afterOptimizeTree,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
this.traceHookPair(
'webpack-compilation-hash',
compilation.hooks.beforeHash,
compilation.hooks.afterHash,
{ parentSpan: () => spans.get(compiler)! }
{ parentSpan: () => spans.get(compilation)! }
)
})
}
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)
const terserSpan = compilationSpan.traceChild(
'terser-webpack-plugin-optimize'
)
terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4)
terserSpan.setAttribute('compilationName', compilation.name)

Expand Down

0 comments on commit 0895d33

Please sign in to comment.