Git Product home page Git Product logo

mini-asset's Introduction

Mini Asset

Software License Build Status codecov.io Total Downloads

Mini Asset is a small footprint library that provide tools to assist in building an asset minification/optimization pipeline. It uses a simple INI based configuration file that lets you define output files, that are comprised of a number of input files. Input files can be processed by filters on a per-extension or per file basis. Filters allow you to integrate existing pre-processors and minifiers or build your own.

Features

  • Built-in support for LESScss, Sass and CoffeeScript, as well as several minifiers.
  • Powerful and flexible filter system allowing you to add your own minifiers/pre-processors.
  • Simple ini configuration files.
  • Incremental builds that don't recompile assets when they don't need to be.
  • Command Line Tools for building and clearing assets.

Installing

Add this library to your application with composer, after that you can start integrating MiniAsset into your application and use the provided CLI tools to generate asset targets:

php composer.phar require 'markstory/mini-asset'

Usage

Before MiniAsset can do anything, you'll need to define a configuration file to define your asset and which filters apply to them. For example purposes, we'll assume you have some CSS files that need to be minified.

Defining a configuration file

In your application's configuration directory create a file called assets.ini. In this file we'll put all the configuration that MiniAsset needs. We'll cover how to use multiple configuration files later on. Assuming your, application has a app.css and reset.css put the following in your config file:

[css]
cachePath = WEBROOT/cache_css/
paths[] = WEBROOT/css/*
filters[] = SimpleCssMin

[app.css]
files[] = reset.css
files[] = app.css

The above also assumes that WEBROOT resolves to your application's document root. In the above we defined a few sections. First, the [css] section defines how all CSS files should behave:

  • The cachePath option tells MiniAsset where to store generated assets.
  • The paths[] options tell MiniAsset where to find CSS files. Paths use glob syntax.
  • The filters[] options let you configure which filters are applied to all CSS files. Filters are applied in the order they are defined.

We also defined an [app.css] section. This section defines the files that are used to create WEBROOT/cache_css/app.css when it is generated. We've added two files.

See the sample configuration file for an annotated sample configuration file.

Use the CLI tool to build your asset

With a build target defined, we can now generate our asset. We can use the CLI tool to generate our assets:

vendor/bin/mini_asset build --config /path/to/assets.ini

The above should generate output indicating that the all.css file we defined was compiled. If we were to re-run the above command all.css would be skipped. MiniAsset is smart enough to know when the components of a build target change and only re-build files when it is necessary.

You can also use the mini_asset CLI tool to clear targets:

vendor/bin/mini_asset clear --config /path/to/assets.ini

PSR7 Middleware

Mini-asset provides a PSR7 middleware adapter that implements the pattern found in zendframework/zend-stratagility and SlimPHP. Before you can use the middleware provided by mini-asset be sure to setup a config file with your assets. Next, integrate mini-asset into your middleware stack. For example in a SlimPHP app you would do:

use MiniAsset\AssetConfig;
use MiniAsset\Middleware\AssetMiddleware;

$assetConfig = AssetConfig::buildFromIniFile(__DIR__ . '../config/assets.ini');
$assets = new AssetMiddleware($assetConfig);

$app->add($assets);

Framework Integrations

Issues

Please report any issues you have with the plugin to the issue tracker on github.

License

Mini Asset is offered under an MIT license.

Copyright

2010-2018 Mark Story (http://mark-story.com)

Authors

See the github contributors list.

Changelog

See CHANGELOG for changes only available on master. See github releases for changelogs on previous releases.

mini-asset's People

Contributors

adamroyle avatar admad avatar afolgado avatar ajfranzoia avatar berarma avatar codaxis avatar dereuromark avatar dinhani avatar evilbloodydemon avatar fullybaked avatar groovenectar avatar jadb avatar jaykay-design avatar jeremyharris avatar jiru avatar josegonzalez avatar lorenzo avatar majna avatar markstory avatar mrothauer avatar pedro-stanaka avatar phally avatar renan avatar rynop avatar stickler-ci avatar swader avatar thomseddon avatar wyrihaximus avatar yourivdlans avatar zeroasterisk 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mini-asset's Issues

middleware and targets dependencies

hello

let's consider this config:

[css]
paths[] = ...
paths[] = ...
cachePath = ...

[tmp_local.css]
files[] = ...
files[] = ...
filters[] = CssMinFilter

[styles.css]
files[] = ...
files[] = tmp_local.css

when compiling through CLI, everything is OK
when using middleware and accessing style.css file, which is the main one, error:

Could not locate tmp_local.css for styles.css in any configured path.

middleware should check if file is a target
(or I did something wrong)

Sass partials skipped in dependency check

Hey,
In sass files, if, say, you @import "partial";, sass looks for both a partial.scss, or a _partial.scss. Mini-asset however only checks for a partial.scss and doesn't look for the underscored version, and so automatically skips the dependency check.

Using paths in filters doesn't work

I've tried to define paths for the LessCSS filter in the following lines:

[filter_LessCss]
node = /usr/local/bin/node
node_path = /usr/local/lib/node_modules
paths[] = APP/vendor/twbs/bootstrap/less/*
paths[] = WEBROOT/css/*

I get this message:

Warning (2): strtr() expects parameter 1 to be string, array given [ROOT/vendor/markstory/mini-asset/src/AssetConfig.php, line 306]

I'm guessing that's because the settings array isn't processed recursively when constant substitution takes place, thus an array is passed instead of the elements of that array. This is the line: https://github.com/markstory/mini-asset/blob/master/src/AssetConfig.php#L443

Some filters won't work with wrapper scripts

I guess this could be an enhancement and make things easier for people using tools like nodenv to manage nodejs versions.

I've found this issue while using the Uglifyjs filter but the CoffeeScript filter does the exact same thing.

These filters call nodejs passing a file from /usr/local/bin. We're guessing the file in the bin directory isn't executable by itself (while it should) and that it will be the Javascript file, not some wrapper around it. In my case, I have links to the nodenv shims and thus the filter fails.

I think the best fix would be changing the configuration seetings so that I could just point to an executable without caring about nodejs versions and paths when I don't need it.

I could accomplish this using this config:

'node' => '/usr/local/bin/uglifyjs'
'uglify' => ''

But this could read easier as:

'uglify' => /usr/local/bin/uglifyjs'

And make it the default configuration since it should work in most cases I guess. The node and node_path settings should be seldom needed.

Add CleanCss Filter to 2.x Branch (CakePHP 5)

Hi Mark,
I updated my app to CakePHP 5 and realized, that my commits for the CleanCss filter are not yet merged to the 2.x branch. Could you do that please?

2.x...master

Minor adaptions for the filter are required (argument types and return types), here you find the adapted file which is working:
https://github.com/foodcoopshop/foodcoopshop/blob/issue-917-cakephp-5-update/config/mini-asset/CleanCss.php

I can do the PR if you want as soon as you merge the commits into the branch.

Thank you,
Mario

Variables in LESS imports.

I've just noticed mini-asset won't find dependencies declared with an import that uses a variable in LESS.

I must say I don't really need this to work since the files I'm importing are vendor files that I won't change. Touching my files is enough to rebuild it. Just noticed it and thought I would report about it.

This will be hard to fully resolve. A possible workaround would be to have an option to disable incremental building or a configurable path to check for changes on any file inside it.

theme: is unresolved in path

as of commit 25f9013 (or around cant be 100% sure) asset compress ignores 'themes[] =' ie it doesnt replace theme: in path with currently set theme

ive trace it back to AssetConfig.php doesnt know how to select current theme

PHP 8.2 support

When I change the php-requirements of my project's composer.json to 8.2 and then run composer update i get a downgraded version of mini-asset (which then throws errors).

Downgrading markstory/mini-asset (1.11.0 => 1.9.1)

Would it be possible to change <8.2 to <=8.2?

"php": ">=7.2,<8.2",

PHP 8.3 support

We are currently using the 1.x release (because of asset-compress 4.x) but since we upgraded to php 8.3 the mini asset version got downgraded to 1.4.2 because of this composer constraint in recent 1.x releases: "php": ">=7.4,<8.3".

What is the reason to not allow php8.3 for the latest 1.x releases?

CssMin

I'm having problems using SimpleCssMin in that it doesn't resolve relative paths. For example, compiling FontAwesome (trying to get this installed and compiled via BowerPHP), I get this:

screenshot 2015-08-03 01 37 37

I then tried to use CssMin, as per package suggestion post-install, but I get this:

Fatal error: Call to undefined method CssMin::settings() in /home/vagrant/.composer/vendor/markstory/mini-asset/src/Factory.php on line 202

Is there something I'm doing wrong?

Wrapping issues

After upgrading to cakephp 5 I have the following error:

: file_exists(): Unable to find the wrapper &quot;wss&quot; - did you forget to enable it when you configured PHP? [in /var/www/acme/vendor/markstory/mini-asset/src/AssetConfig.php, line 129
I tried to search on google without luck.
I have added a "@" as workardoun on line 129.
How can I solve this problem?

Thank you

Rudy

Glob paths when resolving CSS dependencies

I think glob paths don't work when resolving CSS dependencies. The function that searches the file uses file_exists directly on the path setting and the file name.

[css]
paths[] = WEBROOT/css/*
[public.css]
files[] = public/base.less

This will check for the file "WEBROOT/css/*/public/base.less".

