Skip to content

Imports are not considered for page-based code splitting #1632

@Bastian

Description

@Bastian

Describe the bug
When importing a library on a single page, it's bundled in the vendor-<hash>.js file and loaded on every page.
This has not been the case with Sapper.

To Reproduce

I've created two example projects, one with Sapper and one with Svelte-Kit.
They can be found here:

On both projects, I've imported the moment.js library, as it has a large and noticeable bundle size (~20kB gzipped) but every other library would show the same result. The library is imported on the About page and nowhere else. I've recorded a video that shows the Chrome dev tools when loading the page and then navigating to the About page:

Here's how it looks like in Sapper where the automatic code-splitting is working:

sapper-codesplitting.mp4

And here's Svelte Kit where the complete bundle is loaded immediately:

sveltekit-codesplitting.mp4

You can clearly see that in Sapper the ~20kB gzipped bundle is only loaded when navigating to the About page whereas in Svelte-Kit it's loaded immediately (included in the vendor-<hash>.js file).

Expected behavior

Automatic code splitting should make sure that the library is only loaded on pages that import it.

Severity

Not severe but an inconvenience with a non-neglectable impact on performance.

Additional context

You can enforce code-splitting by using dynamic imports. While this is a good temporary solution to mitigate the impact of larger libraries, doing this for every single imported library would be a major inconvenience and result in a lot of unwanted boilerplate code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions