Simplify jqhtml class registration to use Manifest at runtime
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1831,13 +1831,6 @@ implode("\n", array_map(fn ($f) => ' - ' . str_replace(base_path() . '/', '',
|
||||
$files['js'][] = $manifest_file;
|
||||
}
|
||||
|
||||
// Generate JQHTML cache class registration (must come after manifest)
|
||||
// Registers all ES6 classes with jqhtml for proper data cache serialization
|
||||
$jqhtml_registration_file = $this->_create_jqhtml_class_registration($files['js'] ?? []);
|
||||
if ($jqhtml_registration_file) {
|
||||
$files['js'][] = $jqhtml_registration_file;
|
||||
}
|
||||
|
||||
// Generate route definitions for JavaScript
|
||||
$route_file = $this->_create_javascript_routes();
|
||||
if ($route_file) {
|
||||
@@ -2462,92 +2455,6 @@ implode("\n", array_map(fn ($f) => ' - ' . str_replace(base_path() . '/', '',
|
||||
return $this->_write_temp_file($js_code, 'js');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create JQHTML cache class registration code
|
||||
*
|
||||
* Registers all ES6 classes with jqhtml for proper serialization during data caching.
|
||||
* In data cache mode, jqhtml enforces hot/cold cache parity - any class instance stored
|
||||
* in this.data that is NOT registered will be converted to a plain object (properties
|
||||
* preserved but prototype methods lost).
|
||||
*
|
||||
* This code runs immediately after Manifest._define() so classes are available.
|
||||
*
|
||||
* @param array $js_files Array of JavaScript file paths in the bundle
|
||||
* @return string|null Path to temp file containing registration code, or null if no classes
|
||||
*/
|
||||
protected function _create_jqhtml_class_registration(array $js_files): ?string
|
||||
{
|
||||
$manifest_files = Manifest::get_all();
|
||||
$class_names = [];
|
||||
|
||||
// Collect all class names from JS files (same logic as _create_javascript_manifest)
|
||||
foreach ($js_files as $file) {
|
||||
// Skip temp files except auto-generated model classes
|
||||
if (str_contains($file, 'storage/rsx-tmp/')) {
|
||||
if (str_contains($file, 'bundle_generated_models_')) {
|
||||
$content = file_get_contents($file);
|
||||
if (preg_match_all('/class\s+([A-Za-z_][A-Za-z0-9_]*)\s+extends/', $content, $matches)) {
|
||||
foreach ($matches[1] as $class_name) {
|
||||
$class_names[] = $class_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle stub files
|
||||
if (str_contains($file, 'storage/rsx-build/js-stubs/') || str_contains($file, 'storage/rsx-build/js-model-stubs/')) {
|
||||
$stub_content = file_get_contents($file);
|
||||
$stub_metadata = $this->_extract_stub_class_info($stub_content);
|
||||
if (!empty($stub_metadata['class'])) {
|
||||
$class_names[] = $stub_metadata['class'];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get class name from manifest
|
||||
$relative = str_replace(base_path() . '/', '', $file);
|
||||
$file_data = $manifest_files[$relative] ?? null;
|
||||
|
||||
if ($file_data && !empty($file_data['class'])) {
|
||||
$class_name = $file_data['class'];
|
||||
// Skip 'object' as it's not a real class
|
||||
if (strtolower($class_name) !== 'object') {
|
||||
$class_names[] = $class_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no classes found, return null
|
||||
if (empty($class_names)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove duplicates and sort for deterministic output
|
||||
$class_names = array_unique($class_names);
|
||||
sort($class_names);
|
||||
|
||||
// Generate JavaScript code for jqhtml class registration
|
||||
$js_code = "// JQHTML Cache Class Registration - Generated by BundleCompiler\n";
|
||||
$js_code .= "// Registers all ES6 classes for proper serialization in jqhtml data caching.\n";
|
||||
$js_code .= "// Without registration, class instances in this.data become plain objects on cache restore.\n";
|
||||
$js_code .= "(function() {\n";
|
||||
$js_code .= " if (typeof jqhtml === 'undefined' || typeof jqhtml.register_cache_class !== 'function') {\n";
|
||||
$js_code .= " return; // jqhtml not available or doesn't support cache class registration\n";
|
||||
$js_code .= " }\n";
|
||||
|
||||
foreach ($class_names as $class_name) {
|
||||
// Check that class exists before registering (safety check)
|
||||
$js_code .= " if (typeof {$class_name} !== 'undefined') jqhtml.register_cache_class({$class_name});\n";
|
||||
}
|
||||
|
||||
$js_code .= "})();\n";
|
||||
|
||||
// Write to temporary file
|
||||
return $this->_write_temp_file($js_code, 'js');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create JavaScript runner for automatic class initialization
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user