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

bundle from bud.bundle is loaded multiple times #403

Open
RafaelKr opened this issue Oct 2, 2024 · 0 comments
Open

bundle from bud.bundle is loaded multiple times #403

RafaelKr opened this issue Oct 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@RafaelKr
Copy link
Contributor

RafaelKr commented Oct 2, 2024

Version

4.3.0

What did you expect to happen?

We're using ACF Composer to build our blocks. We create a TypeScript file per block and register it as a bud entrypoint like so:

bud
  .entry(`block-team`, [`@scripts/blocks/team`])
  .entry(`block-slider`, [`@scripts/blocks/slider`])

And inside our Block we use an assets method like this:

/**
* Assets enqueued when rendering the block.
*/
public function assets(array $block): void
{
    bundle('block-team')->enqueue();
}

We have multiple blocks using the Swiper library which is imported like:

import Swiper from 'swiper';
import {Autoplay, Navigation, Pagination} from 'swiper/modules';
import 'swiper/css/bundle'

Right now every compiled JavaScript entrypoint file contains the full Swiper library code. We now wanted to use bud.bundle('swiper') to extract the library to an own bundle.

When we do this the generated entrypoint.json file results in

{
  "app": {
    "js": [
      "js/runtime.js",
      "js/app.js"
    ],
    "dependencies": []
  },
  [...]
  "block-team": {
    "js": [
      "js/runtime.js",
      "js/bundle/swiper.js",
      "js/block-team.js"
    ],
    "dependencies": []
  },
  "block-slider": {
    "js": [
      "js/runtime.js",
      "js/bundle/swiper.js",
      "js/block-slider.js"
    ],
    "dependencies": []
  }
}

We expect that every block assets and also the dependencies (js/bundle/swiper.js) are enqueued exactly once.

What actually happens?

What actually happens now is that the resulting HTML code contains multiple instances of the swiper bundle in the footer:

<script src="https://example.localhost:8443/dist/js/bundle/swiper.js"></script>
<script src="https://example.localhost:8443/dist/js/block-team.js">
<script src="https://example.localhost:8443/dist/js/bundle/swiper.js"></script>
<script src="https://example.localhost:8443/dist/js/block-slider.js">

Steps to reproduce

  1. Create multiple bud entrypoints
  2. Import an npm library (e.g. swiper) in all of them
  3. Use bud.bundle('swiper') to create an own bundle for the library
  4. Enqueue all entrypoints
  5. Now the swiper library bundle is loaded multiple times

System info

No response

Log output

No response

Please confirm this isn't a support request.

Yes

@RafaelKr RafaelKr added the bug Something isn't working label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant