Jump to content
David Karich

Module: AIOM+ (All In One Minify) for CSS, LESS, JS and HTML

Recommended Posts

A workaround for Issue #30 - CSS urls when PW installed in subdirectory

// ------------------------------------------------------------------------
// Load source of file and rewrite absolute URLs.
// ------------------------------------------------------------------------
$_css_src   = file_get_contents($stylesheet['absolute_path']).PHP_EOL;
$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path']),wire('config')->paths->root) : $_css_src;

//SB: My fix for issue #30 about paths in css image urls. Compatible with Soma's fix for #20 in which you specify the path in css file starting with /site.  
//Issue 30: When pw is installed in a subdirectory aiom omits the subdirectory from paths it makes for css image urls.
//Allows dev site's pw to be in doc root and deployed site's pw to be in subfolder (or the other way around) and have same css work on both.  
//1. Specify image urls in css without any path (e.g. background:url(T-on75.jpg))
//2. Put the images in templates/styles/    
$rel = str_replace($_SERVER['DOCUMENT_ROOT'], '', wire('config')->paths->templates);	//relative path to templates from doc root. Maybe /subfolder/site/templates 
$_css_src = str_replace('url(/site/templates/styles/', 'url(' . $rel . 'styles/', $_css_src);	//processed url: /subfolder/site/templates/styles/T-off75.jpg

I'm using this on AIOM 3.1.4b with the following...

https://github.com/conclurer/ProcessWire-AIOM-All-In-One-Minify/issues/17#issuecomment-47402040 (My change to _getCacheName)

https://github.com/conclurer/ProcessWire-AIOM-All-In-One-Minify/issues/20#issue-40582619 (Soma's fix mentioned above)

I use this in my template files so urls in $config->styles pointing to hosted libraries are not messed with:

foreach ($config->styles->unique() as $path){
     if(preg_match('#^'.$config->urls->templates.'styles(.*)#',$path,$m)) $path = AIOM::CSS('styles'.$m[1]);
     echo "     <link rel='stylesheet' type='text/css' href='$path' />\n";
}

Enjoy!

Share this post


Link to post
Share on other sites

Hi,

I have ProcessWire set up in a directory in my localhost, and when I check the Minified CSS the URLs for backgroun images are incorrect (it is ignoring the path to the proceswire root). Is there an option somewhere I can use to either stop AIOM modifying the paths or to prepend the paths with the correct path to ProcessWire?

Thanks,

MadHatter

Share this post


Link to post
Share on other sites

A workaround for Issue #30 - CSS urls when PW installed in subdirectory

// ------------------------------------------------------------------------
// Load source of file and rewrite absolute URLs.
// ------------------------------------------------------------------------
$_css_src   = file_get_contents($stylesheet['absolute_path']).PHP_EOL;
$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path']),wire('config')->paths->root) : $_css_src;

//SB: My fix for issue #30 about paths in css image urls. Compatible with Soma's fix for #20 in which you specify the path in css file starting with /site.  
//Issue 30: When pw is installed in a subdirectory aiom omits the subdirectory from paths it makes for css image urls.
//Allows dev site's pw to be in doc root and deployed site's pw to be in subfolder (or the other way around) and have same css work on both.  
//1. Specify image urls in css without any path (e.g. background:url(T-on75.jpg))
//2. Put the images in templates/styles/    
$rel = str_replace($_SERVER['DOCUMENT_ROOT'], '', wire('config')->paths->templates);	//relative path to templates from doc root. Maybe /subfolder/site/templates 
$_css_src = str_replace('url(/site/templates/styles/', 'url(' . $rel . 'styles/', $_css_src);	//processed url: /subfolder/site/templates/styles/T-off75.jpg

I'm using this on AIOM 3.1.4b with the following...

https://github.com/conclurer/ProcessWire-AIOM-All-In-One-Minify/issues/17#issuecomment-47402040 (My change to _getCacheName)

https://github.com/conclurer/ProcessWire-AIOM-All-In-One-Minify/issues/20#issue-40582619 (Soma's fix mentioned above)

I use this in my template files so urls in $config->styles pointing to hosted libraries are not messed with:

foreach ($config->styles->unique() as $path){
     if(preg_match('#^'.$config->urls->templates.'styles(.*)#',$path,$m)) $path = AIOM::CSS('styles'.$m[1]);
     echo "     <link rel='stylesheet' type='text/css' href='$path' />\n";
}

Enjoy!

