Giter Site home page Giter Site logo

dmhendricks / php-combine-scripts Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 14 KB

A simple PHP script example to combine and minify multiple JS or CSS files passed via URI

License: Apache License 2.0

PHP 100.00%
php combine minify rewrite-urls minify-css minify-javascript

php-combine-scripts's Introduction

Author License DigitalOcean Twitter

PHP Combine Scripts

A simple PHP script to combine and minify multiple JS or CSS files passed via URI, inspired by jsDelivr.

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 or Gulp, 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.
  2. composer install
  3. 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.
  4. Configure your web server to rewrite URLs, as shown in the examples below. Change the /combine/ and /minify/ paths as desired.

Nginx

# Rewrite combine/minify URLs
rewrite ^/combine/(.*)$ /combine/index.php?scripts=$1;
rewrite ^/minify/(.*)$ /combine/index.php?minify=true&scripts=$1;

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 with the v4 shims as an example, assuming that you have already extracted them to /var/www/html/scipts/dist/css/.

To combine or minify multiple scripts, separate them with commas:

<!-- Combine example -->
<link rel="stylesheet" href="https://example.com/combine/dist/css/all.min.css,dist/css/v4-shims.min.css" />

<!-- Minify example -->
<link rel="stylesheet" href="https://example.com/minify/dist/css/all.css,dist/css/v4-shims.css" />

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.

php-combine-scripts's People

Contributors

dmhendricks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.