Skip to content

Instantly share code, notes, and snippets.

@mikeclagg
Created November 21, 2012 00:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeclagg/4122228 to your computer and use it in GitHub Desktop.
Save mikeclagg/4122228 to your computer and use it in GitHub Desktop.
Read a css file that contains @import directives, follows those to build a list of files to concatenate for web optimization
def getCssFiles(baseCssFile, css_files):
import os, re
"""
Checks for css files that are just @import containers,
traverses the files and builds a list in the order they
appear in the files. This prepares a list to use for
bundling n webassets
"""
with open(baseCssFile, 'r') as f:
# Check to see if baseCssFile contains an @import statement at the top
isImport = (f.readline()[:7] == '@import')
f.seek(0) # move the file read cursor back to the beginning
if isImport:
for line in f.readlines():
# regexp for the stuff in the quotes
reg = re.search('\"(.*)\"',line)
if reg:
cssFile = reg.group(0).replace('"','')
# determine absolute path for Bundle() arg
absCssFile = os.path.split(os.path.abspath(baseCssFile))[0] + '/' + cssFile
# Yes, read the first line of every file to determine if it is
with open(absCssFile,'r') as cf:
# Check to see if each file contains an @import statement at the top
eachIsImport = (cf.readline()[:7] == '@import')
if not eachIsImport:
# Append files to object passed in
css_files.append(absCssFile)
else:
# *Recursion: for a definition see *Recursion
getCssFiles(absCssFile,css_files)
cf.closed
f.closed
return css_files
# run the function, my file has 15 import statements,
# one being an import to another file with import statements
# The reason to do this is because CSS loading order is
# important, and needs to be preserved
baseCssFile = '../public/web/assets/css/base.css'
cssFiles = getCssFiles(baseCssFile,[])
for cssFile in cssFiles:
print cssFile
@mikeclagg
Copy link
Author

../public/web/assets/css/base.css Looks like this:

@import url("../../../assets/css/base.css");
@import url("common.css");
@import url("table.css");
@import url("form.css");
@import url("list.css");
@import url("effects.css");
@import url("component.css");
@import url("layout.css");
@import url("nav.css");
@import url("calendar.css");
@import url("range.css");
@import url("search.css");
@import url("map.css");
@import url("panel.css");
@import url("entity.css");
@import url("griddle.css");
@import url("auth.css");
@import url("conditional.css");

/assets/css/base.css is a shared file between the apps and the main site and looks like this:

@import url("directions.css");
@import url("grid.css");
@import url("tags.css");
@import url("upload.css");

Having many smaller files for development reduces merge conflicts with a medium sized dev team, but this is not ideal for production.
I will be releasing the entire build script this is part of soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment