gremo / assetic-extra
A collection of extra assetic resources to use with Symfony AsseticBundle.
Installs: 45 727
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 2
Open Issues: 0
pkg:composer/gremo/assetic-extra
Requires
- php: >=5.3.2
Requires (Dev)
- php: >=5.3.2
- kriswallsmith/assetic: ^1.1
README
A collection of extra assetic resources to use with Symfony AsseticBundle.
Table of Contents
Installation
Install this library using Composer:
composer require gremo/assetic-extra
Filters
Note: with Symfony 3.3 you can replace
%kernel.root_dir%/..with%kernel.project_dir%for filters configuration.
The following extra filters can be configured and used in your templates.
Babel
JavaScript transpiler for node.js (https://babeljs.io).
Configuration
assetic: # ... babeljs: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/babeljs.xml' # options here
Options (reference)
bin: path to yourbabelbinary (default:/usr/bin/babel)retain_linespresets: astringorarrayof presets to use (preset name if installed globally, path otherwise)plugins: astringorarrayof plugins to use (plugin name if installed globally, path otherwise)compactminifiedno_babel_rcauxiliary_comment_beforeauxiliary_comment_afterparser_optsgenerator_opts
Note: Babel will look for
.babelrcfile in current asset directory and will travel up the directory tree (see Lookup behavior), unless you specify theno_babel_rcoption. This means you can put your.babelrcfile in your project root without cluttering yourconfig.yml.
Usage
{% javascripts '../app/Resources/js/*.js' filter='babeljs' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Browserify
Lets you require('modules') in the browser (http://browserify.org).
Credits goes to the original author (https://github.com/kriswallsmith/assetic/pull/669), I changed it a bit and added trasforms support.
Configuration
assetic: # ... browserify: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/browserify.xml' # options here
Options
bin: path to yourbrowserifybinary (default:/usr/bin/browserify)node: path to yournodebinary (default:%assetic.node.bin%, set tonullto usebrowserifybinary instead)node_paths: paths to add to Node.js environment when usingnodeoption (default:%assetic.node.paths%)transformsastringorarrayof Browserify transform to apply
Usage
Note: there is no need to combine assets (
modules/module1.jsin the example) as long as you require yourmodule. Browserify filter will take care of combining them in the output file.
{% javascripts '../app/Resources/js/main.js' filter='browserify' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Note: the
assetic:watchcommand will regenerate the assets only if you change the "main" script. Modules changes will not be monitored as they are not included in thejavascriptstag.
Example of main.js:
// main.js require('./modules/module1.js'); console.log('main.js');
Example of modules/module1.js:
// modules/module1.js console.log('modules/module1.js');
CSSO
CSSO (CSS Optimizer) is a CSS minifier (https://github.com/css/csso).
Configuration
assetic: # ... csso: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/csso.xml' # options here
Options (reference)
bin: path to yourcssobinary (default:/usr/bin/csso)commentsforce_media_mergerestructure_offusage
Usage
{% stylesheets '../app/Resources/css/*' filter='csso' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Tip: fast and safe options
csso: # ... comments: none restructure_off: true
PostCSS
A tool for transforming CSS with JavaScript (http://postcss.org).
Configuration
assetic: # ... postcss: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/postcss.xml' # options here
Options (reference)
bin: path to yourpostcssbinary (default:/usr/bin/postcss)no_map: disable the default inline sourcemapsuse: list of postcss plugins to useparser: custom postcss parserstringifier: custom postcss stringifiersyntax: custom postcss syntaxconfig: set a custom path to look for a config file
Usage
{% stylesheets '../app/Resources/css/*' filter='postcss' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Node-sass
Parses SASS/SCSS into CSS using the LibSass bindings for node.js (https://github.com/sass/node-sass).
Configuration
assetic: # ... nodesass: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/nodesass.xml' # options here
Options (reference)
bin: path to yournode-sassbinary (default:/usr/bin/node-sass)import_pathsindent_typeindent_widthlinefeedoutput_styleprecisionsource_commentssource_map_locationsource_map_public_dir
Usage
{% stylesheets '../app/Resources/scss/style.scss' filter='nodesass' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Tip: Boostrap 4
Use this options for Bootstrap 4 (see package.json):
nodesass: # ... precision: 6
Tip: source maps
In order to generate the source maps, you'll need to specify a public accessible directory where the .map files can be placed (source_map_location) together with the base path (source_map_public_dir) which will be used when generating the urls to the mapping files:
nodesass: # ... source_map_location: '%kernel.root_dir%/../web/assets/maps' source_map_public_dir: '/assets/maps'
Recipes
ES6 modules with Browserify
Write ES6 JavaScript modules using import/export style and make it work in the browser.
Problem: Browserify filter transform your source file and not your transpiled one, so it would fail at the first import or export keyword.
Solution: only use the browserify filter with babelify transform filter configuration:
Note if Browserify cannot find the babelify module, try installing it locally in your project folder and use the absolute path.
browserify: resource: '%kernel.root_dir%/../vendor/gremo/assetic-extra/Resources/filter/browserify.xml' transforms: - '[ babelify --presets env ]'