-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle-assets.py
99 lines (77 loc) · 3.31 KB
/
bundle-assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os.path
from cssmin import cssmin
from rjsmin import jsmin
class Bundle(object):
"""A bundle is a unit to organize groups of media files, which filters to apply and where to store them.
This class is inspired by the class ``Bundle`` from the ``webassets`` library.
However, this class is only for the use within this project, and, hence, does not need to be general.
This leads to the advantage that an instance of this class does not need an ``Environment`` class for
translating relative paths to absolute ones. All paths are fixed for the use within this project.
We are not relying on ``flask_assets`` and ``webassets`` as it seems that they are not actively
maintained anymore. Additionally, those library do not work with Flask 3.0+.
"""
STATIC_FOLDER = 'ckanext/datacomparison/static/'
def __init__(self, *contents, **options):
self.contents = contents
self.output = options.pop('output', None)
self.filters = options.pop('filters', None)
if self.output is not None:
self.output = self.STATIC_FOLDER + self.output
def build(self):
"""Builds the bundle.
All files are combined and stored at the specified location.
If any filters where given, they are applied before storing the combined bundle.
Other than with the original implementation of this method, the output will always be generated;
simulating a ``Bundle.build()`` call with ``force=True``.
Step 1: Read all input files into one string
Step 2: Apply filters
Step 3: Write output file
"""
if self.contents is None or self.output is None:
return
content = ''
for file in self.contents:
if not os.path.isfile(file):
continue
content += open(file, 'r').read()
if not content.endswith('\n'):
content += '\n'
if self.filters == 'cssmin':
content = cssmin(content)
elif self.filters == 'rjsmin':
content = jsmin(content, keep_bang_comments=False)
else:
raise NotImplementedError('Filter ' + self.filters + ' not implemented.')
out_dir = os.path.dirname(self.output)
os.makedirs(out_dir, exist_ok=True)
open(self.output, 'w').write(content)
NPM_PATH = 'node_modules/'
JS_PATH = 'js/'
CSS_PATH = 'css/'
bundles = {
'plotly_js': Bundle(
NPM_PATH + 'plotly.js/dist/plotly.min.js',
filters='rjsmin',
output=JS_PATH + 'plotly.min.js'
),
'papaparse_js': Bundle(
NPM_PATH + 'papaparse/papaparse.min.js',
filters='rjsmin',
output=JS_PATH + 'papaparse.min.js'
),
'datatables_js': Bundle(
NPM_PATH + 'datatables.net/js/dataTables.min.js',
NPM_PATH + 'datatables.net-searchbuilder/js/dataTables.searchBuilder.min.js',
filters='rjsmin',
output=JS_PATH + 'dataTables.bundle.min.js'
),
'datatables_css': Bundle(
NPM_PATH + 'datatables.net-dt/css/dataTables.dataTables.min.css',
NPM_PATH + 'datatables.net-searchbuilder-dt/css/searchBuilder.dataTables.min.css',
filters='cssmin',
output=CSS_PATH + 'dataTables.bundle.min.css'
)
}
for name, bundle in bundles.items():
print(' building bundle:', name)
bundle.build()