如何用python拆分大型CSS文件?

时间:2023-01-15 10:23:35

The problem that I am having comes form the fact that IE 8 has limits for CSS files. In my Flask project, some of my CSS files exceed these limits and they don't render properly.

我遇到的问题来自IE 8对CSS文件的限制。在我的Flask项目中,我的一些CSS文件超出了这些限制,并且它们无法正确呈现。

Does anyone know how I can split CSS files with Python so that I can make my files meet the limits that IE 8 imposes on CSS?

有谁知道我如何用Python分割CSS文件,以便我可以使我的文件符合IE 8对CSS的限制?

1 个解决方案

#1


2  

Here's what I came up with. It will of course have to be modified to become either a command line tool to run after SASS, or perhaps some sort of filter in your Flask code.

这就是我想出的。它当然必须被修改成为在SASS之后运行的命令行工具,或者可能是Flask代码中的某种过滤器。

css = "the contents of your enormous css file"
rules = re.findall(r'[^\{]+\{[^\}]*\}', css, re.MULTILINE)

print 'Number of rules: ', len(rules)

with open('output1.css', 'w') as f:
    f.write('\n'.join(rules[:4096]))
if len(rules) > 4096:
    with open('output2.css', 'w') as f:
        f.write('\n'.join(rules[4096:]))

#1


2  

Here's what I came up with. It will of course have to be modified to become either a command line tool to run after SASS, or perhaps some sort of filter in your Flask code.

这就是我想出的。它当然必须被修改成为在SASS之后运行的命令行工具,或者可能是Flask代码中的某种过滤器。

css = "the contents of your enormous css file"
rules = re.findall(r'[^\{]+\{[^\}]*\}', css, re.MULTILINE)

print 'Number of rules: ', len(rules)

with open('output1.css', 'w') as f:
    f.write('\n'.join(rules[:4096]))
if len(rules) > 4096:
    with open('output2.css', 'w') as f:
        f.write('\n'.join(rules[4096:]))