Minifying resource content and creating static files

At my job we use resources to add JS and CSS to our sites. I’m sure the impact of this is minor in comparison to using static resources, but it would be nice to be able to do something like this:

<head>
    [[generateStyles?
        &parents=`23` [[- &resources would also be supported a la get|pdoResources ]]
        &minify=`nano` [[- preference on which minification tool to use ]]
        &concat=`1` [[- should the css files be concatenated? ]]
    ]]
</head>
<body>
    ...
    [[generateScripts?
        &parents=`24`
        &uglify=`terser` [[- Some option to add a methodology to the uglification ]]
        &module=`1`
    ]]
</body>

My ideal scenario would be that it would take the content from those resources and make static files for them for visitors of the site, and then they would be individual scripts/link tags for logged in users for easier debugging.

Is this reasonably easy to do?

P.S. I’ve used MinifyX in the past and ran into issues with how it got rid of whitespace and comments in JS.

What PHP library are you planning to use for this minification?

MinifyX uses Assetic which may also work for this use case (but probably will create the same JS issues.)


This very much depends on your programming skills and how familiar you are with the PHP library you’re going to use.

This very much depends on your programming skills and how familiar you are with the PHP library you’re going to use.

That’s very fair, I think I was less referring towards the library/minification part and more referencing the creating static files from content in MODX resources.


What PHP library are you planning to use for this minification?

I haven’t looked deeply into it, it seems like the pickings are slim after a quick google search. I was hoping there was something that would be able to handle obfuscation with JS (dream scenario would be something that could create per page CSS that stripped away anything that wasn’t used on the page, but that’s for another post)

This shouldn’t be that hard. You can query the resources with xPDO, concat their content and write the result into a file (using a PHP function like file_put_contents or maybe xPDOCacheManager.writeFile).

The trickier part is to figure out how to name the files (so that you don’t create duplicate files with the same content, when the snippet is called with the same parameters on different pages), when to regenerate a file and when to clear all generated files.