UNPKG

gebeya-dala-error-fixer

Version:

Automatic runtime error detection and fix suggestions for Next.js applications with config-based setup

61 lines (60 loc) 2.52 kB
"use strict"; /** * Next.js Plugin for automatic error detection integration * Usage: Add to next.config.js */ const path = require('path'); function createErrorDetectorPlugin(userConfig = {}) { return (nextConfig = {}) => { const defaultConfig = { enabled: true, autoShow: true, position: 'top-right', environment: 'development', theme: 'dark', fixItButton: { enabled: true } }; const config = { ...defaultConfig, ...userConfig }; // Only enable in development by default if (config.environment === 'development' && process.env.NODE_ENV !== 'development') { return nextConfig; } // Merge with existing Next.js config const mergedConfig = { ...nextConfig, webpack: (webpackConfig, { buildId, dev, isServer, defaultLoaders, webpack }) => { // Add error detector configuration as a global variable webpackConfig.plugins.push(new webpack.DefinePlugin({ __ERROR_DETECTOR_CONFIG__: JSON.stringify(config) })); // Add error detector as entry point for client-side if (!isServer) { const originalEntry = webpackConfig.entry; webpackConfig.entry = async () => { const entries = await originalEntry(); // Add error detector to all client entries Object.keys(entries).forEach(key => { if (key.startsWith('pages/') || key === 'main' || key === 'main-app') { entries[key] = [ 'gebeya-dala-error-fixer/dist/auto-init', ...entries[key] ]; } }); return entries; }; } // Call user's webpack config if provided if (typeof nextConfig.webpack === 'function') { return nextConfig.webpack(webpackConfig, { buildId, dev, isServer, defaultLoaders, webpack }); } return webpackConfig; } }; return mergedConfig; }; } module.exports = createErrorDetectorPlugin; module.exports.createErrorDetectorPlugin = createErrorDetectorPlugin;