Skip to content

Conversation

azygis
Copy link
Contributor

@azygis azygis commented Aug 22, 2025

Description

As mentioned in #378, the full library size is astronomical, especially if you just have some small components to use to view some basic map data. This PR intends to solve the issue by making the library tree-shakable.

How Has This Been Tested?

I've been testing the changes using yalc (local package store for npm/yarn), results in screenshots. The bundle size drops substantially. Keep in mind I have a separate bundle for both openlayers and vue3-openlayers packages in there.

I have not run the tests yet as I'm still in progress of making it better, reexporting the categories for example so you don't need to flood your import section with a bunch of default imports. All in all, it seems to be working fine as is mostly backwards compatible, except capitalization issues for fixed component names (it's best to follow OL capitalization IMO).

Screenshots (if appropriate):

Today's result, importing the "namespaced" components, even if you only use a few of them:
image

After using the explicit, tree-shaken imports:
image

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Tests
  • Other (Tooling, Dependency Updates, etc.)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I added a new test.

If you added a new component feature (layer, geom, source, etc.), please be sure to update the documentation:

  • Add component to output.globals in vite.config.ts
  • Provide at least one simple snapshot test (see test directory)
  • Create a src/demos/<Component>Demo.vue
  • Create a docs/componentsguide/<Category>/<Feature>/index.md containing the Demo and documentation for the component
  • Add the docs page to docs/.vitepress/config.ts

azygis added 11 commits August 20, 2025 10:40
…lineDynamicImports

- Added sideEffects: false to package.json
- Removed inlineDynamicImports from vite.config.ts
- Achieved code splitting with main bundle reduced to ~0.7KB (ES) / ~1.2KB (CJS)
- Multiple chunks created for proper tree-shaking
…entry points

- Add tree-shaking entry points for 9 categories: map, layers, sources, controls, animations, geometries, interactions, styles, helpers
- Create vite.treeshaking.config.ts for multi-entry tree-shaking builds
- Update package.json with subpath exports for all categories
- Add build:treeshaking and build:all npm scripts

Bundle sizes achieved:
- helpers: 343B ES / 443B CJS
- controls: 701B ES / 1.3KB CJS
- geometries: 5.0KB ES / 5.2KB CJS
- animations: 6.2KB ES / 6.4KB CJS
- layers: 20.9KB ES / 21.5KB CJS
- interactions: 21.8KB ES / 22.7KB CJS
- sources: 26.1KB ES / 26.6KB CJS
- styles: 43.0KB ES / 43.6KB CJS
- map: 183.7KB ES / 184.1KB CJS

Enables selective imports like:
import { OlMap } from 'vue3-openlayers/map'
import { OlVectorLayer } from 'vue3-openlayers/layers'
- Split UMD build into separate vite.umd.config.ts to avoid esbuild errors
- Add emptyOutDir: false to all configs to prevent cleaning between builds
- Update build scripts to run in sequence: ES/CJS → UMD → tree-shaking
- All three build types now complete successfully without conflicts

Builds generated:
- vue3-openlayers.es.js (687B) - ES module
- vue3-openlayers.cjs.js (1.2KB) - CommonJS
- vue3-openlayers.umd.js (1.6MB) - UMD bundle
- Category tree-shaking bundles (343B-184KB each)

This resolves the previous esbuild syntax errors in UMD format.
- Add rollup-plugin-visualizer dependency for bundle analysis
- Create build:analyze scripts that set VITE_ANALYZE=true
- Conditionally include visualizer plugin when VITE_ANALYZE is set
- Generate separate stats files for main build and tree-shaking builds

Scripts added:
- npm run build:analyze - Analyze main ES/CJS build
- npm run build:analyze:treeshaking - Analyze tree-shaking builds
- npm run build:analyze:all - Analyze both main and tree-shaking builds

Outputs:
- dist/stats.html - Main build visualization
- dist/stats-treeshaking.html - Tree-shaking build visualization

Both files show interactive bundle size analysis with gzip/brotli compression stats.
…e-shaking

- Add providers-entry.ts for OpenLayers utilities and providers
- Add composables-entry.ts for Vue composables
- Update vite.treeshaking.config.ts with new entry points
- Update package.json exports with subpath exports for new categories

New tree-shaking bundles:
- providers: 788B ES / 1.9KB CJS - OpenLayers utilities, format, geom, etc.
- composables: 700B ES / 1.2KB CJS - Vue composables for layer, source, etc.

Complete Phase 2 categories (11 total):
✅ map, layers, sources, controls, animations, geometries, interactions, styles, helpers, providers, composables

This completes the comprehensive Phase 2 granular tree-shaking implementation.
Copy link

netlify bot commented Aug 22, 2025

Deploy Preview for vue3openlayers ready!

Name Link
🔨 Latest commit 983f278
🔍 Latest deploy log https://app.netlify.com/projects/vue3openlayers/deploys/68afffcd8d9c0b000842e99f
😎 Deploy Preview https://deploy-preview-434--vue3openlayers.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@d-koppenhagen
Copy link
Collaborator

Great job! I like it so far but since the refactoring introduces breaking changes, can you provide a detailled mirgation guide for the changelog?

@azygis
Copy link
Contributor Author

azygis commented Aug 22, 2025

Good thing is, almost nothing particularly breaking, other than changing the component names in markup where needed. Since I'm still in progress, I will leave the changelog for the last step. Also, I'm working strictly on manual imports since on our side we do not use plugin itself, someone else will need to verify (and perhaps adjust) tree shaking for that use case. The worst that can happen is really no tree shaking for plugin usage yet (and obviously differences in component names consumers will need to fix).

@azygis
Copy link
Contributor Author

azygis commented Aug 27, 2025

@d-koppenhagen looks like I'm done for now, would be great to review and perhaps release. Not sure if you want a preview version or not, seems to be working okay with my app, one migrated fully to a treeshaken library, one still using the old "namespace" imports. I'm not sure what's up with playwright here, but seems perfectly fine locally:

image

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