https://github.com/dmhendricks/php-combine-scripts
A simple PHP script example to combine and minify multiple JS or CSS files passed via URI
https://github.com/dmhendricks/php-combine-scripts
combine minify minify-css minify-javascript php rewrite-urls
Last synced: about 1 month ago
JSON representation
A simple PHP script example to combine and minify multiple JS or CSS files passed via URI
- Host: GitHub
- URL: https://github.com/dmhendricks/php-combine-scripts
- Owner: dmhendricks
- License: apache-2.0
- Created: 2019-07-08T00:27:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-13T21:18:47.000Z (almost 6 years ago)
- Last Synced: 2025-04-10T12:07:18.323Z (about 1 month ago)
- Topics: combine, minify, minify-css, minify-javascript, php, rewrite-urls
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.danhendricks.com/?utm_source=github.com&utm_medium=campaign&utm_content=button&utm_campaign=dmhendricks%2Fphp-combine-scripts)
[](https://github.com/dmhendricks/php-combine-scripts/blob/master/LICENSE)
[](https://m.do.co/t/8a88362f5683?utm_source=github.com&utm_medium=referral&utm_content=button&utm_campaign=dmhendricks%2Fphp-combine-scripts)
[](https://twitter.com/danielhendricks)# PHP Combine Scripts
A simple PHP script to combine and minify multiple JS or CSS files passed via URI, inspired by [jsDelivr](https://www.jsdelivr.com/features/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fphp-combine-scripts#combine).
It was created for when you have multiple scripts at a static/CDN domain, allowing you to pick and bundle different libraries based on your current needs.
Generally, it is best to combine multiple assets into bundles using a task runner such as [webpack](https://webpack.js.org/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fphp-combine-scripts) or [Gulp](https://gulpjs.com/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fphp-combine-scripts), but it can be handy depending on your use case.
## Setup
1. Clone or unzip the files to a directory off of your web root. For example, `/combine/`. Change to that directory.
1. `composer install`
1. Rename `.env-sample` to `.env`. Set the `COMBINE_BASEDIR` to the directory that will contain your JS/CSS files that you want to allow combining/minification for.
1. Configure your web server to rewrite URLs, as shown in the examples below. Change the `/combine/` and `/minify/` paths as desired.### Nginx
```nginx
# Rewrite combine/minify URLs
rewrite ^/combine/(.*)$ /combine/index.php?scripts=$1;
rewrite ^/minify/(.*)$ /combine/index.php?minify=true&scripts=$1;
```### Apache
```apache
# Rewrite combine/minify URLs
RewriteRule "^/combine/(.*)$" "/combine/index.php?scripts=$1"
RewriteRule "^/minify/(.*)$" "/combine/index.php?minify=true&scripts=$1"
```## Usage Example
For example, let's say that you set your `COMBINE_BASEDIR` to `/var/www/html/scipts/`. Withing this directory, you might have some JS or CSS libraries. We'll combine [Font Awesome](https://fontawesome.com/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fphp-combine-scripts) with the [v4 shims](https://fontawesome.com/how-to-use/on-the-web/setup/upgrading-from-version-4/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fphp-combine-scripts#shims) as an example, assuming that you have already [extracted them](https://github.com/FortAwesome/Font-Awesome/tree/master/css) to `/var/www/html/scipts/dist/css/`.
To combine or minify multiple scripts, separate them with commas:
```html
```
Of course, minification is not necessary for scripts that you already have minified versions for. It is merely an option for libraries/scripts that do not contain a minified version. If you have minified versions avaible, it is recommended to simply use the "combine" method as it consumes less system resources.