Make CSS, JS, HTML compression tool in Python

Other language site
ja ja
Google Translate
  • -

    シェア
  • ---

    LINEで送る
  • -

    はてなブックマーク
    ブックマーク
  • -

    pocket
  • -

    rss
python logo

By using Python's css_html_js_minify package, you can easily create CSS, JavaScript, HTML compression tools.

Download css_html_js_minify

Use the pip command to download the css_html_js_minify package to your Python environment.

Execute the command as superuser.

pip install –upgrade css_html_js_minify

 Easy source code

Let's see the source code.

#!/usr/bin/env python
import os
import requests
from css_html_js_minify import process_single_html_file, process_single_js_file, process_single_css_file, html_minify, js_minify, css_minify

encoding = 'utf-8_sig'

url_base = 'https://●●●.com/wp-content/'
url_plugins = url_base + 'plugins/'
url_theme_parent = url_base + 'themes/xeory_base/'
url_theme_custom = url_base + 'themes/xeory_base_custom/'
out_file = './late-read.css'

list = [ \
    url_theme_parent + 'base.css', \
    url_theme_parent + 'style.css?ver=4.8', \
    url_theme_custom + 'style.css?ver=4.8', \
    url_plugins + 'contact-form-7/includes/css/styles.css', \
    url_plugins + 'wp-external-links/public/css/wpel.css?ver=2.1.3', \
    url_theme_custom + 'js/prism.css?ver=4.8', \
    url_plugins + 'easy-fancybox/fancybox/jquery.fancybox-1.3.8.min.css?ver=1.6', \
    url_plugins + 'jetpack/css/jetpack.css?ver=5.1'
]

if os.path.exists(out_file):
    os.remove(out_file)

f_add = open(out_file + '.tmp', 'a', encoding=encoding)

for css in list:
    res = requests.get(css)
    res.encoding = res.apparent_encoding
    # fw = open(css)
    # f_add.write(fw.read())
    f_add.write(res.text)
    # fw.close()

f_add.close()

os.rename(out_file + '.tmp', out_file)

process_single_css_file(out_file, overwrite=False)

"Import requests" in  line 3 is a package related to http request.

Here, we will collect CSS files uploaded to the website by http request.

In line 4, I imported the downloaded css_js_html_minify package.

"encoding = 'utf - 8 _ sig'" in line 6 is the character code encoding for the Windows environment.

By forcing utf - 8 even in Windows environment, we will make it the same result as Linux and Mac environments.

Lines 8 to 23 define the list of css files to retrieve.

Here, we collect CSS files of Web sites created with WordPress.

You can read this list definition entirely from an external file.

Here, since it is a tool, it has no merit of converting to an external file, so it is writing directly.

Lines 25 and 26 are the result file cleanup.

From line 30 to line 40, we get the content of css file with http request. And I will add the contents to the tmp file.

This is done for the number of defined css lists, and finally the template file is reflected in the result file.

In fact, we just changed the template file name to the output file name.

The commented part in for statement processing is not an http request.

This is processing when reading css file from a local file and acquiring it.

I left it as a reference.

Compress css file with "process_single_css_file (out_file, overwrite = False)" on line 42.

File compression is this one line only.

Easy, right?

The result file is "***. min.css".

If it is a JavaScript file it will be "process_single_js_file ()".

If it is an html file it will be "process_single_html_file ()".

SNS also distributes articles.
Leave a Reply

*

If you like this article, share it!