Git Product home page Git Product logo

assetic's Introduction

Assetic

Average time to resolve an issue Percentage of issues still open

Assetic is an asset management framework for PHP maintained by the Winter CMS team.

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;

$js = new AssetCollection(array(
    new GlobAsset('/path/to/js/*'),
    new FileAsset('/path/to/another.js'),
));

// the code is merged when the asset is dumped
echo $js->dump();

Assets

An Assetic asset is something with filterable content that can be loaded and dumped. An asset also includes metadata, some of which can be manipulated and some of which is immutable.

Property Accessor Mutator
content getContent setContent
mtime getLastModified n/a
source root getSourceRoot n/a
source path getSourcePath n/a
target path getTargetPath setTargetPath

The "target path" property denotes where an asset (or an collection of assets) should be dumped.

Filters

Filters can be applied to manipulate assets.

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;
use Assetic\Filter\LessFilter;
use Assetic\Filter\UglifyCssFilter;

$css = new AssetCollection(array(
    new FileAsset('/path/to/src/styles.less', array(new LessFilter())),
    new GlobAsset('/path/to/css/*'),
), array(
    new UglifyCssFilter('/path/to/uglifycss'),
));

// this will echo CSS compiled by LESS and compressed by uglifycss
echo $css->dump();

The filters applied to the collection will cascade to each asset leaf if you iterate over it.

<?php

foreach ($css as $leaf) {
    // each leaf is compressed by uglifycss
    echo $leaf->dump();
}

The core provides the following filters in the Assetic\Filter namespace:

  • CoffeeScriptFilter: compiles CoffeeScript into Javascript
  • CssImportFilter: inlines imported stylesheets
  • CSSMinFilter: minifies CSS
  • CssRewriteFilter: fixes relative URLs in CSS assets when moving to a new URL
  • GoogleClosure\CompilerApiFilter: compiles Javascript using the Google Closure Compiler API
  • HandlebarsFilter: compiles Handlebars templates into Javascript
  • JavaScriptMinifierFilter: minifies Javascript
  • JpegoptimFilter: optimize your JPEGs
  • JpegtranFilter: optimize your JPEGs
  • LessFilter: parses LESS into CSS (using less.js with node.js)
  • LessphpFilter: parses LESS into CSS (using lessphp)
  • OptiPngFilter: optimize your PNGs
  • PackerFilter: compresses Javascript using Dean Edwards's Packer
  • PhpCssEmbedFilter: embeds image data in your stylesheet
  • ReactJsxFilter: compiles React JSX into JavaScript
  • ScssphpFilter: parses SCSS into CSS
  • SeparatorFilter: inserts a separator between assets to prevent merge failures
  • StylesheetMinifyFilter: compresses stylesheet CSS files
  • StylusFilter: parses STYL into CSS
  • TypeScriptFilter: parses TypeScript into Javascript
  • UglifyCssFilter: minifies CSS
  • UglifyJs2Filter: minifies Javascript
  • UglifyJs3Filter: minifies Javascript

Asset Manager

An asset manager is provided for organizing assets.

<?php

use Assetic\AssetManager;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;

$am = new AssetManager();
$am->set('jquery', new FileAsset('/path/to/jquery.js'));
$am->set('base_css', new GlobAsset('/path/to/css/*'));

The asset manager can also be used to reference assets to avoid duplication.

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\AssetReference;
use Assetic\Asset\FileAsset;

$am->set('my_plugin', new AssetCollection(array(
    new AssetReference($am, 'jquery'),
    new FileAsset('/path/to/jquery.plugin.js'),
)));

Filter Manager

A filter manager is also provided for organizing filters.

<?php

use Assetic\FilterManager;
use Assetic\Filter\ScssFilter;
use Assetic\Filter\CssMinFilter;

$fm = new FilterManager();
$fm->set('sass', new ScssFilter('/path/to/parser/scss'));
$fm->set('cssmin', new CssMinFilter());

Asset Factory

If you'd rather not create all these objects by hand, you can use the asset factory, which will do most of the work for you.

<?php

use Assetic\Factory\AssetFactory;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setDebug(true);

$css = $factory->createAsset(array(
    '@reset',         // load the asset manager's "reset" asset
    'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
), array(
    'scss',           // filter through the filter manager's "scss" filter
    '?cssmin',        // don't use this filter in debug mode
));

echo $css->dump();

The AssetFactory is constructed with a root directory which is used as the base directory for relative asset paths.

Prefixing a filter name with a question mark, as cssmin is here, will cause that filter to be omitted when the factory is in debug mode.

You can also register Workers on the factory and all assets created by it will be passed to the worker's process() method before being returned. See Cache Busting below for an example.

Dumping Assets to static files

You can dump all the assets an AssetManager holds to files in a directory. This will probably be below your webserver's document root so the files can be served statically.

<?php

use Assetic\AssetWriter;

$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

This will make use of the assets' target path.

Cache Busting

If you serve your assets from static files as just described, you can use the CacheBustingWorker to rewrite the target paths for assets. It will insert an identifier before the filename extension that is unique for a particular version of the asset.

This identifier is based on the modification time of the asset and will also take depended-on assets into consideration if the applied filters support it.

<?php

use Assetic\Factory\AssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setDebug(true);
$factory->addWorker(new CacheBustingWorker());

$css = $factory->createAsset(array(
    '@reset',         // load the asset manager's "reset" asset
    'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
), array(
    'scss',           // filter through the filter manager's "scss" filter
    '?yui_css',       // don't use this filter in debug mode
));

echo $css->dump();

Internal caching

A simple caching mechanism is provided to avoid unnecessary work.

<?php

use Assetic\Asset\AssetCache;
use Assetic\Asset\FileAsset;
use Assetic\Cache\FilesystemCache;
use Assetic\Filter\JavaScriptMinifierFilter;

$jsMinifier = new JavaScriptMinifierFilter();
$js = new AssetCache(
    new FileAsset('/path/to/some.js', array($jsMinifier)),
    new FilesystemCache('/path/to/cache')
);

// the JavaScriptMinifierFilter compressor will only run on the first call
$js->dump();
$js->dump();
$js->dump();

Twig

To use the Assetic Twig extension you must register it to your Twig environment:

<?php

$twig->addExtension(new AsseticExtension($factory));

Once in place, the extension exposes a stylesheets and a javascripts tag with a syntax similar to what the asset factory uses:

{% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

This example will render one link element on the page that includes a URL where the filtered asset can be found.

When the extension is in debug mode, this same tag will render multiple link elements, one for each asset referenced by the css/src/*.sass glob. The specified filters will still be applied, unless they are marked as optional using the ? prefix.

This behavior can also be triggered by setting a debug attribute on the tag:

{% stylesheets 'css/*' debug=true %} ... {% stylesheets %}

These assets need to be written to the web directory so these URLs don't return 404 errors.

<?php

use Assetic\AssetWriter;
use Assetic\Extension\Twig\TwigFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\LazyAssetManager;

$am = new LazyAssetManager($factory);

// enable loading assets from twig templates
$am->setLoader('twig', new TwigFormulaLoader($twig));

// loop through all your templates
foreach ($templates as $template) {
    $resource = new TwigResource($twigLoader, $template);
    $am->addResource($resource, 'twig');
}

$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

Assetic is based on the Python webassets library (available on GitHub).

assetic's People

Contributors

abretaud avatar adlenton avatar alexash avatar amyboyd avatar bennothommo avatar benoitleveque avatar brikou avatar brunoais avatar cedriclombardot avatar dbu avatar fran6co avatar hason avatar heristop avatar jaxwilko avatar jeroenvisser101 avatar kastaneda avatar keyvanakbary avatar krichprollsch avatar kriswallsmith avatar krmarien avatar luketowers avatar maximilian-walter avatar mente avatar moox avatar mpdude avatar pierredup avatar schmittjoh avatar speckins avatar stof avatar yguedidi 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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

assetic's Issues

php 8.0

Hi,

is it possible for you to provide this package for php 8.0 too? I'm not able to install it with the current composer settings.

Following error is shown:

  • assetic/framework v2.0.0 requires php ^7.2 -> your php version (8.0.1) does not satisfy that requirement.

But in the composer.json the requirements should be met ("php": "^7.2|^8.0")?!

Thank you, Best Regards

Support for enabling compass in ScssphpFilter should be removed

The scss_compass class belongs to leafo/scssphp-compass (or one of its published forks).

This package was never released, is not maintained anymore (last commit in 2012) and is for leafo/scssphp, not scssphp/scssphp. The last point means that the code is broken in Assetic, as ScssphpFilter uses the class from scssphp/scssphp.

Custom functions in ScssphpFilter are not exposing the full upstream API

ScssphpFilter::registerFunction only exposes arguments for the name and the callable.
But the ScssPhp API supports a third argument to configure the Sass signature (or signatures) of the function, and this parameter is not exposed in any way in Assetic.
Currently, this parameter is optional, with a different behavior when you don't have it or you have it. and the behavior without knowing the signature is not spec-compliant (as the compiler is expected to validate the signature). As such, the plan is to make that argument mandatory in 2.0 of scssphp. It would be great if Assetic could allow using this argument.

Note that we currently plan a refactoring in scssphp which will impact the implementation of custom functions, for which we plan to add a new (optional) argument in registerFunction to opt-in for the new behavior (which also won't allow omitting the signature). Once that argument appears, it might make sense to support it too (forcing the value to opt-in or opt-out in ScssphpFilter itself would defeat the purpose of the per-function opt-in)

PHP Fatal error: Interface 'Assetic\Factory\Worker\WorkerInterface' not found.

Hey,

Good day.

Seems kriswallsmith assetic has been replaced by assetic-php. I have a custom LastModifiedStrategy which worked fine but now throws the error:

PHP Fatal error:  Interface 'Assetic\Factory\Worker\WorkerInterface' not found...

If I replace the Assetic\Factory\Worker\WorkerInterface with Assetic\Contracts\Factory\Worker\WorkerInterface instead, the new error is:

Argument 1 passed to AsseticBundle\Service::setCacheBusterStrategy() must be an instance of Assetic\Factory\Worker\WorkerInterface

Been trying to revert my composer file to a previous version, but can't seem to get kriswallsmith version installed again.

Any assistance would be much appreciated, thanks.

Regards.
Jarrett

Example for symfony 5 or 6 integration?

Hi!

First, thx for your great work!

At the moment I'm trying to get the package connected to my symfony 5 project. But i'm too stupid to get the twig integration running.
Do you know a working example for this where I can have a look at?
Best,
Tim

Change property scopes to protected?

It'd be nice to be able to extend/enhance parts of the library instead of copy/paste to do so.

Would you be open to changing the privates to protected to allow easier extension?

Removal of the Assetic\Extension\Twig\TwigFormulaLoader class

Hi,

while updating a project to PHP 7.4, I had to upgrade the unmaintained kriswallsmith/assetic to assetic-php/assetic. In doing so, a section of our code stopped working because it uses the Assetic\Extension\Twig\TwigFormulaLoader class, which was removed by commit 4e42ff8, authored by @jaxwilko.

Do you have any insight as to why this was done so, instead of updating it to work with Twig 2.11 as the commit message implies? I haven't found any drop-in replacement for this class in the project's code, so I'm currently in the process of reimplementing it on our side as a quick fix, but if you know of a more solid solution I'd gladly take it.

Also of note is that the README.md file still mentions that class in the Twig section, so maybe this example should also be updated.

Thanks.

Universal selector can't follow a comment

When css file contains an universel selector following a comment, with another comment far in file or comment in var, it is then considered as a opening comment .

With comment in var
In:

/*! Keep me */*{box-sizing: border-box;}.form{--ring-inset: var(--empty, /*!*/ /*!*/);border: 0;}

Expected:

/*! Keep me */*{box-sizing:border-box}.form{--ring-inset:var(--empty,/*!*/ /*!*/);border:0}

Output:

/*! Keep me * /*!*/);border:0}

