Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle Analysis: add ability to ingest dynamic imports #445

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

JerrySentry
Copy link
Contributor

  • Add a DB migration to create a new table (dynamic_imports), this table is a fk mapping between chunks and assets to specify what dynamically imported assets a chunk has.
  • Add a new v3 JSON stats file parser that reads chunk.dynamicImport array of strings and stores this into the dynamic_imports table

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

@@ -0,0 +1,424 @@
import json
Copy link
Contributor Author

@JerrySentry JerrySentry Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire file is basically a carbon copy of v2, except what is identified below. Note that each time a new stats file json schema is updated we create a new parser version 95% of the code between each version would be the same.

https://www.diffchecker.com/aI6EWSfr/

"initial": bool,
"files": [str],
"names": [str],
"dynamicImports": [str]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only change to the schema, also the "version": "3"

# reset parser state
self.module = None
self.module_chunk_unique_external_ids = []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function below is a new one, it collects all the pairs of (chunk, asset) that is a dynamic import to be saved into the DB at the end.

self.db_session.execute(insert_modules)

self.db_session.flush()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is where we have the list of items to insert into the DB. Also a small above change

self.db_session.add_all(self.chunk_list)

compared to before of Chunk.__table__.insert().values

@JerrySentry JerrySentry marked this pull request as ready for review December 2, 2024 22:43
Copy link
Contributor

@suejung-sentry suejung-sentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't guarantee I fully tracked all the mappings across name, path, id, unique_id, unique_external_id to catch any bugs in there but looks like you've got some great test fixtures so looks good to me!

asset_id integer not null,
primary key (chunk_id, asset_id),
foreign key (chunk_id) references chunks (id),
foreign key (asset_id) references assets (id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we need any indexes on this?

create index idx_chunk_id on dynamic_imports (chunk_id);
create index idx_asset_id on dynamic_imports (asset_id);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The good thing about these "databases" are that it is super small (like 100kB), it only matches bundle data for 1 singular commit. This means we don't need to worry about performance on querying things haha.

self.module_list = []

# dynamic imports: mapping between Chunk and each file name of its dynamic imports
self.dynamic_imports_mapping = defaultdict(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on naming this dynamic_import_file_names_by_chunk or similar?

]
)
if inserts:
self.db_session.execute(chunks_modules.insert(), inserts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need any transaction safety/rollback on the assets_chunks.insert() in above and this chunks_modules.insert() if one succeeds but the next fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire parse should be already in a transaction, we don't actually call session.commit in here which writes everything into the filesystem, that happens on the caller of the parser.

self.module_chunk_unique_external_ids = []

def _parse_dynamic_imports(self) -> List[dict]:
dynamic_imports_list = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be nice to have a more specific type on this dynamic_imports_list and then we can omit the type from the var name. Can see that being a pain though

@JerrySentry JerrySentry added this pull request to the merge queue Dec 3, 2024
Merged via the queue into main with commit 45252f7 Dec 3, 2024
6 checks passed
@JerrySentry JerrySentry deleted the dec_02_dynamicimports branch December 3, 2024 01:22
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.02%. Comparing base (00e90bb) to head (a20d64d).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #445      +/-   ##
==========================================
- Coverage   90.69%   90.02%   -0.68%     
==========================================
  Files         394      324      -70     
  Lines       12002     9310    -2692     
  Branches     2018     1663     -355     
==========================================
- Hits        10885     8381    -2504     
+ Misses       1028      865     -163     
+ Partials       89       64      -25     
Flag Coverage Δ
shared-docker-uploader ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants