From 90e2b77887b9bf5b56c73977c292e06b0a82f6c7 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 18 Jan 2021 11:37:51 +0100 Subject: [PATCH 1/3] Fix webpack 5 warning + disabling of profiling --- .../build/webpack/plugins/profiling-plugin.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/next/build/webpack/plugins/profiling-plugin.ts b/packages/next/build/webpack/plugins/profiling-plugin.ts index 23c9272c363f69f..ac2a2ea5e5ff04d 100644 --- a/packages/next/build/webpack/plugins/profiling-plugin.ts +++ b/packages/next/build/webpack/plugins/profiling-plugin.ts @@ -1,4 +1,5 @@ import { tracer } from '../../tracer' +import { isWebpack5 } from 'next/dist/compiled/webpack/webpack' const pluginName = 'ProfilingPlugin' @@ -7,7 +8,12 @@ export const spans = new WeakMap() export class ProfilingPlugin { apply(compiler: any) { // Only enabled when instrumentation is loaded - if (!tracer.getCurrentSpan()) { + const currentSpan = tracer.getCurrentSpan() + console.log({ + currentSpan, + isRecording: currentSpan?.isRecording(), + }) + if (!currentSpan || !currentSpan.isRecording()) { return } @@ -29,13 +35,14 @@ export class ProfilingPlugin { }) }) - compilation.hooks.normalModuleLoader.tap( - pluginName, - (loaderContext: any, module: any) => { - const parentSpan = spans.get(module) - loaderContext.currentTraceSpan = parentSpan - } - ) + const hook = isWebpack5 + ? // @ts-ignore TODO: Webpack 5 types + webpack.NormalModule.getCompilationHooks(compilation).loader + : compilation.hooks.normalModuleLoader + hook.tap(pluginName, (loaderContext: any, module: any) => { + const parentSpan = spans.get(module) + loaderContext.currentTraceSpan = parentSpan + }) compilation.hooks.succeedModule.tap(pluginName, (module: any) => { spans.get(module).end() From 769854deca9c4649bfbe4a04f637a0a6fa0acf0c Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 18 Jan 2021 15:18:11 +0100 Subject: [PATCH 2/3] Update import --- packages/next/build/webpack/plugins/profiling-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/build/webpack/plugins/profiling-plugin.ts b/packages/next/build/webpack/plugins/profiling-plugin.ts index ac2a2ea5e5ff04d..bd58574ab2d11c2 100644 --- a/packages/next/build/webpack/plugins/profiling-plugin.ts +++ b/packages/next/build/webpack/plugins/profiling-plugin.ts @@ -1,5 +1,5 @@ import { tracer } from '../../tracer' -import { isWebpack5 } from 'next/dist/compiled/webpack/webpack' +import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack' const pluginName = 'ProfilingPlugin' From 8758137758ee4e9d8c54d6a235c2c9e04c3e907d Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 18 Jan 2021 16:56:35 +0100 Subject: [PATCH 3/3] Fix ts-ignore not being detected --- .../build/webpack/plugins/profiling-plugin.ts | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/next/build/webpack/plugins/profiling-plugin.ts b/packages/next/build/webpack/plugins/profiling-plugin.ts index bd58574ab2d11c2..6dee491cd8499d3 100644 --- a/packages/next/build/webpack/plugins/profiling-plugin.ts +++ b/packages/next/build/webpack/plugins/profiling-plugin.ts @@ -5,6 +5,15 @@ const pluginName = 'ProfilingPlugin' export const spans = new WeakMap() +function getNormalModuleLoaderHook(compilation: any) { + if (isWebpack5) { + // @ts-ignore TODO: Remove ignore when webpack 5 is stable + return webpack.NormalModule.getCompilationHooks(compilation).loader + } + + return compilation.hooks.normalModuleLoader +} + export class ProfilingPlugin { apply(compiler: any) { // Only enabled when instrumentation is loaded @@ -35,14 +44,13 @@ export class ProfilingPlugin { }) }) - const hook = isWebpack5 - ? // @ts-ignore TODO: Webpack 5 types - webpack.NormalModule.getCompilationHooks(compilation).loader - : compilation.hooks.normalModuleLoader - hook.tap(pluginName, (loaderContext: any, module: any) => { - const parentSpan = spans.get(module) - loaderContext.currentTraceSpan = parentSpan - }) + getNormalModuleLoaderHook(compilation).tap( + pluginName, + (loaderContext: any, module: any) => { + const parentSpan = spans.get(module) + loaderContext.currentTraceSpan = parentSpan + } + ) compilation.hooks.succeedModule.tap(pluginName, (module: any) => { spans.get(module).end()