Skip to content

Commit 46aaac9

Browse files
committed
Add performance polyfill for webpack dev mode
1 parent 330f172 commit 46aaac9

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

packages/nextjs/rollup.npm.config.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,20 @@ export default [
8888
},
8989
}),
9090
),
91+
...makeNPMConfigVariants(
92+
makeBaseNPMConfig({
93+
entrypoints: ['src/config/polyfills/perf_hooks.js'],
94+
95+
packageSpecificConfig: {
96+
output: {
97+
// Preserve the original file structure (i.e., so that everything is still relative to `src`)
98+
entryFileNames: 'config/polyfills/[name].js',
99+
100+
// make it so Rollup calms down about the fact that we're combining default and named exports
101+
exports: 'named',
102+
},
103+
},
104+
}),
105+
),
91106
...makeOtelLoaders('./build', 'sentry-node'),
92107
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Polyfill for Node.js perf_hooks module in edge runtime
2+
// This mirrors the polyfill from packages/vercel-edge/rollup.npm.config.mjs
3+
4+
// Ensure performance global is available
5+
if (typeof globalThis !== 'undefined' && globalThis.performance === undefined) {
6+
globalThis.performance = {
7+
timeOrigin: 0,
8+
now: function () {
9+
return Date.now();
10+
},
11+
};
12+
}
13+
14+
// Export the performance object for perf_hooks compatibility
15+
export const performance = globalThis.performance || {
16+
timeOrigin: 0,
17+
now: function () {
18+
return Date.now();
19+
},
20+
};
21+
22+
// Default export for CommonJS compatibility
23+
export default {
24+
performance,
25+
};

packages/nextjs/src/config/webpack.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ export function constructWebpackConfigFunction(
103103

104104
addOtelWarningIgnoreRule(newConfig);
105105

106+
// Add edge runtime polyfills when building for edge in dev mode
107+
if (runtime === 'edge' && isDev) {
108+
addEdgeRuntimePolyfills(newConfig, buildContext);
109+
}
110+
106111
let pagesDirPath: string | undefined;
107112
const maybePagesDirPath = path.join(projectDir, 'pages');
108113
const maybeSrcPagesDirPath = path.join(projectDir, 'src', 'pages');
@@ -865,6 +870,27 @@ function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules)
865870
}
866871
}
867872

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+
868894
function _getModules(projectDir: string): Record<string, string> {
869895
try {
870896
const packageJson = path.join(projectDir, 'package.json');

0 commit comments

Comments
 (0)