2017 © Pedro PelĂĄez
 

library laravel-assets

Asset management for Laravel

image

fisharebest/laravel-assets

Asset management for Laravel

  • Friday, September 1, 2017
  • by fisharebest
  • Repository
  • 2 Watchers
  • 11 Stars
  • 2,997 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 15 Versions
  • 12 % Grown

The README.md

Latest Stable Version Unit tests Coverage Status SymfonyInsight Scrutinizer Code Quality, (*1)

laravel-assets

Simple, flexible asset management for Laravel 5 - 8. Combine and minify your CSS and JS files to make your website faster., (*2)

Installation

Add the dependency to composer.json:, (*3)

composer require fisharebest/laravel-assets

Starting with Laravel 5.5, packages are discovered automatically. For earlier versions, you must add the service provider and facade to config/app.php., (*4)

return [
    'providers' => [
        Fisharebest\LaravelAssets\AssetsServiceProvider::class,
    ],
    'aliases' => [
        'Assets' => Fisharebest\LaravelAssets\AssetsFacade::class,
    ],
]

Create a configuration file, config/assets.php, containing default values. Edit the settings in this file to match your project’s directory structure., (*5)

$ php artisan vendor:publish --provider="Fisharebest\LaravelAssets\AssetsServiceProvider"

Step 1. How to add assets

You would usually add assets in each of your templates (layouts, views, partials, etc.) that requires them., (*6)

<!-- resources/views/layouts/master.blade.php -->
<?php Assets::add(['jquery', 'bootstrap', 'global.js', 'style.css', 'analytics.js']) ?>
<!-- the rest of your view ... -->
<!-- resources/views/pages/list.blade.php -->
<?php Assets::add('list.js') ?>
<!-- the rest of your view ... -->

Of course, you could also add assets anywhere you choose; controllers, helpers, etc., (*7)

As well as individual files, you can add named collections of files. These are defined in config/assets.php., (*8)

Where you have dependencies, you should list the files in the order they should be loaded. For example, if list.js depends on jQuery, you would specify jQuery before list.js., (*9)

<!-- resources/views/pages/list.blade.php -->
<?php Assets::add(['jquery', 'list.js']) ?>
<!-- the rest of your view ... -->

Duplicates are ignored, so you can add jQuery to each view that uses it and it will only be rendered once., (*10)

It is conventional to render CSS assets in the <head> element, and JS assets at the end of the <body> element., (*11)

<!-- resources/views/layouts/master.blade.php -->
<html>
    <head>
        {!! Assets::css() !!}
    </head>
    <body>
        ...
        {!! Assets::js() !!}
    </body>
</html>

But what if


What if my assets don't have a .js or .css extension?

Specify the type as a parameter when adding the assets. For example,, (*12)

Assets::add('http://example.com/script?parameter', 'js')

What if I want to divide my assets into separate groups?

Specify the group as a parameter when adding and rendering assets., (*13)

<!-- resources/views/layouts/master.blade.php -->
<?php Assets::add('jquery.js') ?>
<?php Assets::add('ie8.js', null, 'ie8') ?>
<?php Assets::add('analytics.js', null, 'head-script') ?>
<html>
    <head>
        ...
        <!--[if lte IE 8]>{!! Assets::js('ie8') !!}<![endif]-->
        {!! Assets::js('head-script') !!}
    </head>
    <body>
        ...
        {!! Assets::js() !!}
    </body>
</html>

What if I want to add additional attributes to the style/script?

Specify a list of attributes as an argument to the render functions., (*14)

{!! Assets::css('print', ['media' => 'print']) !!}
{!! Assets::js('analytics', ['async']) !!}

What if I my asset files are tiny?

There's a configuration option inline_threshold. Any asset file smaller than this number of bytes will be rendered inline, thus saving an HTTP request., (*15)

What if I want to change the configuration at runtime?

Configuration can be changed at any time. It only takes effect when the assets are rendered., (*16)

Assets::setGzipStatic(6);
Assets::css(); // will create compressed assets

What if I want to use my own minifier?

Write your own filter (implement Fisharebest\LaravelAssets\Filters\FilterInterface) and specify it in the configuration file config/assets.php. Use one of the existing filters as a template., (*17)

What if I want to use a CDN or a cookie-free domain?

Specify a URL in the destination_url setting which corresponds to the folder given in destination., (*18)

// config/assets.php
return [
    'destination'     => 'min',                   // We create assets here
    'destination_url' => 'http://my-cdn.com/min', // Users read assets from here
]

What if I need to copy my pipelined assets to an external server?

Write your own notifier (implement Fisharebest\LaravelAssets\Notifiers\NotifierInterface) and specify it in the configuration file config/assets.php. Use one of the existing notifiers as a template., (*19)

You would most likely set the destination_url to your CDN server, and add a notifier which copies the file from the destination folder to this server., (*20)

What if I cannot use file_get_contents() because of firewall/proxy issues?

Write your own loader (implement Fisharebest\LaravelAssets\Loaders\LoaderInterface) and specify it in the configuration file config/assets.php. Use one of the existing loaders as a template., (*21)

How do I delete old files after I update my assets or change my filters?

There is an artisan command for that., (*22)

php artisan assets:purge

The Versions

01/09 2017
14/08 2017
20/02 2017

1.4.2

1.4.2.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

29/02 2016

1.4.1

1.4.1.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

04/02 2016

1.4.0

1.4.0.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

04/02 2016

1.3.0

1.3.0.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

03/02 2016

1.2.1

1.2.1.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

13/11 2015

1.2.0

1.2.0.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

13/11 2015

1.1.0

1.1.0.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify

03/11 2015

1.0.0

1.0.0.0

Asset management for Laravel

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Greg Roach

laravel css assets asset js pipeline minify