NewbieSSProgrammer NewbieSSProgrammer - 3 months ago 12
CSS Question

Silverstripe requirements combined files CSS and JS

In SS we can do combined files to make run websites faster. But It was a problem when some files is being updated and the are not refreshed into combined file. I must delete the combined file and flush the cache to recreate the file... Is there are a function to redo the creation of the combined file when a CSS file being changed? Like check the date of files?

Example :

$stylesheets = array(
"$themeDir/css/HomePage.css",
"$themeDir/css/Page.css",
);

Requirements::combine_files('combinedfiles.css', $stylesheets');

Answer

Files aren't being combined when in dev mode. So during development, when files change a lot, make sure to run your site in dev-mode.

You should also require all your files normally. Changed code:

$stylesheets = [
    $this->themeDir() . '/css/HomePage.css',
    $this->themeDir() . '/css/Page.css'
];

// require the css stylesheet normally
foreach ($stylesheets as $stylesheet) {
    Requirements::css($stylesheet);
}

// combine the files. In dev-mode, this has no effect
Requirements::combine_files('combinedfiles.css', $stylesheets);

When your website is in live mode, the individual files will be replaced with the combined file.