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 bd17c77
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
8 changes: 4 additions & 4 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ 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 compilerSpan = spans.get(compilation)
const genClientManifestSpan = compilerSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest'
)
Expand Down Expand Up @@ -115,7 +115,7 @@ export default class BuildManifestPlugin {
}

createAssets(compiler: any, compilation: any, assets: any) {
const compilerSpan = spans.get(compiler)
const compilerSpan = spans.get(compilation)
const createAssetsSpan = compilerSpan?.traceChild(
'NextJsBuildManifest-createassets'
)
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
2 changes: 1 addition & 1 deletion packages/next/build/webpack/plugins/build-stats-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync(
'NextJsBuildStats',
async (stats, callback) => {
const compilerSpan = spans.get(compiler)
const compilerSpan = spans.get(stats.compilation)
try {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/plugins/css-minimizer-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class CssMinimizerPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
async (assets: any) => {
const compilerSpan = spans.get(compiler)
const compilerSpan = spans.get(compilation)
const cssMinimizerSpan = compilerSpan!.traceChild(
'css-minimizer-plugin'
)
Expand Down Expand Up @@ -114,7 +114,7 @@ export class CssMinimizerPlugin {
compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => {
const compilerSpan = spans.get(compiler)
const compilerSpan = spans.get(compilation)
const cssMinimizerSpan = compilerSpan!.traceChild(
'css-minimizer-plugin'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ 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 compilerSpan = spans.get(compilation)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
'next-trace-entrypoint-plugin'
)
Expand All @@ -327,7 +327,7 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
})
} else {
compiler.hooks.emit.tap(PLUGIN_NAME, (compilation: any) => {
const compilerSpan = spans.get(compiler)!
const compilerSpan = spans.get(compilation)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
'next-trace-entrypoint-plugin'
)
Expand All @@ -341,7 +341,7 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
})

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilerSpan = spans.get(compiler)!
const compilerSpan = spans.get(compilation)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild(
'next-trace-entrypoint-plugin'
)
Expand Down
56 changes: 31 additions & 25 deletions packages/next/build/webpack/plugins/profiling-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ 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.Module,
Span
>()
export const webpackInvalidSpans = new WeakMap<any, Span>()

function getNormalModuleLoaderHook(compilation: any) {
Expand Down Expand Up @@ -39,21 +42,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 +75,13 @@ 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),
}
)

Expand Down Expand Up @@ -115,8 +121,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 @@ -138,7 +144,7 @@ export class ProfilingPlugin {
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 @@ -161,7 +167,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 +180,7 @@ export class ProfilingPlugin {
request: entry.request,
}
},
parentSpan: () => spans.get(compiler)!,
parentSpan: () => spans.get(compilation)!,
}
)
}
Expand All @@ -183,37 +189,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,7 +122,7 @@ export class TerserPlugin {
cache,
{ SourceMapSource, RawSource }
) {
const compilerSpan = spans.get(compiler)
const compilerSpan = spans.get(compilation)
const terserSpan = compilerSpan.traceChild('terser-webpack-plugin-optimize')
terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4)
terserSpan.setAttribute('compilationName', compilation.name)
Expand Down

0 comments on commit bd17c77

Please sign in to comment.