Skip to content

Astro pre-evaluates dynamic imports that prevents tree-shaking #6328

@putnikproj

Description

@putnikproj

What version of astro are you using?

2.0.14

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

It doesn't matter for this bug

Describe the Bug

All the dynamic imports and import.meta.glob imports are bundled in my production build, regardless of whether they are used on the page. I could resolve all the modules by myself when needed, but it's impossible due to this bug. For a more detailed description of the bug and why I need this feature, see vitejs/vite#12104.

Here is another reproduction of the bug: https://stackblitz.com/edit/github-b1xdf7?file=src%2Fpages%2Findex.astro. I also think that this feature would help a lot of projects to significantly reduce the size of a bundle.

Astro's official documentation:

You can also use import.meta.glob() itself in your Astro project. You may want to do this when:

  • You don’t want to load each file immediately. import.meta.glob() can return functions that import the file content, rather than returning the content itself.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-b1xdf7-rajkhn?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.

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