nickdekruijk / minify
A package for automaticaly minifying CSS, SCSS and Javascript for Laravel
Requires
- php: ^8.2
- scssphp/scssphp: ^2.0
- tedivm/jshrink: ^1.3
Requires (Dev)
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.0|^12.0
README
A simple package to minify CSS/SCSS and Javascript on the fly without the need of tools like Laravel Mix or Webpack. It combines all stylesheet files or javascript files into a single, minified file with simple but effective cachebusting with filemtime().
Version 2 is a completely new package (version 1 is archived here) using scssphp/scssphp and tedivm/jshrink. Because minify now replaces natxet/cssmin with scssphp it can now compile SASS/SCSS code too!
Installation
Begin by installing this package with composer.
composer require nickdekruijk/minify
Upgrading from 3.x to 4.0
Two defaults changed. If you never published config/minify.php, both apply to you.
Import paths are now absolute. They used to be relative ('../resources/sass/'), which
only resolves when the working directory is public/ — true for a web request, false for
anything run through artisan. So a compile from the console looked for your stylesheets one
directory above the project and threw not found within importPaths. The defaults now use
resource_path() and public_path(), which resolve the same files from any working
directory. A published config keeps whatever is in it, so if yours still has relative paths
and you run minify from the console, make them absolute the same way.
testing is no longer skipped. With it in skip_environment, minify did not compile
during tests at all: it pointed at the output file and hoped it was there. That made a test
suite depend on a build left behind by an earlier browser request — green locally, and
either absent or stale on a fresh CI checkout. Tests now compile from your sources. To keep
the old behaviour, publish the config and add 'testing' back to skip_environment.
The default arguments of Minify::stylesheet() and Minify::javascript() dropped their
relative prefix, from ['../resources/sass/app.scss'] to ['app.scss'] and from
['../resources/js/app.js'] to ['app.js']. Both resolve through the import paths, so this
works whether your config is relative or absolute. Calls that pass files explicitly are
unaffected.
Upgrading from 1.x
When upgrading change your projects composer.json to require nickdekruijk/minify with at least version "^2.0" and run composer update.
If you use .gitignore to ignore the old builds in js/builds and css/builds dont' forget to remove them from your .gitignore file and delete all obsolete build .css and .js files.
You may also need to change the Minify::stylesheet and Minify::javascript calls in your code/views since pathname might change depending on your configuration.
Laravel installation
Publish the config file if the defaults doesn't suite your needs:
php artisan vendor:publish --provider="NickDeKruijk\Minify\ServiceProvider" --tag=config
Stylesheet
// app/views/hello.blade.php <html> <head> ... {!! Minify::stylesheet(['lightbox.css', 'fonts.css', 'styles.css']) !!} </head> ... </html>
Javascript
// app/views/hello.blade.php <html> <body> ... {!! Minify::javascript(['lazyload.min.js', 'scripts.js']) !!} <!-- Or: --> {!! Minify::javascript(['https://cdn.jsdelivr.net/npm/vanilla-lazyload@12.4.0/dist/lazyload.min.js', 'scripts.js') !!} </body> </html>
Config
See the config file at /config/minify.php