Giter Club home page Giter Club logo

minify's Introduction

Minify

Build Status Latest Stable Version Total Downloads License

With this package you can minify your existing stylesheet and JavaScript files for Laravel 10. This process can be a little tough, this package simplifies and automates this process.

For Laravel 5 - 9 please use version 1.x of this package.

For Laravel 4 please use ceesvanegmond/minify

Installation

Begin by installing this package through Composer.

{
    "require": {
        "devfactory/minify": "^2.0"
    }
}

After the package installation, the MinifyServiceProvider and Minify facade are automatically registered. You can use the Minify facade anywhere in your application.

To publish the config file:

php artisan vendor:publish --provider="Devfactory\Minify\MinifyServiceProvider" --tag="config"

Upgrade to v2

Minify version 2 is PHP 8.1+ and Laravel 10+ only.

Required upgrade changes

If the Devfactory\Minify\Contracts\MinifyInterface interface is implemented, make sure update your implementation according to the updated types and exceptions.

If the Devfactory\Minify\Providers\BaseProvider abstract class is used, make sure update your classes according to the updated types and exceptions.

The method Devfactory\Minify\Providers\StyleSheet#urlCorrection has been renamed to Devfactory\Minify\Providers\StyleSheet#getFileContentWithCorrectedUrls.

Rename the minify.config.php configuration file to minify.php.

Usage

Stylesheet

// app/views/hello.blade.php

<html>
    <head>
        ...
        {!! Minify::stylesheet('/css/main.css') !!}
        // or by passing multiple files
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css']) !!}
        // add custom attributes
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css'], ['foo' => 'bar']) !!}
        // add full uri of the resource
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css'])->withFullUrl() !!}
        {!! Minify::stylesheet(['//fonts.googleapis.com/css?family=Roboto']) !!}

        // minify and combine all stylesheet files in given folder
        {!! Minify::stylesheetDir('/css/') !!}
        // add custom attributes to minify and combine all stylesheet files in given folder
        {!! Minify::stylesheetDir('/css/', ['foo' => 'bar', 'defer' => true]) !!}
        // minify and combine all stylesheet files in given folder with full uri
        {!! Minify::stylesheetDir('/css/')->withFullUrl() !!}
    </head>
    ...
</html>

Javascript

// app/views/hello.blade.php

<html>
    <body>
    ...
    </body>
    {!! Minify::javascript('/js/jquery.js') !!}
    // or by passing multiple files
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js']) !!}
    // add custom attributes
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js'], ['bar' => 'baz']) !!}
    // add full uri of the resource
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js'])->withFullUrl() !!}
    {!! Minify::javascript(['//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js']) !!}

    // minify and combine all javascript files in given folder
    {!! Minify::javascriptDir('/js/') !!}
    // add custom attributes to minify and combine all javascript files in given folder
    {!! Minify::javascriptDir('/js/', ['bar' => 'baz', 'async' => true]) !!}
    // minify and combine all javascript files in given folder with full uri
    {!! Minify::javascriptDir('/js/')->withFullUrl() !!}
</html>

minify's People

Contributors

amalfra avatar andryshok avatar antal-levente avatar atyagi avatar ceesvanegmond avatar devdavido avatar gandalfthegreydev avatar jimmyko avatar jorygeerts avatar jrean avatar laravel-shift avatar luizpedone avatar markcameron avatar mcordingley avatar mean-cj avatar mlantz avatar muta244 avatar niranjan94 avatar oriceon avatar perryz avatar pjona avatar sirsquall avatar smgladkovskiy avatar spxcxcxxs21-gxst avatar tobi76 avatar xaockd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minify's Issues

Disk and name destination

Hi, thanks for the package, is been great using it, I was wondering if its possible to:

  1. Specify the destination of the (css,js) file to example a S3 bucket? this would be great and save a lot of time

  2. Config that the name is always the same, so every time we change or add a css file it doesn't output a new css file, instead it replaces the same and maintains only one

Hope you consider this, I prefer to use this instead of Elixir

Possible Bug Found: Integrating Minified Plupload File

We're using the Plupload library by @moxiecode and have found an error when integrating it with Plupload 2.1.2 in it's minified version.

When we run:

{!! Minify::javascript('/assets/js/plupload/plupload-2.1.2/plupload.full.min.js')->withFullUrl() !!}

We get the following Errors:

ErrorException in Minifier.php line 476: Unclosed string at position: 67069

We know:

  • It doesn't give any errors when we just include the file outside of Minify
  • If we pass an uncompressed version through Minify, we don't get any errors, but it's not actually minifing the file, just clearing it of any whitespace.

But when we put the .full.min.js file through Minify, the error is produced.

Here's the package link: https://github.com/moxiecode/plupload

