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

Treeshaking and duplicate code in core/lightbox ESM modules #2040

Open
danielvy opened this issue May 24, 2023 · 2 comments
Open

Treeshaking and duplicate code in core/lightbox ESM modules #2040

danielvy opened this issue May 24, 2023 · 2 comments

Comments

@danielvy
Copy link

It appears that photoswipe.esm.js and photoswipe-lightbox.esm.js modules have a lot of the same code. So when I import them as suggested in the docs:

import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/style.css';

const lightbox = new PhotoSwipeLightbox({
  gallery: '#my-gallery',
  children: 'a',
  pswpModule: () => import('photoswipe')
});
lightbox.init();

This produces many of the same functions in the final javascript. Treeshaking (I'm using Rollup) is not possible as these are seen as different code paths.

Using static imports:

import PhotoSwipeLightbox from 'photoswipe/lightbox';
import PhotoSwipe from 'photoswipe'; 

Gives an even larger size of ~250k (unminified). This is also not treeshakable.

The documentation says:

Lightbox (photoswipe-lightbox.esm.js) - loads Core and chooses when PhotoSwipe should be opened. Its file size is significantly smaller. It also loads the first image (in parallel with Core).

So do I have to import core again and pass a separate PhotoSwap to the lightbox? Am I missing something?

@dimsemenov
Copy link
Owner

The Lightbox inherits only a small fraction of the Core. It's mainly used to start loading the first image in parallel with the Core JS.

The script is intentionally split into two parts, so you can preload a large part of it separately and/or on-demand when a user actually needs the viewer.

@saas786
Copy link

saas786 commented Feb 3, 2024

@dimsemenov

The Lightbox inherits only a small fraction of the Core. It's mainly used to start loading the first image in parallel with the Core JS.
The script is intentionally split into two parts, so you can preload a large part of it separately and/or on-demand when a user actually needs the viewer.

If there's any demo or documentation available, I'm interested in utilizing it.

Never mind, I didn't realize I was already handling it myself.

pswpModule: () => import('photoswipe')

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

No branches or pull requests

3 participants