From 58ff67b6279938049e84eb51aae8d5f6a71d8010 Mon Sep 17 00:00:00 2001 From: Kyle McCormick Date: Mon, 5 Jun 2023 09:15:47 -0400 Subject: [PATCH] Revert "build: remove prefixes from XModule resource copies (#32287)" This reverts commit 585b96583a6f5ea68a4d59c2554be40064a59a5f. --- xmodule/static_content.py | 42 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/xmodule/static_content.py b/xmodule/static_content.py index 57eb8ce23487..58ffcb2de12b 100755 --- a/xmodule/static_content.py +++ b/xmodule/static_content.py @@ -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)