onlyUrl and withFullUrl should be resetted to default

As Facades are running with a singleton, if we set onlyUrl or withFullUrl all subsequent calls to Minify would preserve the value of these two flag.

I think both should be resetted to false just before returning from the render function.

CSS camelCase issue

Original CSS:

:root { --brendColor: #F13D30;

Minified result:
:root{--brendcolor:#F13D30;

"brendColor" has been replaced to "brendcolor"

as well as
background-color: var(--menulevel2active);
has been removed

Minify files outside Laravel directory?

I have a lot of .js files that are shared across two different websites. The .js files are placed in a directory outside the Laravel folder. With a virtual directory link the .js assets can be retrieved by url.

I was looking for a way to minify and return these files by specifying a relative path to the original files but it generates an url that is incompatible with my virtual directory link.

For example this is what I have tried.
{!! Minify::javascript('\..\..\..\shared_js\createjs\game.js') !!}

The same file can be retrieved directly by this url:
\js\shared_js\createjs\game.js

Do you know if this is possible?

not minifying files js and css

installed package in my project and added service provider and Facade in config/app.php after that i ran command php artisan vendor:publish and published - Provider: Devfactory\Minify\MinifyServiceProvider in public css i added folder 'builds' with rights 775 in my blade i added {!! Minify::stylesheet(['/front/assets/css/base.css']) !!} but its not minifying files

php 7.1.3 laravel 5.6

Static syntax in blade examples

Examples in README.md call Minify::javascript() & stylesheet() statically, but these are defined as non-static functions in src/Minify.php. If these cannot be rewritten as static functions, then please provide examples of how to get & pass required $config & $environment args into new Devfactory\Minify\Minify($cfg, $env).stylesheet([]) for a correct syntax.
Not sure, if future versions of PHP will require such correctness, or other problems might ensue.

Breaking base64 images in CSS

Hello, first I want to say that I love this lib, it is simple, pragmatic, and does the job exactly as I want. Thank you!

But it appears that it breaks base64 encoded images, for example I have:
background-image: url("data:image/png;base64,iVBORw0KG...
which is transformed to
background-image:url("/css/data:image/png;base64,iVBOR...
which, as you guess, is not valid.

Can you help me with this?

Thanks

Error in the http Check

$http_response_header = array(false);

It seems some code is missing before the strpos, it will always trigger the exception.

if (strpos($http_response_header[0], '200') === false) {

Futhermore, the FileNotExistException use statement is missing in the header.

throw new FileNotExistException("File '{$file}' does not exist");

Class 'Devfactory\Minify\MinifyServiceProvider' not found

Hi

I'm sure it's my fault but I can't seem to figure it out.

Added in composer.json:

"require": {
        "laravel/framework": "5.0.*",
        "devfactory/minify": "1.0.*"
    },

Run composer update and got : "Installing devfactory/minify (1.0.7)"

Added in config/app.php:

'Devfactory\Minify\MinifyServiceProvider',

But getting: Class 'Devfactory\Minify\MinifyServiceProvider' not found

Css minify ' error

Hi DevFactory, Im having this little problem, hope one of you can help me, Im working with the new bootstrap4 and other css files, some are local, most are in S3, but Im getting this (') in the middle, before html... what can this be? I know the business is busy, but maybe you're seen this problem before, thanks (Im on L5.4)

@media print {
.hidden-print {
display: none !important;
}
}
' html, body { height.......

Config parameter for filename hash type

Hi,
currently we have problems using this nice minify implementation on multiple servers. The deployment process sets different timestamps to public/assets files, so the filename hash generated by filename and mtime is different on balanced servers. If there is no sticky loadbalancer session, the laravel deployment try to open unknown hash-filenames on the other servers.
It would be great, if there is an config parameter to define the hash-building. Possibly only use filenames, or filenames with timestamps (currently), or other thinks.
What do you think about that?

Thanks,
Tobias

Inline javascript/css

This is more feature request then issue.

It would be nice if it can minify (save to file) inline javascript/css (passed as string) , ex:
Minify::javascript("<script>alert('x')</scirpt>")

Laravel 5.1 Not working

Hi,
I followed each steps in the documentation. The minified css are generated in the /css/builds folder and loaded and present in the final html source file.
But they are not loaded by the browser!

I deleted all caches but it is not working.

Any idea? Did I miss something?

Laravel 5.1

Thank you.

[Question] What about multiple build paths?

I'm using this library and it's useful for most aspects. I separated public assets for both desktop and mobile. Example folder structure:

  • public
    • webv1
      • css
        • builds
      • js
        • builds
      • vendor
    • mobilev1
      • css
        • builds
      • js
        • builds
      • vendor

So I need multiple config options for css_build_path, css_url_path, js_build_path and js_url_path. Now I'm using like this but it's not suitable for my situation:

'css_build_path' => '/webv1/css/builds/'
'css_url_path' => '/webv1/css/builds/'

'js_build_path' => '/webv1/js/builds/'
'js_url_path' => '/webv1/js/builds/'

Is there a logical way to achieve this?

Paths not working when in sub-directory

I've installed the plugin successfully.

Path to application http://localhost/app

If I include the css-files without slash at the beginning:

{!! Minify::stylesheet([
      'assets/plugins/pace/pace-theme-flash.css'
    ]) !!}

I get the error message:
File '/Applications/XAMPP/xamppfiles/htdocs/app/publicassets/plugins/pace/pace-theme-flash.css' does not exist

If I add the slash I get no error message but the css-files are not loaded because the path is not correct:

http://localhost/assets/plugins/.....

Adding a base_url didn't help. What I am doing wrong?

Thank you!

Problem with UTF chars

On my minified JS file I have wrong statement:
varπ=Math.PI,ε=1e-6,d3_radians=π/ 180,d3_degrees=180 /π;

Oryginal line:
var π = Math.PI, ε = 1e-6, d3_radians = π / 180, d3_degrees = 180 / π;

Laravel 5.1
devfactory/minify 1.0.1

How does it work ?

Hi,

Maybe can you explain a bit how it works ?
I think it uses caching and looks at the last modification time of each css/js file to build a new minified version of each file, is that right ?
And it uses the laravel cache system ?

Thank you !
Allan

Minify type?

Hello,
Regarding js minify, your package minify process is make the js file into one line?
or its compress minify with changing the variables names etc. ?

Doesn't seem to do anything

I tried using this package, but it doesn't seem to work. It loads the files fine locally, but when I take local out of ignore_environments, nothing happens. It does not build the files that it should. Do I need to run a command to have it build the files?

How can i use in php view

I am try to add in php view not in php blade but its showing only <style></style> tag not the actual tag with url. Below is my code.

Controller
$css["css"] = array(url("/") . '/assets/css/web/register.css');

Php View
<?php if (isset($css)) { foreach ($css as $style) { echo \Minify::stylesheet($style); }} ?>

Configuration value calls wrong

Hi,

Instead of minify.config.* values only minify.* should be used.
I'm getting errors that config values are not found.

  protected function registerServices() {
    $this->app->bindShared('minify', function ($app) {
      return new Minify(
        array(
          'css_build_path' => config('minify.css_build_path'),
          'css_url_path' => config('minify.css_url_path'),
          'js_build_path' => config('minify.js_build_path'),
          'js_url_path' => config('minify.js_url_path'),
          'ignore_environments' => config('minify.ignore_environments'),
          'base_url' => config('minify.base_url'),
          'reverse_sort' => config('minify.reverse_sort'),
          'disable_mtime' => config('minify.disable_mtime'),
          'hash_salt' => config('minify.hash_salt'),
        ),
        $app->environment()
      );
    });
  }

Please have a look, Andrei. 

PHP 8

On PHP 8 it causes timeout when trying to minify JS files.

Issue when used on angular-material.js

When minify is being used, and angular-material.js is getting minified.

The line 21481 of angular-material.js ( v1.0.4)
var urlRegex = /[-a-zA-Z0-9@:%+.~#?&//=]{2,256}.[a-z]{2,4}\b(/[-a-zA-Z0-9@:%+.~#?&//=]*)?/i;

Gets compressed as

var urlRegex = /[-a-zA-Z0-9@:%+.~#?&//=]{2,256}.[a-z]{2,4}\b(/[-a-zA-Z0-9@:%+.~#?&

It is skipping the second instance of '//='

Minify does not work

I test in laravel 5 but after render i see Minify doesn't change anything
image
image
I checked in css/builds/ path but don't see any file
Please help me

What exactly are "custom attributes"?

        // add custom attributes
        {!! Minify::javascript(array('/js/jquery.js', '/js/jquery-ui.js'), array('bar' => 'baz')) !!}

Ive been looking through the sourcecode, but i coudn't quite figure out what the purpose is of "custom attributes", any help?

Specifying the order in which minified content is generated

The way that files are minified in a directory is a mystery to me. I think it prioritizes subdirectories first which is a problem.

If my directory structure is this:

  • /js/
  • /js/app/
  • /js/app/[multiple javascript files for my site depending on libraries]
  • /js/vendor/
  • /js/vendor/lib/[jquery and modernizr]
  • /js/vendor/plugin/[jquery plugins]

I have no way of specifying order. I need /vendor/lib/ first, /vendor/plugin/ second, and then /app/.

The only way to accomplish this is to add 3 different Minify instances which will create 3 requests.

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.