More elegant way to fix this is simply remove wire document root assumption and expect real DOCUMENT_ROOT to be replaced with Minify_CSS_UriRewriter::rewrite() method.

So simply:

Line 590:

$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path']),wire('config')->paths->root) : $_css_src;

To:

$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path'])) : $_css_src;

Fixes this issue, and all URL rewrites are done from DOCUMENT_ROOT instead of wire root

  • Like 1

Share this post


Link to post
Share on other sites

I've encountered an interesting problem when using AIOM recently - but also have a potential fix, so it's not all bad. Here's the information!

I am using separate IE and non-IE stylesheets, with each loaded via conditional comments in the markup.

There are two arrays for the files:

$config->css->all = array(
'css/pure-min.css',
'css/grids-responsive-min.css',
'css/fa/css/font-awesome.min.css',
'css/cds-all.css',
);
 
$config->css->oldie = array(
'css/pure-min.css',
'css/grids-responsive-old-ie-min.css',
'css/fa/css/font-awesome.min.css',
'css/cds-oldie.css',
);

These get minified in the template:

<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo AIOM::CSS($config->css->oldie); ?>">
<![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<link rel="stylesheet" type="text/css" href="<?php echo AIOM::CSS($config->css->all); ?>">
<!--<![endif]-->
That is all great. The problem arises when the source files all have the same modification time - which it does on the live environment of this website, and when using an automated deployment service.

Because each list of files has the same number of files, and the files have the same modification time, the generated cache name is always the same, and one overwrites the other.

My fix was to include the file's paths when generating the cache name in _getCacheName:

foreach ($files as $file) {
    $_timestamp .= $file['absolute_path'] . $file['last_modified'];
}
I'm more than happy to create a pull request on GitHub for this, but thought it'd be worth mentioning/discussing first :)
  • Like 3

Share this post


Link to post
Share on other sites

This unfortunately doesn't work for me - leaves me with empty css.

My PW is installed in subdirectory. Stikki fix was there already. Does any of you know what might be happening?

