softberg / quantum-php-core

Quantum PHP Framework
http://quantumphp.io
MIT License
31 stars 13 forks source link

Asset library enhancements #174

Open armanist opened 2 weeks ago

armanist commented 2 weeks ago

Currently, assets in the application are loaded individually, resulting in multiple requests for CSS and JS files. To improve performance, we need to implement a feature that combines and minifies multiple CSS and JS assets into single files, similar to functionality provided by build tools like Gulp. This will reduce HTTP requests and optimize loading times.

The new feature should:

Combine multiple CSS files into a single minified CSS file. Combine multiple JS files into a single minified JS file. Dynamically generate unique filenames based on the assets’ hash to handle cache invalidation. Implement caching to avoid regenerating files unless the assets change. Provide an option to toggle this feature on or off for development vs. production environments. Acceptance Criteria:

New Method in AssetManager:

Add a compileAssets(int $type): string method that: Accepts a type parameter (CSS or JS). Generates a hash based on the asset paths to create a unique filename. Checks if a combined file already exists for this hash, and if so, returns its URL. If not, concatenates and minifies the assets, saves the minified output to a file, and returns the URL. Uses a third-party minification library (e.g., MatthiasMullie\Minify). Update the dump Method:

Modify dump(int $type) in AssetManager to check if asset compilation is enabled. If enabled, use compileAssets to get the URL of the combined file and output a single link/script tag. If not, fall back to outputting individual tags for each asset. Configuration Toggle:

Add a configuration option to enable/disable asset compilation for different environments. Cache Invalidation:

Ensure that when assets are updated, the hash changes, generating a new filename for cache busting. Error Handling and Logging:

Handle any errors from the minification process gracefully. Log errors to assist with debugging if compilation fails. Update View Helper assets():

Ensure the assets() helper function works seamlessly with the updated dump method, outputting either the combined or individual asset files as configured.