@@ -103,6 +103,11 @@ export function constructWebpackConfigFunction(
103
103
104
104
addOtelWarningIgnoreRule ( newConfig ) ;
105
105
106
+ // Add edge runtime polyfills when building for edge in dev mode
107
+ if ( runtime === 'edge' && isDev ) {
108
+ addEdgeRuntimePolyfills ( newConfig , buildContext ) ;
109
+ }
110
+
106
111
let pagesDirPath : string | undefined ;
107
112
const maybePagesDirPath = path . join ( projectDir , 'pages' ) ;
108
113
const maybeSrcPagesDirPath = path . join ( projectDir , 'src' , 'pages' ) ;
@@ -865,6 +870,27 @@ function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules)
865
870
}
866
871
}
867
872
873
+ function addEdgeRuntimePolyfills ( newConfig : WebpackConfigObjectWithModuleRules , buildContext : BuildContext ) : void {
874
+ const { webpack } = buildContext ;
875
+
876
+ // Use ProvidePlugin to inject performance global only when accessed
877
+ newConfig . plugins = newConfig . plugins || [ ] ;
878
+ newConfig . plugins . push (
879
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
880
+ new ( webpack as any ) . ProvidePlugin ( {
881
+ performance : [ path . resolve ( __dirname , 'polyfills' , 'perf_hooks.js' ) , 'performance' ] ,
882
+ } ) ,
883
+ ) ;
884
+
885
+ // Add module resolution aliases for problematic Node.js modules in edge runtime
886
+ newConfig . resolve = newConfig . resolve || { } ;
887
+ newConfig . resolve . alias = {
888
+ ...newConfig . resolve . alias ,
889
+ // Redirect perf_hooks imports to a polyfilled version
890
+ perf_hooks : path . resolve ( __dirname , 'polyfills' , 'perf_hooks.js' ) ,
891
+ } ;
892
+ }
893
+
868
894
function _getModules ( projectDir : string ) : Record < string , string > {
869
895
try {
870
896
const packageJson = path . join ( projectDir , 'package.json' ) ;
0 commit comments