(I'm no a good coder)

 

Share this post


Link to post
Share on other sites

The seems to be a bug with the CSS compression.

#nav :hover{}

becomes when compressed:

 #nav:hover{}

The blank should not be removed as it is a separator between a parent and a child element and removing it changes the cascade!

The the first rule identifies a child element of a #nav element which is hovered over like the anchor link in this HTML snippet:

<div id="nav"><a href="/">I am a hovering child element</a><div>  

and the latter identifies just the #nav element itself which is hovered over like:

<div id="nav"> I am a hovering parent element. My children don't matter. <div>  

As a consequence this bug breaks CSS based menus.

  • Like 2

Share this post


Link to post
Share on other sites

The CSS compression bug I described in my earlier post has been fixed in AIOM version 3.2.

  • Like 1

Share this post


Link to post
Share on other sites

The CSS compression bug I described in my earlier post has been fixed in AIOM version 3.2.

As you can see, we've updated AIOM to version 3.2. In this version we shift from the current CSS compressor to YUI, which should solve multiple CSS-related bugs.

This unfortunately doesn't work for me - leaves me with empty css.

My PW is installed in subdirectory. Stikki fix was there already. Does any of you know what might be happening?

(I'm no a good coder)

Can you please verify if the bug is still present in AIOM 3.2? An empty CSS file sounds for me like a problem with your CSS code...

Share this post


Link to post
Share on other sites

Can you please verify if the bug is still present in AIOM 3.2? An empty CSS file sounds for me like a problem with your CSS code...

Unfortunately it still doesn't work. CSS files come directly from webflow.com export. Maybe I'm messing something up, but there is no too many possibilities to make a mess with. I copy this the line below and change file names. My css files are also in css folder. Don't know what's wrong.
<link rel="stylesheet" href="<?php echo AllInOneMinify::CSS(array('css/file-1.css', 'css/file-2.less', 'css/file-3.css', 'css/file-4.less')); ?>">
Should filename change after emptying the cache? Mine stays the same: css_d41d8cd98f00b204e9800998ecf8427e.css. It's probably stupid question, but I want to be sure - does it assume that the css folder is in templates folder?

Share this post


Link to post
Share on other sites

Hello artamteex,

Yes, AIOM assumes that the CSS files are inside the templates folder. Take a look in the manual: "By default, only files can be included, which are in ProcessWire template folder. If you wish to add files outside that folder, you have to activate the backend "Allow Directory Traversal" option. Then you can jump back in the path."

Share this post


Link to post
Share on other sites

Please make it a possible for absolute path from root. That traversal back with ../../ is not very convenient at all. I have a fork I'm using ever since where I changed it to allow for files outside templates folder using root path.

It's an assumption that shouldnt be made in th first place. The hx

  • Like 2

Share this post


Link to post
Share on other sites

Please make it a possible for absolute path from root. That traversal back with ../../ is not very convenient at all. I have a fork I'm using ever since where I changed it to allow for files outside templates folder using root path.

Hi Soma,

I totally agree with you. Currently, It is possible to use absolute paths using the $config->scripts and $config->styles arrays (see: https://github.com/conclurer/ProcessWire-AIOM-All-In-One-Minify/issues/20).

We are planning to change the behavior of AIOM in AIOM 4. 

Thanks in advance!

Marvin

  • Like 2

Share this post


Link to post
Share on other sites

I don't recommend using the config->scripts that core uses. Cause if you use form API or others core modules on front end you will end up having scripts in there you don't want.

Share this post


Link to post
Share on other sites

I don't recommend using the config->scripts that core uses. Cause if you use form API or others core modules on front end you will end up having scripts in there you don't want.

I thought that $config->styles and $config->scripts are the official containers for gathering and collecting scripts and CSS for frontend output.

On https://processwire.com/api/variables/config/ it says " Feel free to use it for the same purpose in your own sites.".

Maybe I mistunderstood something here and if there are issues with this approach or it is not recomended this probably should be spelled out in the documentation.

Share this post


Link to post
Share on other sites

I thought that $config->styles and $config->scripts are the official containers for gathering and collecting scripts and CSS for frontend output.

On https://processwire.com/api/variables/config/ it says " Feel free to use it for the same purpose in your own sites.".

Maybe I mistunderstood something here and if there are issues with this approach or it is not recomended this probably should be spelled out in the documentation.

It can be used, just what do you do if you have files in there that come from core modules you don't want? When using form API on front-end and load a Datetime field, css and js from that Inputfield will get added to the array, but I don't like to use backend scripts in the front-end. You add another dependency, that if core changes or has a bug you also have that on front-end after update. You have already have to take care of changed to Inputfields anyway so maybe it's no big deal. Also has cases where third party autoload modules added scripts or css to the array and it may cause troubles.

All that led me to create my own  file array for front-end.

$config->siteScripts = new FilenameArray();
$config->siteStyles = new FilenameArray();

Now that isn't something other front-end module know of so maybe it again isn't good regarding that, where the core $config->scripts could give a standard, so third party front-end modules could rely on. I just don't think mixing them isn't very nice.

  • Like 3

Share this post


Link to post
Share on other sites

All that led me to create my own  file array for front-end.

$config->siteScripts = new FilenameArray();
$config->siteStyles = new FilenameArray();

Now that isn't something other front-end module know of so maybe it again isn't good regarding that, where the core $config->scripts could give a standard, so third party front-end modules could rely on. I just don't think mixing them isn't very nice.

You can pass any FilenameArray to AIOM starting on version 3.2.

I know, this solution is not optimal but it is our solves the given problem of artamteex.

  • Like 1

Share this post


Link to post
Share on other sites

Not sure if this is a new bug or not - I haven't seen it on other sites, but I just installed it on a new site and with HTML minify turned on, certain spaces are being removed. This works as expected:

<strong>Lesson Rating: </strong>Excellent

But if the code is generated like this, the space between ":" and "Excellent" is removed:

<strong>Lesson Rating:</strong> Excellent
  • Like 1

Share this post


Link to post
Share on other sites

                   I have used AIOM on all website which have been developed by us on processwire. Recently one of our site started to throw error and it was working fine tilll few days ago.Error is

                  " Error:     Exception: The combined js file can not be written. Check if the script has sufficient permissions ".

                   We tried allowing the module  permission with 777 and also changed the permission in the config file and have failed.AIOM is able to create file but is not able to write anything in that file.

                   We are using AIOM version 3.2.1 on Processwire 2.5.3.

                  

Thanks

Share this post


Link to post
Share on other sites

Just make some steps with AIOM and have a strange issue:

CSS works fine but with the JS files on a quick and dirty try it seems that the exact same scripts won't work with AIOM and with single loading the do....

Q&D Code below:

<!-- jQuery -->
<!--<script src="
<?php
	//echo AIOM::JS(
				//  array(
				//		'tpl/js/jquery.js',
				//		'tpl/js/bootstrap.min.js',
				//		'tpl/js/owl-carousel/owl.carousel.min.js',
				//		'tpl/js/easytabs/easyResponsiveTabs.js',
				//		'tpl/js/jquery.easing.min.js',
				//		'tpl/js/flex-slider/jquery.flexslider.js',
				//		'tpl/js/jquery.appear.js',
				//		'tpl/js/jquery.inview.js',
				//		'tpl/js/jquery.prettyphoto.js',
				//		'tpl/js/jquery.nicescroll.js',
				//		'tpl/js/main.js'
				//		));
	?>
"></script>-->

<script src="<?php echo $config->urls->templates?>tpl/js/jquery.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/bootstrap.min.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/owl-carousel/owl.carousel.min.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/jquery.easing.min.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/flex-slider/jquery.flexslider.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/jquery.inview.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/jquery.prettyphoto.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/jquery.nicescroll.js"></script>
<script src="<?php echo $config->urls->templates?>tpl/js/main.js"></script>

Before posting i've checked the created files for shure.

all code seems to be there - disabled minifying in AIOM only take the files together...??

Share this post


Link to post
Share on other sites

@mr-fan - what problem are you seeing? does the minified file have any byte order marks? you might need to check the encoding of the js files, or zap gremlins

Share this post


Link to post
Share on other sites

Thanks it's working perfect now...like you thought...some codecleaning get it working.

On this project i'm working with a paid themeforrest html template that fits to the client - but on such themes there are almost many things to improve exspecially some overloaded usage of scripts and plugins on such themes...so i've tidy up first and the compress and minify.

regards mr-fan

Share this post


Link to post
Share on other sites

Is it possible to exclude pages from the HTML-minifying per template?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MoritzLost
      TrelloWire
      This is a module that allows you to automatically create Trello cards for ProcessWire pages and update them when the pages are updated. This allows you to setup connected workflows. Card properties and change handling behaviour can be customized through the extensive module configuration. Every action the module performs is hookable, so you can modify when and how cards are created as much as you need to. The module also contains an API-component that makes it easy to make requests to the Trello API and build your own connected ProcessWire-Trello workflows.
      Features
      All the things the module can do for you without any custom code: Create a new card on Trello whenever a page is added or published (you can select applicable templates). Configure the target board, target list, name and description for new cards. Add default labels and checklists to the card. Update the card whenever the page is updated (optional). When the status of the card changes (published / unpublished, hidden / unhidden, trashed / restored or deleted), move the card to a different list or archive or delete it (configurable). You can extend this through hooks in many ways: Modifiy when and how cards are created. Modify the card properties (Target board & list, title, description, et c.) before they are sent to Trello. Create your own workflows by utilizing an API helper class with many convenient utility methods to access the Trello API directly. Feedback & Future Plans
      Let me know what you think! In particular:
      If you find any bugs report them here or on Github, I'll try to fix them. This module was born out of a use-case for a client project where we manage new form submissions through Trello. I'm not sure how many use-cases there are for this module. If you do use it, tell me about it! The Trello API is pretty extensive, I'll try to add some more helper methods to the TrelloWireApi class (let me know if you need anything in particular). I'll think about how the module can support different workflows that include Twig – talk to me if you have a use-case! Next steps could be a dashboard to manage pages that are connected to a Trello card, or a new section in the settings tab to manage the Trello connection. But it depends on whether there is any interest in this 🙂 Links
      Repository on Github Complete module documentation (getting started, configuration & API documentation) [Module directory pending approval] Module configuration

    • By MoritzLost
      Process Cache Control
      This module provides a simple solution to clearing all your cache layers at once, and an extensible interface to perform various cache-related actions.
      The simple motivation behind this module was that I was tired of manually clearing caches in several places after deploying a change on a live site. The basic purpose of this module is a simple Clear all caches link in the Setup menu which clears out all caches, no matter where they hide. You can customize what exactly the module does through it's configuration menu:
      Expire or delete all cache entries in the database, or selectively clear caches by namespace ($cache API) Clear the the template render cache. Clear out specific folders inside your site's cache directory (/site/assets/cache) Clear the ProCache page render cache (if your site is using ProCache) Refresh version strings for static assets to bust client-side browser caches (this requires some setup, see the full documentation for details). This is the basic function of the module. However, you can also add different cache management action through the API and execute them through the module's interface. For this advanced usage, the module provides:
      An interface to see all available cache actions and execute them. A system log and logging output on the module page to see verify what the module is doing. A CacheControlTools class with utility functions to clear out different caches. An API to add cache actions, execute them programmatically and even modify the default action. Permission management, allowing you granular control over which user roles can execute which actions. The complete documentation can be found in the module's README.
      Plans for improvements
      If there is some interest in this, I plan to expand this to a more general cache management solution. I particular, I would like to add additional cache actions. Some ideas that came to mind:
      Warming up the template render cache for publicly accessible pages. Removing all active user sessions. Let me know if you have more suggestions!
      Links
      https://github.com/MoritzLost/ProcessCacheControl ProcessCacheControl in the Module directory CHANGELOG in the repository Screenshots


    • By Macrura
      PrevNextTabs Module
      Github: https://github.com/outflux3/PrevNextTabs
      Processwire helper modules for adding page navigation within the editor.
      Overview
      This is a very simple module that adds Previous and Next links inline with the tabs on the page editor. Hovering over the tab shows the title of the previous or next page (using the admin's built in jqueryUI tooltips.)
      Usage
      This module is typically used during development where you or your editors need to traverse through pages for the purpose of proofing, flagging and/or commenting. Rather than returning to the page tree or lister, they can navigate with these links.
      Warnings
      If you are using PW version 2.6.1 or later, the system will prevent you from leaving the page if you have unsaved edits.
      For earlier versions, to avoid accidentally losing changes made to a page that might occur if a user accidentally clicks on one of these, make sure to have the Form Save Reminder module installed.
      http://modules.processwire.com/modules/prev-next-tabs/
    • By Gadgetto
      SnipWire - Snipcart integration for ProcessWire
      Snipcart is a powerful 3rd party, developer-first HTML/JavaScript shopping cart platform. SnipWire is the missing link between Snipcart and the content management framework ProcessWire.
      With SnipWire, you can quickly turn any ProcessWire site into a Snipcart online shop. The SnipWire plugin helps you to get your store up and running in no time. Detailed knowledge of the Snipcart system is not required.
      SnipWire is free and open source licensed under Mozilla Public License 2.0! A lot of work and effort has gone into development. It would be nice if you could donate an amount to support further development:

      Status update links (inside this thread) for SnipWire development
      2020-03-21 -- SnipWire 0.8.5 (beta) released! Improves SnipWires webhooks interface and provides some other fixes and additions 2020-03-03 -- SnipWire 0.8.4 (beta) released! Improves compatibility for Windows based Systems. 2020-03-01 -- SnipWire 0.8.3 (beta) released! The installation and uninstallation process has been heavily revised. 2020-02-08 -- SnipWire 0.8.2 (beta) released! Added a feature to change the cart and catalogue currency by GET, POST or SESSION param 2020-02-03 -- SnipWire 0.8.1 (beta) released! All custom classes moved into their own namespaces. 2020-02-01 -- SnipWire is now available via ProcessWire's module directory! 2020-01-30 -- SnipWire 0.8.0 (beta) first public release! (module just submitted to the PW modules directory) 2020-01-28 -- added Custom Order Fields feature (first SnipWire release version is near!) 2020-01-21 -- Snipcart v3 - when will the new cart system be implemented? 2020-01-19 -- integrated taxes provider finished (+ very flexible shipping taxes handling) 2020-01-14 -- new date range picker, discount editor, order notifiactions, order statuses, and more ... 2019-11-15 -- orders filter, order details, download + resend invoices, refunds 2019-10-18 -- list filters, REST API improvements, new docs platform, and more ... 2019-08-08 -- dashboard interface, currency selector, managing Orders, Customers and Products, Added a WireTabs, refinded caching behavior 2019-06-15 -- taxes provider, shop templates update, multiCURL implementation, and more ... 2019-06-02 -- FieldtypeSnipWireTaxSelector 2019-05-25 -- SnipWire will be free and open source Plugin Key Features
      Fast and simple store setup Full integration of the Snipcart dashboard into the ProcessWire backend (no need to leave the ProcessWire admin area) Browse and manage orders, customers, discounts, abandoned carts, and more Multi currency support Custom order and cart fields Process refunds and send customer notifications from within the ProcessWire backend Process Abandoned Carts + sending messages to customers from within the ProcessWire backend Complete Snipcart webhooks integration (all events are hookable via ProcessWire hooks) Integrated taxes provider (which is more flexible then Snipcart own provider) Useful Links
      SnipWire in PW modules directory SnipWire Docs (please note that the documentation is a work in progress) SnipWire @GitHub (feature requests and suggestions for improvement are welcome - I also accept pull requests) Snipcart Website  
      ---- INITIAL POST FROM 2019-05-25 ----
       
×
×
  • Create New...