With another comment
In:

/*! Keep me */*{box-sizing: border-box;}/* Remove me */.noborder{border: 0;}

Expected:

/*! Keep me */*{box-sizing:border-box}.noborder{border:0}

Output:

/*! Keep me *.noborder{border:0}

I encountered this bug when using Laravel Mix with TailwindCss.
In Mix cssNano.discardComments is true by default, so the css code is compacted, and the universal selector is then appended to the Tailwind retained comment:

@charset "UTF-8";
/*! tailwindcss v3.0.23 | MIT License | https://tailwindcss.com*/*,:after,:before{border:0 solid #e7e5e4;box-sizing:border-box} ...

Hidden API for registerFunction in LessphpFilter

The LessphpFilter does not provide an interface to the registerFunction method in lessc.inc.php, however the ScssphpFilter does provide an interface to this through \Assetic\Filter\ScssphpFilter::registerFunction. Is there a reason there isn't parity between these 2 filters? I'm happy to do a PR to add this in, if it's likely to be approved.

thanks!

JSMin not found when using JSMinFilter

I have mrclay/minify component installed as well.

In class Assetic\Filter\JSMinFilter, JsMin is called like this:

public function filterDump(AssetInterface $asset)
    {
        $asset->setContent(\JSMin::minify($asset->getContent()));
    }

except that there is no root class JSMin declared in mrclay module, only class JSMin\JSMin exists.

so changing Assetic\Filter\JSMinFilter like this solve the issue

<?php namespace Assetic\Filter;

use Assetic\Contracts\Asset\AssetInterface;
use JSMin\JSMin;

/**
 * Filters assets through JsMin.
 *
 * All credit for the filter itself is mentioned in the file itself.
 *
 * @link https://raw.github.com/mrclay/minify/master/min/lib/JSMin.php
 * @author Brunoais <[email protected]>
 */
class JSMinFilter extends BaseFilter
{
    public function filterDump(AssetInterface $asset)
    {
        $asset->setContent(JSMin::minify($asset->getContent()));
    }
}

am i something wrong that I have to change this file in order to make it work ? Or did the class namespace and declaration from mrclay changed ?

Thank you in advance

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.