Skip to content

Commit

Permalink
Revert "build: remove prefixes from XModule resource copies (#32287)"
Browse files Browse the repository at this point in the history
This reverts commit 585b965.
  • Loading branch information
NawfalAhmed committed Jun 14, 2023
1 parent e00dcfb commit 717182c
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions xmodule/static_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,44 @@ def _ensure_dir(directory):
def _write_styles(selector, output_root, classes, css_attribute, suffix):
"""
Write the css fragments from all XModules in `classes`
into `output_root` as individual files
into `output_root` as individual files, hashed by the contents to remove
duplicates
"""
contents = {}

css_fragments = defaultdict(set)
for class_ in classes:
class_css = getattr(class_, css_attribute)()
fragment_paths = class_css.get('scss', [])
if not fragment_paths:
continue
fragment_names = []
for fragment_path in fragment_paths:
with open(fragment_path, 'rb') as fragment_file:
fragment = fragment_file.read()
fragment_name = "{hash}.{type}".format(
hash=hashlib.md5(fragment).hexdigest(),
type='scss')
# Prepend _ so that sass just includes the files into a single file
filename = '_' + fragment_name
contents[filename] = fragment
fragment_names.append(fragment_name)
for filetype in ('sass', 'scss', 'css'):
for idx, fragment_path in enumerate(class_css.get(filetype, [])):
with open(fragment_path, 'rb') as fragment_file:
fragment = fragment_file.read()
css_fragments[idx, filetype, fragment].add(class_.__name__)
css_imports = defaultdict(set)
for (idx, filetype, fragment), classes in sorted(css_fragments.items()): # lint-amnesty, pylint: disable=redefined-argument-from-local
fragment_name = "{idx:0=3d}-{hash}.{type}".format(
idx=idx,
hash=hashlib.md5(fragment).hexdigest(),
type=filetype)
# Prepend _ so that sass just includes the files into a single file
filename = '_' + fragment_name
contents[filename] = fragment

for class_ in classes:
css_imports[class_].add(fragment_name)

for class_, fragment_names in sorted(css_imports.items()):
module_styles_lines = []
module_styles_lines.append("""{selector}.xmodule_{class_.__name__} {{""".format(

fragment_names = sorted(fragment_names)
module_styles_lines.append("""{selector}.xmodule_{class_} {{""".format(
class_=class_, selector=selector
))
module_styles_lines.extend(f' @import "{name}";' for name in fragment_names)
module_styles_lines.append('}')
file_hash = hashlib.md5("".join(fragment_names).encode('ascii')).hexdigest()

contents[f"{class_.__name__}{suffix}.{file_hash}.scss"] = '\n'.join(module_styles_lines)
contents[f"{class_}{suffix}.{file_hash}.scss"] = '\n'.join(module_styles_lines)

_write_files(output_root, contents)

Expand Down

0 comments on commit 717182c

Please sign in to comment.