Skip to content

Commit c337180

Browse files
gdixonchibie
andauthored
GITC-194: Replaces ./js/libs/* with NPM libs (#9276)
* GITC-194: Replaces ./js/libs/* with NPM libs * GITC-194: Add bundleContext and fixup bundles * GITC-194: Restores svgator_player in ./v2/js/libs to pass eslint * GITC-194: Sets the fontawesome registry to avoid the need for a auth token (yarnpkg) * GITC-194: Switch py mixedCase imports to snake_case (bundle_context) * GITC-194: Removes jquery-ui as a dependency (we're using jquery-ui-dist) Co-authored-by: Chibuotu Amadi <[email protected]>
1 parent f4d7ffe commit c337180

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3062
-645
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ celerybeat.pid
3131
chatconfig/config.json
3232

3333
# Sensitive environment files
34+
.npmrc
3435
.env
3536
*.pem
3637

app/app/bundle_context.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
"""Define additional context data to be passed to any request.
3+
4+
Copyright (C) 2021 Gitcoin Core
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as published
8+
by the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Affero General Public License for more details.
15+
16+
You should have received a copy of the GNU Affero General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
"""
20+
21+
from cacheops import cached_as
22+
from perftools.models import JSONStore
23+
24+
@cached_as(JSONStore.objects.filter(view='bundleTags', key='bundleTags'), timeout=60)
25+
def templateTags():
26+
27+
# list any templateTags used inside bundle tags
28+
return ['static']
29+
30+
31+
@cached_as(JSONStore.objects.filter(view='bundleContext', key='bundleContext'), timeout=60)
32+
def context():
33+
context = {}
34+
35+
# return constructed context
36+
return context

app/app/context.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from django.utils import timezone
2727

2828
import requests
29+
from app.bundle_context import context as bundleContext
2930
from app.utils import get_location_from_ip
3031
from cacheops import cached_as
3132
from dashboard.models import Activity, Tip, UserAction
@@ -213,4 +214,8 @@ def preprocess(request):
213214
context['unclaimed_tips'] = context['unclaimed_tips'].filter(network='mainnet').cache(timeout=60)
214215
context['unclaimed_kudos'] = context['unclaimed_kudos'].filter(network='mainnet').cache(timeout=60)
215216

217+
# add bundleContext to context in dev
218+
if settings.DEBUG:
219+
context.update(bundleContext())
220+
216221
return context

app/app/templates/shared/tip_dependancies.html

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
{% load static bundle %}
1818

1919
<script src="/dynamic/js/tokens_dynamic.js"></script>
20-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
20+
2121
{% bundle merge_js file tip_dependancies %}
22-
<script src="{% static "v2/js/tokens.js" %}"></script>
23-
<script src="{% static "v2/js/ipfs.js" %}"></script>
24-
<script src="{% static "v2/js/lib/secrets.min.js" %}"></script>
22+
<script src="index.min.js" base-dir="/node_modules/ipfs-api/dist/"></script>
23+
<script src="secrets.min.js" base-dir="/node_modules/secrets.js-grempe/"></script>
24+
<script src="{% static "v2/js/tokens.js" %}"></script>
25+
<script src="{% static "v2/js/ipfs.js" %}"></script>
26+
<script src="{% static "v2/js/ethereumjs-accounts.js" %}"></script>
27+
<script src="{% static "onepager/js/send.js" %}"></script>
2528
{% endbundle %}
26-
<script src="{% static "v2/js/ethereumjs-accounts.js" %}"></script>
27-
<script src="{% static "onepager/js/send.js" %}"></script>

app/assets/v2/js/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-loop-func */
22
/* eslint-disable no-console */
33
/* eslint-disable nonblock-statement-body-position */
4-
$(document).ready(function() {
4+
document.addEventListener('DOMContentLoaded', function() {
55

66
$.fn.isInViewport = function() {
77
var elementTop = $(this).offset().top;

app/dashboard/management/commands/bundle.py

+48-15
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44

55
from django.conf import settings
66
from django.core.management.base import BaseCommand
7+
from django.template import Context, Template
78
from django.template.loaders.app_directories import get_app_template_dirs
89

910
from dashboard.templatetags.bundle import render
1011

12+
from app.bundle_context import context, templateTags
1113

12-
def rmdir(loc):
14+
15+
def rmdir(loc, depth=1):
1316
# drop both the bundled and the bundles before recreating
1417
if os.path.exists(loc) and os.path.isdir(loc):
15-
print('- Deleting assets from: %s' % loc)
16-
shutil.rmtree(loc)
18+
# list all dirs/paths in the loc
19+
files = os.listdir(loc)
20+
21+
print('%s Deleting %s assets from: %s' % ('-' * depth, len(files), loc))
1722

23+
# delete files/dirs from the given loc leaving the loc dir intact
24+
for path in files:
25+
nextLoc = os.path.join(loc, path)
26+
if os.path.isdir(nextLoc):
27+
rmdir(nextLoc, depth+1)
28+
else:
29+
os.remove(nextLoc)
1830

1931
def rmdirs(loc, kind):
2032
# base path of the assets
@@ -29,6 +41,11 @@ class Command(BaseCommand):
2941
help = 'generates .js/.scss files from bundle template tags'
3042

3143
def handle(self, *args, **options):
44+
""" This command will collect templates, render them with the bundleContext and run them through the bundle procedure"""
45+
46+
print('\nCollect template files from all apps:')
47+
48+
# get a list of all templates (rather than views to avoid segfault error that django-compressor was giving us on production)
3249
template_dir_list = []
3350
for template_dir in get_app_template_dirs('templates'):
3451
if settings.BASE_DIR in template_dir:
@@ -41,29 +58,38 @@ def handle(self, *args, **options):
4158
if ".html" in filename:
4259
template_list.append(os.path.join(base_dir, filename))
4360

61+
print('\n- %s templates discovered' % len(template_list))
62+
4463
# using regex to grab the bundle tags content from html
4564
block_pattern = re.compile(r'({%\sbundle(.|\n)*?(?<={%\sendbundle\s%}))')
4665
open_pattern = re.compile(r'({%\s+bundle\s+(js|css|merge_js|merge_css)\s+?(file)?\s+?([^\s]*)?\s+?%})')
4766
close_pattern = re.compile(r'({%\sendbundle\s%})')
48-
static_open_pattern = re.compile(r'({%\sstatic\s["|\'])')
49-
static_close_pattern = re.compile(r'(\s?%}(\"|\')?\s?\/?>)')
67+
68+
print('\nClear bundle directories:\n')
5069

5170
# remove the previously bundled files
5271
for ext in ['js', 'scss', 'css']:
5372
rmdirs('assets', ext)
5473
rmdirs('static', ext)
5574

56-
print('\nStart generating bundle files\n')
75+
print('\nStart generating bundled assets (using app.bundleContext as context):\n')
5776

5877
# store unique entries for count
5978
rendered = dict()
6079

80+
# get the tags and context from bundleContext
81+
tags = templateTags()
82+
bundleContext = context()
83+
# load the bundleContext into a Context instance so that it can be fed to Template.render
84+
bundleContext = Context(bundleContext)
85+
86+
# check every template for bundle tags
6187
for template in template_list:
6288
try:
63-
f = open(('%s' % template).replace('/', os.sep), 'r', encoding='utf8')
64-
65-
t = f.read()
66-
if re.search(block_pattern, t) is not None:
89+
# read the template file
90+
t = open(('%s' % template).replace('/', os.sep), 'r', encoding='utf8').read()
91+
# check for bundle tags
92+
if re.search(block_pattern, t):
6793
for m in re.finditer(block_pattern, t):
6894
block = m.group(0)
6995
details = re.search(open_pattern, block)
@@ -76,15 +102,22 @@ def handle(self, *args, **options):
76102
block = re.sub(open_pattern, '', block)
77103
block = re.sub(close_pattern, '', block)
78104

79-
# clean static helper if we havent ran this through parse
80-
block = re.sub(static_open_pattern, '', block)
81-
block = re.sub(static_close_pattern, '>', block)
105+
# add static helper to the block
106+
block = '{% ' + 'load %s' % ' '.join(tags) + ' %}\n' + block
107+
108+
# create a template from the block
109+
block = Template(block)
110+
111+
# render the template with bundleContext
112+
block = block.render(bundleContext)
113+
114+
# clean static_url from the path (its not required but is included as legacy in most script/link inclusions)
115+
block = block.replace(settings.STATIC_URL, "")
82116

83117
# render the template (producing a bundle file)
84118
rendered[render(block, kind, 'file', name, True)] = True
85-
86119
except Exception as e:
87120
print('-- X - failed to parse %s: %s' % (template, e))
88121
pass
89122

90-
print('\nGenerated %s bundle files' % len(rendered))
123+
print('\n\n------------------- Generated %s bundled assets ------------------- \n\n' % len(rendered))

app/dashboard/templates/bounty/details.html

+8-5
Original file line numberDiff line numberDiff line change
@@ -529,20 +529,23 @@ <h3>{{ noscript.keywords }}</h3>
529529

530530
{% include 'shared/current_profile.html' %}
531531

532-
<script src="{% static "v2/js/lib/highlight.js" %}"></script>
533-
<script src="{% static "v2/js/lib/markdown-it.js" %}"></script>
532+
<script src="/dynamic/js/tokens_dynamic.js"></script>
533+
534+
{% bundle merge_js file libs %}
535+
<script src="index.min.js" base-dir="/node_modules/ipfs-api/dist/"></script>
536+
<script src="highlight.min.js" base-dir="/node_modules/@highlightjs/cdn-assets/"></script>
537+
<script src="markdown-it.min.js" base-dir="/node_modules/markdown-it/dist/"></script>
538+
<script src="daterangepicker.js" base-dir="/node_modules/daterangepicker/"></script>
539+
{% endbundle %}
534540

535541
<script src="{% static "v2/js/abi.js" %}"></script>
536-
<script src="/dynamic/js/tokens_dynamic.js"></script>
537542
<script src="{% static "v2/js/tokens.js" %}"></script>
538543
<script src="{% static "v2/js/amounts.js" %}"></script>
539544
<script src="{% static "v2/js/clipboard.js" %}"></script>
540-
<script src="{% static "v2/js/lib/daterangepicker.min.js" %}"></script>
541545
<script src="{% static "v2/js/pages/bounty_funder_payout_reminder.js" %}"></script>
542546
<script src="{% static "v2/js/pages/bounty_share.js" %}"></script>
543547
<script src="{% static "v2/js/pages/bounty_details.js" %}"></script>
544548
<script src="{% static "v2/js/hackathon-projects.js" %}"></script>
545-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
546549
<script src="{% static "v2/js/ipfs.js" %}"></script>
547550
<script src="{% static "v2/js/user-search.js" %}"></script>
548551
<script src="{% static "v2/js/waiting_room_entertainment.js" %}"></script>

app/dashboard/templates/bounty/details2.html

+24-16
Original file line numberDiff line numberDiff line change
@@ -1097,15 +1097,21 @@ <h3>{{ noscript.keywords }}</h3>
10971097
{% include 'shared/footer.html' %}
10981098
{% include 'shared/current_profile.html' %}
10991099
</div>
1100-
<script src="{% static "v2/js/lib/highlight.js" %}"></script>
1101-
<script src="{% static "v2/js/lib/markdown-it.js" %}"></script>
11021100

1103-
<script src="{% static "v2/js/abi.js" %}"></script>
1101+
{% bundle merge_js file libs %}
1102+
<script src="highlight.min.js" base-dir="/node_modules/@highlightjs/cdn-assets/"></script>
1103+
<script src="markdown-it.min.js" base-dir="/node_modules/markdown-it/dist/"></script>
1104+
{% endbundle %}
1105+
11041106
<script src="/dynamic/js/tokens_dynamic.js"></script>
1107+
<script src="{% static "v2/js/abi.js" %}"></script>
11051108
<script src="{% static "v2/js/tokens.js" %}"></script>
11061109
<script src="{% static "v2/js/amounts.js" %}"></script>
11071110
<script src="{% static "v2/js/clipboard.js" %}"></script>
1108-
<script src="{% static "v2/js/lib/qrcode.js" %}"></script>
1111+
1112+
{% bundle merge_js file qrcode %}
1113+
<script src="qrcode.min.js" base-dir="/node_modules/qrcodejs/"></script>
1114+
{% endbundle %}
11091115

11101116
<script>
11111117
$('body').bootstrapTooltip({
@@ -1165,21 +1171,23 @@ <h3>{{ noscript.keywords }}</h3>
11651171
<script src="{% static "v2/js/pages/bounty_detail/PYPL.js" %}"></script>
11661172

11671173
{% elif web3_type == 'web3_modal' %}
1168-
1169-
<script src="{% static "v2/js/pages/bounty_detail/web3_modal.js" %}"></script>
1170-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
1171-
<script src="{% static "v2/js/ipfs.js" %}"></script>
1172-
1174+
{% bundle merge_js file web3_modal %}
1175+
<script src="index.min.js" base-dir="/node_modules/ipfs-api/dist/"></script>
1176+
<script src="{% static "v2/js/pages/bounty_detail/web3_modal.js" %}"></script>
1177+
<script src="{% static "v2/js/ipfs.js" %}"></script>
1178+
{% endbundle %}
11731179
{% endif %}
11741180

1181+
{% bundle merge_js file details %}
1182+
<link rel="stylesheet" type="text/x-scss" href={% static "v2/scss/lib/daterangepicker.scss" %} />
1183+
<script src="{% static "v2/js/pages/bounty_details2.js" %}"></script>
1184+
<script src="{% static "v2/js/pages/bounty_funder_payout_reminder.js" %}"></script>
1185+
<script src="{% static "v2/js/pages/bounty_share.js" %}"></script>
1186+
<script src="{% static "v2/js/hackathon-projects.js" %}"></script>
1187+
<script src="{% static "v2/js/user-search.js" %}"></script>
1188+
<script src="{% static "v2/js/waiting_room_entertainment.js" %}"></script>
1189+
{% endbundle %}
11751190

1176-
<script src="{% static "v2/js/pages/bounty_details2.js" %}"></script>
1177-
<script src="{% static "v2/js/lib/daterangepicker.min.js" %}"></script>
1178-
<script src="{% static "v2/js/pages/bounty_funder_payout_reminder.js" %}"></script>
1179-
<script src="{% static "v2/js/pages/bounty_share.js" %}"></script>
1180-
<script src="{% static "v2/js/hackathon-projects.js" %}"></script>
1181-
<script src="{% static "v2/js/user-search.js" %}"></script>
1182-
<script src="{% static "v2/js/waiting_room_entertainment.js" %}"></script>
11831191
</body>
11841192

11851193
</html>

app/dashboard/templates/bounty/fulfill.html

+9-6
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,16 @@ <h4 class="text-center mb-4">{% trans "Submit Work" %}</h4>
227227
<script src="{% static "v2/js/user-search.js" %}"></script>
228228

229229
{% if is_bounties_network %}
230-
<script src="{% static "v2/js/abi.js" %}"></script>
231-
<script src="{% static "v2/js/tokens.js" %}"></script>
232230
<script src="/dynamic/js/tokens_dynamic.js"></script>
233-
<script src="{% static "v2/js/pages/shared_bounty_mutation_estimate_gas.js" %}"></script>
234-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
235-
<script src="{% static "v2/js/ipfs.js" %}"></script>
236-
<script src="{% static "v2/js/pages/fulfill_bounty/ETH.js" %}"></script>
231+
232+
{% bundle merge_js file bounty_fullfill %}
233+
<script src="index.min.js" base-dir="/node_modules/ipfs-api/dist/"></script>
234+
<script src="{% static "v2/js/tokens.js" %}"></script>
235+
<script src="{% static "v2/js/ipfs.js" %}"></script>
236+
<script src="{% static "v2/js/abi.js" %}"></script>
237+
<script src="{% static "v2/js/pages/shared_bounty_mutation_estimate_gas.js" %}"></script>
238+
<script src="{% static "v2/js/pages/fulfill_bounty/ETH.js" %}"></script>
239+
{% endbundle %}
237240
{% else %}
238241
<script src="{% static "v2/js/pages/fulfill_bounty/token.js" %}"></script>
239242
{% endif %}

app/dashboard/templates/bounty/increase.html

+13-22
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,21 @@ <h5 class="text-uppercase h3 font-weight-bold mb-0">{% trans "Total"%}</h5>
169169
{% include 'shared/footer.html' %}
170170
</div>
171171

172-
</body>
172+
<script>
173+
document.is_funder_github_user_same = {{ is_funder }};
174+
document.FEE_PERCENTAGE = {{ FEE_PERCENTAGE }};
175+
{% if expired_coupon %}
176+
_alert({ message: 'This coupon has expired.' }, 'danger');
177+
{% endif %}
178+
</script>
173179

180+
{% include 'shared/tip_dependancies.html' %}
174181

175-
<script>
176-
document.is_funder_github_user_same = {{ is_funder }};
177-
document.FEE_PERCENTAGE = {{ FEE_PERCENTAGE }};
178-
{% if expired_coupon %}
179-
_alert({ message: 'This coupon has expired.' }, 'danger');
180-
{% endif %}
181-
</script>
182-
183-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
184-
<script src="{% static "v2/js/ipfs.js" %}"></script>
185-
<script src="{% static "v2/js/lib/secrets.min.js" %}"></script>
186-
<script src="{% static "v2/js/ethereumjs-accounts.js" %}"></script>
187-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
188-
<script src="{% static "v2/js/ipfs.js" %}"></script>
189-
<script src="{% static "v2/js/amounts.js" %}"></script>
190-
<script src="{% static "v2/js/abi.js" %}"></script>
191-
<script src="/dynamic/js/tokens_dynamic.js"></script>
192-
<script src="{% static "v2/js/tokens.js" %}"></script>
193-
<script src="{% static "v2/js/pages/shared_bounty_mutation_estimate_gas.js" %}"></script>
194-
<script src="{% static "v2/js/pages/increase_bounty.js" %}"></script>
195-
<script src="{% static "onepager/js/send.js" %}"></script>
182+
<script src="{% static "v2/js/amounts.js" %}"></script>
183+
<script src="{% static "v2/js/abi.js" %}"></script>
184+
<script src="{% static "v2/js/pages/shared_bounty_mutation_estimate_gas.js" %}"></script>
185+
<script src="{% static "v2/js/pages/increase_bounty.js" %}"></script>
196186

187+
</body>
197188

198189
</html>

app/dashboard/templates/bounty/new_bounty.html

+8-4
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,14 @@ <h5 class="text-uppercase h3 font-weight-bold mb-0">Total</h5>
730730
{% endfor %}
731731
]
732732
</script>
733-
<script src="{% static "v2/js/lib/daterangepicker.min.js" %}"></script>
734-
<script src="{% static "v2/js/lib/highlight.js" %}"></script>
735-
<script src="{% static "v2/js/lib/markdown-it.js" %}"></script>
736-
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script>
733+
734+
{% bundle merge_js file libs %}
735+
<script src="index.min.js" base-dir="/node_modules/ipfs-api/dist/"></script>
736+
<script src="highlight.min.js" base-dir="/node_modules/@highlightjs/cdn-assets/"></script>
737+
<script src="markdown-it.min.js" base-dir="/node_modules/markdown-it/dist/"></script>
738+
<script src="daterangepicker.js" base-dir="/node_modules/daterangepicker/"></script>
739+
{% endbundle %}
740+
737741
<script src="{% static "v2/js/ipfs.js" %}"></script>
738742
<script src="{% static "v2/js/abi.js" %}"></script>
739743

0 commit comments

Comments
 (0)