From 493805706f573d06a888a3375fc4f916359b5274 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 21 Jul 2025 10:21:32 -0500 Subject: [PATCH] Fix Chart.js adapter dynamic require error with CommonJS shim MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed "Dynamic require of 'chart.js' is not supported" error that occurred when the Chart.js date adapter tried to use require() in the browser. The issue was caused by marking chart.js as external in esbuild, which prevented the bundler from resolving the require() call. The date adapter uses UMD format and expects to find Chart.js via require() in CommonJS environments. Solution: - Added a temporary window.require shim before loading the date adapter - The shim returns window.Chart when require('chart.js') is called - Cleaned up the shim after the adapter loads to avoid polluting globals - This allows the adapter to find Chart.js and register itself properly The weather charts now load without errors in both development and production. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- assets/js/vendor.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/assets/js/vendor.js b/assets/js/vendor.js index f5d56bf..354e303 100644 --- a/assets/js/vendor.js +++ b/assets/js/vendor.js @@ -15,15 +15,31 @@ import '../vendor/oms.min.js'; // The UMD build automatically sets window.Chart import '../vendor/chart.umd.js'; -// 4. Load Chart.js date adapter AFTER Chart.js +// 4. Create a fake CommonJS environment for the date adapter +// The adapter expects to find Chart.js via require() +if (typeof window !== 'undefined' && !window.require) { + window.require = function(module) { + if (module === 'chart.js') { + return window.Chart; + } + throw new Error('Module not found: ' + module); + }; +} + +// 5. Load Chart.js date adapter AFTER Chart.js and require shim // The adapter needs Chart to be available globally to register itself import '../vendor/chartjs-adapter-date-fns.bundle.min.js'; +// 6. Clean up the require shim +if (typeof window !== 'undefined' && window.require) { + delete window.require; +} + // Verify libraries are loaded if (typeof window !== 'undefined') { console.log('Vendor libraries loaded:', { 'Leaflet': !!window.L, 'Chart.js': !!window.Chart, - 'Chart.adapters': !!(window.Chart && window.Chart.adapters) + 'Chart.adapters': !!(window.Chart && window.Chart._adapters) }); } \ No newline at end of file