Skip to content

Commit 4201051

Browse files
authored
Bundle Analysis: update module <-> changed files matching heuristic (#488)
1 parent 01df5a3 commit 4201051

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

shared/bundle_analysis/report.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,33 @@ def modules(
103103
.filter(Asset.id == self.asset.id)
104104
)
105105

106-
# Apply additional filter if pr_changed_files is provided
107-
if pr_changed_files:
108-
# Normalize pr_changed_files to have './' prefix for relative paths
109-
normalized_files = {
110-
f"./{file}" if not file.startswith("./") else file
111-
for file in pr_changed_files
112-
}
113-
query = query.filter(Module.name.in_(normalized_files))
114-
115-
modules = query.all()
116-
return [ModuleReport(self.db_path, module) for module in modules]
106+
if pr_changed_files is None:
107+
return [ModuleReport(self.db_path, module) for module in query]
108+
109+
"""
110+
Apply filter on modules where the module name has to be part of the PR's changed file list.
111+
However we can't simply do a simple equality match because the module names we store is relative
112+
to the root of the app while the PR's file path is relative to the root of the repo. So we will
113+
need to check that any of the PR's file lists ends with any of the modules for the given asset.
114+
For example,
115+
PR changed files: ["abc/def.ts", "ghi/jkl.ts"],
116+
modules: ["def.ts", "mno.ts"]
117+
-> ["def.ts"]
118+
"""
119+
normalized_changed_files = [
120+
os.path.normpath(path[2:] if path.startswith("./") else path)
121+
for path in pr_changed_files
122+
]
123+
filtered_modules = set()
124+
for file in normalized_changed_files:
125+
for module in query:
126+
normalized_module = os.path.normpath(
127+
module.name[1:] if file.startswith(".") else module.name
128+
)
129+
if file.endswith(normalized_module):
130+
filtered_modules.add(module)
131+
132+
return [ModuleReport(self.db_path, module) for module in filtered_modules]
117133

118134
def routes(self) -> Optional[List[str]]:
119135
plugin_name = self.bundle_info.get("plugin_name")

tests/unit/bundle_analysis/test_bundle_comparison.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -324,22 +324,33 @@ def test_bundle_asset_comparison_using_closest_size_delta():
324324
].contributing_modules()
325325
assert [module.name for module in module_reports] == []
326326

327-
# Check asset contributing modules is correct when pr_files are used for filtering
327+
# Check no contributing modules filter
328+
module_reports = asset_comparison_d[
329+
("assets/index-666d2e09.js", "assets/index-666d2e09.js")
330+
].contributing_modules()
331+
assert len(module_reports) == 28
332+
333+
# Check no PR changed files
334+
module_reports = asset_comparison_d[
335+
("assets/index-666d2e09.js", "assets/index-666d2e09.js")
336+
].contributing_modules([])
337+
assert len(module_reports) == 0
338+
339+
# Check with proper filtered files
328340
module_reports = asset_comparison_d[
329341
("assets/index-666d2e09.js", "assets/index-666d2e09.js")
330342
].contributing_modules(
331-
pr_changed_files=[
332-
"src/index.css",
333-
"src/main.tsx",
334-
"./index.html",
335-
"/src/App.css",
336-
] # first 3 is in, 4th is not
343+
[
344+
"app1/index.html",
345+
"app2/index.html", # <- don't match because dupe
346+
"./app1/src/main.tsx",
347+
"/example/svelte/app1/src/App.css",
348+
"abc/def/ghi.ts", # <- don't match
349+
]
350+
)
351+
assert set([module.name for module in module_reports]) == set(
352+
["./index.html", "./src/App.css", "./src/main.tsx"]
337353
)
338-
assert [module.name for module in module_reports] == [
339-
"./src/index.css",
340-
"./src/main.tsx",
341-
"./index.html",
342-
]
343354

344355

345356
def test_bundle_asset_comparison_using_uuid():

0 commit comments

Comments
 (0)