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
      This is a new module that 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) 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.
      Beta release
      Note that I consider this a Beta release. Since the module is relatively aggressive in deleting some caches, I would advise you to install in on a test environment before using it on a live site.
      Let me know if you're getting any errors, have trouble using the module or if you have suggestions for improvement!
      In particular, can someone let me know if this module causes any problems with the ProCache module? I don't own or use it, so I can't check. As far as I can tell, ProCache uses a folder inside the cache directory to cache static pages, so my module should be able to clear the ProCache site cache as well, I'd appreciate it if someone can test that for me.
      Future plans
      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

    • By joshua
      This module is (yet another) way for implementing a cookie management solution.
      Of course there are several other possibilities:
      - https://processwire.com/talk/topic/22920-klaro-cookie-consent-manager/
      - https://github.com/webmanufaktur/CookieManagementBanner
      - https://github.com/johannesdachsel/cookiemonster
      - https://www.oiljs.org/
      - ... and so on ...
      In this module you can configure which kind of cookie categories you want to manage:

      You can also enable the support for respecting the Do-Not-Track (DNT) header to don't annoy users, who already decided for all their browsing experience.
      Currently there are four possible cookie groups:
      - Necessary (always enabled)
      - Statistics
      - Marketing
      - External Media
      All groups can be renamed, so feel free to use other cookie group names. I just haven't found a way to implement a "repeater like" field as configurable module field ...
      When you want to load specific scripts ( like Google Analytics, Google Maps, ...) only after the user's content to this specific category of cookies, just use the following script syntax:
      <script type="optin" data-type="text/javascript" data-category="statistics" data-src="/path/to/your/statistic/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing" data-src="/path/to/your/mareketing/script.js"></script> <script type="optin" data-type="text/javascript" data-category="external_media" data-src="/path/to/your/external-media/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing">console.log("Inline scripts are also working!");</script> The type has to be "optin" to get recognized by PrivacyWire, the data-attributes are giving hints, how the script shall be loaded, if the data-category is within the cookie consents of the user. These scripts are loaded asynchronously after the user made the decision.
      If you want to give the users the possibility to change their consent, you can use the following Textformatter:
      [[privacywire-choose-cookies]] It's planned to add also other Textformatters to opt-out of specific cookie groups or delete the whole consent cookie.
      You can also add a custom link to output the banner again with a link / button with following class:
      <a href="#" class="privacywire-show-options">Show Cookie Options</a> <button class="privacywire-show-options">Show Cookie Options</button> This module is still in development, but we already use it on several production websites.
      You find it here: https://github.com/blaueQuelle/privacywire/tree/master
      Download: https://github.com/blaueQuelle/privacywire/archive/master.zip
      I would love to hear your feedback 🙂
      Edit: Updated URLs to master tree of git repo
       
    • By David Karich
      Admin Page Tree Multiple Sorting
      ClassName: ProcessPageListMultipleSorting
      Extend the ordinary sort of children of a template in the admin page tree with multiple properties. For each template, you can define your own rule. Write each template (template-name) in a row, followed by a colon and then the additional field names for sorting.
      Example: All children of the template "blog" to be sorted in descending order according to the date of creation, then descending by modification date, and then by title. Type:
      blog: -created, -modified, title  Installation
      Copy the files for this module to /site/modules/ProcessPageListMultipleSorting/ In admin: Modules > Check for new modules. Install Module "Admin Page Tree Multible Sorting". Alternative in ProcessWire 2.4+
      Login to ProcessWire backend and go to Modules Click tab "New" and enter Module Class Name: "ProcessPageListMultipleSorting" Click "Download and Install"   Compatibility   I have currently tested the module only under PW 2.6+, but think that it works on older versions too. Maybe someone can give a feedback.     Download   PW-Repo: http://modules.processwire.com/modules/process-page-list-multiple-sorting/ GitHub: https://github.com/FlipZoomMedia/Processwire-ProcessPageListMultipleSorting     I hope someone can use the module. Have fun and best regards, David
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version (has been tested up to v3.3), add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "https://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


×
×
  • Create New...