jasonlewis / basset

A better asset management package for Laravel.
http://jasonlewis.me/code/basset
241 stars 76 forks source link

Assets were not combined to one single (collection) file #165

Open marvinschroeder opened 11 years ago

marvinschroeder commented 11 years ago

Since the last update (or change in my configuration file), the files in the collections were not build to one single file. Instead they were several files with long paths:

< link rel="stylesheet" type="text/css" href="/build/admin-base/assets/css/vendors/bootstrap/bootstrap-f465d87e388699367f16384d27521480.css" /> < link rel="stylesheet" type="text/css" href="/build/admin-base/assets/css/vendors/bootstrap/responsive-f930ab6c1024fd1174fa4fa629211945.css" /> < link rel="stylesheet" type="text/css" href="/build/admin-base/assets/css/admin/style-67366d51d39984330e0c3d90ce3c833f.css" />

I work with Laravel 4 and below is my config file. Important notice: i removed "$filter->whenAssetIs('.*.less')" because with this rule my less-files were not compiled as less.


<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Collections
    |--------------------------------------------------------------------------
    |
    | Basset is built around collections. A collection contains assets for
    | your application. Collections can contain both stylesheets and
    | javascripts.
    |
    | A default "application" collection is ready for immediate use. It makes
    | a couple of assumptions about your directory structure.
    |
    | /public
    |    /assets
    |        /stylesheets
    |            /less
    |            /sass
    |        /javascripts
    |            /coffeescripts
    |
    | You can overwrite this collection or remove it by publishing the config.
    |
    */

    'collections' => array(

        'base' => function($collection){

            $collection->directory('/assets', function($collection){
                $dir = $collection->directory('/css', function($collection){                
                    $collection->stylesheet("vendors/bootstrap/bootstrap.less");
                    $collection->stylesheet("vendors/bootstrap/responsive.less");
                    $collection->stylesheet("style.css");
                });
                $dir->apply('Less');
                $dir->apply('CssMin');     
                $dir->apply('UriRewriteFilter');       

                $dir = $collection->directory('/js/vendors', function($collection){                
                    $collection->javascript("jquery-1.9.1.min.js");
                    $collection->javascript("bootstrap.min.js");
                });
                $dir->apply('JsMin');
            });
        },
        'admin-base' => function($collection){

            $collection->directory('/assets', function($collection){
                $dir = $collection->directory('/css', function($collection){                
                    $collection->stylesheet("vendors/bootstrap/bootstrap.less");
                    $collection->stylesheet("vendors/bootstrap/responsive.less");
                    $collection->stylesheet("admin/style.css");
                });
                $dir->apply('Less');
                $dir->apply('CssMin');     
                $dir->apply('UriRewriteFilter');       

                $dir = $collection->directory('/js/vendors', function($collection){                
                    $collection->javascript("jquery-1.9.1.min.js");
                    $collection->javascript("bootstrap.min.js");
                });
                $dir->apply('JsMin');
            });
        },
        'ie9' => function($collection){ 
            $collection->javascript("/assets/js/vendors/html5shiv.js")->apply('JsMin');
        },
    ),

    /*
    |--------------------------------------------------------------------------
    | Production Environment
    |--------------------------------------------------------------------------
    |
    | Basset needs to know what your production environment is so that it can
    | respond with the correct assets. When in production Basset will attempt
    | to return any built collections. If a collection has not been built
    | Basset will dynamically route to each asset in the collection and apply
    | the filters.
    |
    | The last method can be very taxing so it's highly recommended that
    | collections are built when deploying to a production environment.
    |
    | You can supply an array of production environment names if you need to.
    |
    */

    'production' => array('production', 'prod'),

    /*
    |--------------------------------------------------------------------------
    | Build Path
    |--------------------------------------------------------------------------
    |
    | When assets are built with Artisan they will be stored within a directory
    | relative to the public directory.
    |
    | If the directory does not exist Basset will attempt to create it.
    |
    */

    'build_path' => 'build',

    /*
    |--------------------------------------------------------------------------
    | Debug
    |--------------------------------------------------------------------------
    |
    | Enable debugging to have potential errors or problems encountered
    | during operation logged to a rotating file setup.
    |
    */

    'debug' => false,

    /*
    |--------------------------------------------------------------------------
    | Node Paths
    |--------------------------------------------------------------------------
    |
    | Many filters use Node to build assets. We recommend you install your
    | Node modules locally at the root of your application, however you can
    | specify additional paths to your modules.
    |
    */

    'node_paths' => array(

        base_path().'/node_modules'

    ),

    /*
    |--------------------------------------------------------------------------
    | Gzip Built Collections
    |--------------------------------------------------------------------------
    |
    | To get the most speed and compression out of Basset you can enable Gzip
    | for every collection that is built via the command line. This is applied
    | to both collection builds and development builds.
    |
    | You can use the --gzip switch for on-the-fly Gzipping of collections.
    |
    */

    'gzip' => false,

    /*
    |--------------------------------------------------------------------------
    | Asset and Filter Aliases
    |--------------------------------------------------------------------------
    |
    | You can define aliases for commonly used assets or filters.
    | An example of an asset alias:
    |
    |   'layout' => 'stylesheets/layout/master.css'
    |
    | Filter aliases are slightly different. You can define a simple alias
    | similar to an asset alias.
    |
    |   'YuiCss' => 'Yui\CssCompressorFilter'
    |
    | However if you want to pass in options to an aliased filter then define
    | the alias as a nested array. The key should be the filter and the value
    | should be a callback closure where you can set parameters for a filters
    | constructor, etc.
    |
    |   'YuiCss' => array('Yui\CssCompressorFilter', function($filter)
    |   {
    |       $filter->setArguments('path/to/jar');
    |   })
    |
    |
    */

    'aliases' => array(

        'assets' => array(),

        'filters' => array(

            /*
            |--------------------------------------------------------------------------
            | Less Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter is applied only when asset has a ".less" extension and it will
            | attempt to find missing constructor arguments.
            |
            */

            'Less' => array('LessphpFilter', function($filter)
            {
                $filter->whenAssetIsStylesheet()->findMissingConstructorArgs();
            }),

            /*
            |--------------------------------------------------------------------------
            | Sass Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter is applied only when asset has a ".sass" or ".scss" extension and
            | it will attempt to find missing constructor arguments.
            |
            */

            'Sass' => array('Sass\ScssFilter', function($filter)
            {
                $filter->whenAssetIs('.*\.(sass|scss)')->findMissingConstructorArgs();
            }),

            /*
            |--------------------------------------------------------------------------
            | CoffeeScript Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter is applied only when asset has a ".coffee" extension and it will
            | attempt to find missing constructor arguments.
            |
            */

            'CoffeeScript' => array('CoffeeScriptFilter', function($filter)
            {
                $filter->whenAssetIs('.*\.coffee')->findMissingConstructorArgs();
            }),

            /*
            |--------------------------------------------------------------------------
            | CssMin Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter is applied only when within the production environment and when
            | the "CssMin" class exists.
            |
            */

            'CssMin' => array('CssMinFilter', function($filter)
            {
                $filter->whenAssetIsStylesheet()->whenProductionBuild()->whenClassExists('CssMin');
            }),

            /*
            |--------------------------------------------------------------------------
            | JsMin Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter is applied only when within the production environment and when
            | the "JsMin" class exists.
            |
            */

            'JsMin' => array('JSMinFilter', function($filter)
            {
                $filter->whenAssetIsJavascript()->whenProductionBuild()->whenClassExists('JSMin');
            }),

            /*
            |--------------------------------------------------------------------------
            | UriRewrite Filter Alias
            |--------------------------------------------------------------------------
            |
            | Filter gets a default argument of the path to the public directory.
            |
            */

            'UriRewriteFilter' => array('UriRewriteFilter', function($filter)
            {
                $filter->setArguments(public_path())->whenAssetIsStylesheet();
            })

        )

    )

);
jasonlewis commented 11 years ago

When building via artisan you need to use the --production flag.

php artisan basset:build collection --production
marvinschroeder commented 11 years ago

Ok, this works. But why didn't it work standalone? I defined my environment as production and everything was set up right.

jasonlewis commented 11 years ago

Do you mean without you needing to run that artisan command? Unfortunately that's not how it works.

electblake commented 11 years ago

For anyone else Googling and coming across this issue.

My environment wasn't being detected properly on my production, so basset didn't know to serve the bundled collection-hash.css (and was including single asset files instead).

Make sure in bootstrap/start.php your $env = $app->detectEnvironment is returning production when it should so basset can serve your bundled goodness :)