Skip to content

Commit cb4cbd1

Browse files
authored
minify js files (#7)
* minify js files * install jsmin * fix bug * bugfix
1 parent 56af5da commit cb4cbd1

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This package should be used in all sphinx projects under the [@deepmodeling](htt
55
## Features
66

77
- Adding the DeepModeling banner
8-
- Minify HTML files
8+
- Minify HTML and Javascript files
99

1010
## How to use it
1111

deepmodeling_sphinx/inject.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44

55
import htmlmin
6+
from jsmin import jsmin
67
from sphinx.application import Sphinx
78
from sphinx.util.fileutil import copy_asset_file
89

@@ -72,9 +73,20 @@ def render(self, template, render_context):
7273
app.builder.templates)
7374

7475

76+
def minify_js_files(app, exception):
77+
for js in app.builder.script_files:
78+
fn = os.path.join(app.builder.outdir, js)
79+
if os.path.isfile(fn):
80+
with open(fn, 'r+') as f:
81+
minified_js = jsmin(f.read())
82+
f.seek(0)
83+
f.write(minified_js)
84+
f.truncate()
85+
86+
7587
def setup(app: Sphinx) -> Dict[str, Any]:
7688
app.connect('builder-inited', copy_custom_files)
7789
app.connect('html-page-context', insert_sidebar)
78-
app.connect('html-page-context', minify_html_files)
90+
app.connect('build-finished', minify_js_files)
7991

8092
return {'parallel_read_safe': True}

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
install_requires=[
99
'sphinx',
1010
'htmlmin',
11+
'jsmin',
1112
],
1213
package_data={
1314
'deepmodeling_sphinx': ['banner.html',

0 commit comments

Comments
 (0)