Missing directory separator when using ROOT constant in ClosureJs.php

Hi Mark,

just tried to use your new mini-asset plugin in a CakePHP 3 project to minify js assets with ClosureJs.

However, compiler.jar could not be found in vendor/closure path due to a missing DS in the _findExecutable call.

line 50
$jar = $this->_findExecutable([ROOT . 'vendor'], $this->_settings['path']);

should become
$jar = $this->_findExecutable([ROOT . DS . 'vendor'], $this->_settings['path']);

as ROOT is defined by cake without trailing slash explicitly.

I think your tests don't come up with this error because you use
define('ROOT', dirname(__DIR__) . DS);
in tests/bootstrap.php which differs from the way CakePHP defines the ROOTconstant itself.

Best regards,
Stefan

SimpleCssMin removes required whitespace

the following CSS rule breaks when using SimpleCssMin:

.hoverable :hover span {foo:bar;}

is compressed to:

.hoverable:hover span {foo:bar;}

Any rule with a pseudo-class not attached to a class or element will break.

CLI constants

how to set constants from console? f.e. WEBROOT in your example

Global require

I suggest adding global installation instructions into the readme:

composer global require markstory:mini-asset --dev

and adding __DIR__ . '../../../autoload.php' as the first option into the bin file under autoload paths, in order to accommodate this (it crashes on global install otherwise).

Alternatively, consider using something like box to automate this and avoid the options array altogether.

Custom (app level) filters

How can you define a custom filter, e.g. in your project and add it to the stack?
I didn't see any config on this customization in the readme.

Problem with UgilfyJs command

Hey, in this line the options are always prepended with a -, but why? It causes errors in my build (using UglifyJs 1.3.8 and node 4.2.6), since it treats - as a file to uglify. Am I doing something wrong, or is this an error? Thanks in advance for the reply.

Imported CSS files with media queries not checked

@import rules with media queries are ignored when checking for changes in CSS files. Examples of such rules:

@import url("fineprint.css") print;
@import url("bluish.css") projection, tv;
@import "common.css" screen, projection;
@import url('landscape.css') screen and (orientation:landscape);

I'm testing a fix using the following modified regular expression in CssUtils:

const IMPORT_PATTERN = '/^\s*@import\s*(?:(?:([\'"])([^\'"]+)\\1)|(?:url\(([\'"])([^\'"]+)\\3\)))(\s.*)?;/m';

I guess this should be enough.

Sass external libraries (compass) dependency check error

Hello,
Dependency checking now works, however, if I'm trying to import an external library (compass, for example, with @import "compass";, File\Local::__construct() throws an error, saying it can't find compass.scss.

I made a CompassFIlter for this purpose, which basically extends ScssFilter and appends '--compass' at the end of the sass executable, so it's not technically a problem with the core functionality, but perhaps a feature in the filters to exclude a dependency would be useful, or maybe just throw a warning instead of an error if a dependency is not found.

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.