Skip to content

AP114 Bundle size optimisations#65

Merged
davecoates merged 15 commits intomainfrom
feature/ap114-bundle-size-optimisations
Mar 18, 2026
Merged

AP114 Bundle size optimisations#65
davecoates merged 15 commits intomainfrom
feature/ap114-bundle-size-optimisations

Conversation

@davecoates
Copy link
Copy Markdown
Contributor

@davecoates davecoates commented Dec 8, 2024

AP114: Bundle Size Optimisations

This PR introduces significant improvements to how frontend resources are managed, providing greater flexibility
in bundling. With these changes, the default bundle size in the template size is ~75% smaller.

Key Changes:

  1. FrontendResource: A New Abstraction for Resources
    • replaces the previous approach of defining everything as simple paths.
    • Allows more detailed representation of resources, such as ES Modules with specific used exports. This enables the bundler to produce smaller builds.
    • Simplifies filtering of resources based on the resource class, as opposed to guessing the type based on file extensions.
  2. Rename and Update Resource Registry
    • FrontendAssetRegistry is now FrontendResourceRegistry.
    • Enhanced to support adding both FrontendResource instances and paths (paths are still supported for convenience).
  3. New Resource Extraction Method
    • Introduces extract_frontend_resources, which exports used resources in a new, detailed format. This format includes all the necessary metadata for the bundler to leverage additional information (initially just the ES module usage data).
  4. Deprecation of extract_frontend_assets
    • While still functional, extract_frontend_assets is now deprecated in favour of extract_frontend_resources.

I've tested the package in an existing project with no changes to use new interfaces, and it will work but with deprecation warnings.

I will be introducing another MR to the template, and possibly a new @alliancesoftware/ JS package, to handle the build side of this (i.e. the Vite plugin that makes use of the output of extract_frontend_resources). I'll also add a doc page for upgrading once that's done.

@davecoates davecoates requested a review from micahklaz December 8, 2024 06:10
@davecoates davecoates force-pushed the feature/ap114-bundle-size-optimisations branch from 81c1841 to 5a80d26 Compare December 8, 2024 09:20
@davecoates davecoates changed the base branch from main to sys/sphinx-upgrades December 8, 2024 09:20
@davecoates davecoates changed the base branch from sys/sphinx-upgrades to main December 9, 2024 03:49
Copy link
Copy Markdown
Collaborator

@micahklaz micahklaz left a comment

Choose a reason for hiding this comment

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

A few typos and doc fixes to address

Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/context.py Outdated
Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/base.py Outdated
Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/context.py Outdated
Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/vite.py
Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/vite.py Outdated
Comment thread packages/ap-frontend/alliance_platform/frontend/bundler/vite.py
Comment thread .changeset/rich-melons-cheer.md Outdated
@davecoates davecoates requested a review from micahklaz December 9, 2024 21:51
- Introduce `FrontendResource` to represent a resource that will be bundled. This is
  a change from the previous method of defining everything as just a path. This allows
  more flexibility in how resources are handled. For example, we can now represent usage
  of an ES Module as a path and the used exports from that module, which enables the
  bundler to produce a smaller build. It also means we can more easily filter resources
  based on the resource class, whereas before the only thing you can filter on was the
  path (e.g. guess the type from the extension).
- Rename FrontendAssetRegistry to FrontendResourceRegistry and change it to support adding
  resources, not just paths (paths are still supported for convenience).
- Intrduce extract_frontend_resources that will export the used resources in a new format
  containing all the details necessary for the bundler to make use of the extra info.
- extract_frontend_assets still works, but is deprecated
Fix cases where an asset may be treated as undiscoverable incorrectly. This occurs when
using a component as a var, e.g. {% component "package" "MyComponent" as my_var %}{% endcomponent %}
and then passing that as a prop to another component. When the prop is processed in the
component it is passed to it would previously throw an error about it being undiscoverable.
This fix accumulates the known resources as we go, so if the prop is encountered after the
node has been processed it is no longer considered unknown.

This would not have previously caused issues if the prop passed used an import path that was
used in the component (e.g. @alliancesoftware/ui components). With the new changes, this
would be much more common as using a new import from that package would be treated as a
unique usage.
Allow a full JavascriptResource to cover off on specific imports from that resource

This makes it so adding a path to a file is sufficient to cover off on any imports
used from that file.
Avoid importing from barrel file due to bundler (Vite) performance issues
in some situations
@davecoates davecoates force-pushed the feature/ap114-bundle-size-optimisations branch from 930c2fc to 15101dc Compare March 17, 2026 02:37
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 17, 2026

Codecov Report

❌ Patch coverage is 79.03226% with 78 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.41%. Comparing base (0a3b5df) to head (53feba6).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
...nce_platform/frontend/bundler/resource_registry.py 52.27% 21 Missing ⚠️
...rontend/alliance_platform/frontend/bundler/vite.py 71.42% 14 Missing ⚠️
...nce_platform/frontend/bundler/frontend_resource.py 82.53% 11 Missing ⚠️
...iance_platform/frontend/bundler/vanilla_extract.py 75.00% 7 Missing ⚠️
...rontend/alliance_platform/frontend/bundler/base.py 73.68% 5 Missing ⚠️
...rm/ui/templatetags/alliance_platform/pagination.py 16.66% 5 Missing ⚠️
...liance_platform/frontend/bundler/asset_registry.py 0.00% 4 Missing ⚠️
...frontend/alliance_platform/frontend/bundler/ssr.py 90.69% 4 Missing ⚠️
...ap-frontend/alliance_platform/frontend/settings.py 82.60% 4 Missing ⚠️
...tend/alliance_platform/frontend/bundler/context.py 93.75% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #65      +/-   ##
==========================================
+ Coverage   81.47%   82.41%   +0.93%     
==========================================
  Files          94       95       +1     
  Lines        6760     6870     +110     
==========================================
+ Hits         5508     5662     +154     
+ Misses       1252     1208      -44     
Flag Coverage Δ
ap-audit 78.46% <ø> (ø)
ap-codegen 79.69% <ø> (ø)
ap-core 68.38% <ø> (ø)
ap-frontend 81.77% <79.60%> (+2.16%) ⬆️
ap-ordered-model 94.46% <ø> (ø)
ap-pdf 81.69% <ø> (ø)
ap-server-choices 89.11% <ø> (ø)
ap-storage 89.52% <ø> (ø)
ap-ui 77.85% <64.28%> (+2.53%) ⬆️
django42 82.02% <79.03%> (+0.94%) ⬆️
django52 82.41% <79.03%> (+0.93%) ⬆️
py3.11 82.41% <79.03%> (+0.93%) ⬆️
py3.12 82.41% <79.03%> (+0.93%) ⬆️
py3.13 82.41% <79.03%> (+0.93%) ⬆️
py3.14 82.11% <78.33%> (+0.94%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This check has a noticeable performance impact and so is not practical to run all the time,
but can be enabled in CI for example
@davecoates davecoates merged commit caca03b into main Mar 18, 2026
74 of 75 checks passed
@davecoates davecoates deleted the feature/ap114-bundle-size-optimisations branch March 18, 2026 10:35
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