symfony / webpack-encore-bundle

Symfony integration with Webpack Encore!
https://symfony.com/webpack-encore
MIT License
937 stars 83 forks source link

Use this bundle in Drupal #170

Closed rpayanm closed 2 years ago

rpayanm commented 2 years ago

I would like to use this bundle in Drupal, is any way to do it?

I create a module and in my *.services.yml file, inside I have:

services:
  webpack_encore.twig_entry_files_extension:
    class: Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension
    arguments:
      ['@service_container']
    tags:
      - { name: twig.extension }
  webpack_encore.tag_renderer:
    class: Symfony\WebpackEncoreBundle\Asset\TagRenderer
    tags:
      - { name: kernel.reset}
    arguments:
      ['@webpack_encore.entrypoint_lookup_collection', '@assets.packages']
  assets.packages:
    class: Symfony\Component\Asset\Packages
  webpack_encore.entrypoint_lookup_collection:
    class: Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection
    arguments:
      ['@service_container']

I installed this module, but I got this error:

The website encountered an unexpected error. Please try again later.

Symfony\WebpackEncoreBundle\Exception\UndefinedBuildException: The build "_default" is not configured in Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection->getEntrypointLookup() (line 45 of /app/vendor/symfony/webpack-encore-bundle/src/Asset/EntrypointLookupCollection.php).

Symfony\WebpackEncoreBundle\Asset\TagRenderer->getEntrypointLookup('_default') (Line: 104)
Symfony\WebpackEncoreBundle\Asset\TagRenderer->renderWebpackLinkTags('app', NULL, '_default', Array) (Line: 60)
Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension->renderWebpackLinkTags('app') (Line: 69)
__TwigTemplate_103a4471cd8923672129d300fc53d0caeda98f2d872b07fbb171e95a19619a10->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('themes/custom/mytheme/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 422)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array) (Line: 162)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 163)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
weaverryan commented 2 years ago

Hi there!

This, is obviously a bit tricky to get wired up :). The problematic part is:

  webpack_encore.entrypoint_lookup_collection:
    class: Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection
    arguments:
      ['@service_container']

The 1st argument - ContainerInterface $buildEntrypoints - isn't referring to the "main" service container, but to a "mini" container locator that contains your "EntrypointLookup services". https://symfony.com/doc/current/service_container/service_subscribers_locators.html#defining-a-service-locator

I believe you would need something like this:

  webpack_encore.default_entrypoint:
    class: Symfony\WebpackEncoreBundle\Asset\EntrypointLookup
    arguments:
      - 'path/to/entrypoints.json'

  webpack_encore.entrypoint_lookup_collection:
    class: Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection
    arguments:
      - !service_locator
        _default: '@webpack_encore.default_entrypoint'

Good luck!

rpayanm commented 2 years ago

Hi, @weaverryan thank you! But I got this new error:

In YamlSymfony.php line 40:

  Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "!service_locator" at line 27 (near "!service_locator").  

In Parser.php line 1157:

  Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "!service_locator" at line 27 (near "!service_locator").  
weaverryan commented 2 years ago

Hmm. I'm not sure if there is any other way to define this in YAML. You could try to create (I can't remember what it's called in Drupal) a PHP config where you define these services (instead of YAML), but it still tricky. This is, unfortunately, a spot where Drupal and Symfony differ a bit.

My best advice would be to "steal" from this bundle. Parsing the entrypoints.json is actually pretty simple. We've added a lot of "bells and whistles", but at the end of the day, we're effectively just parsing that JSON file and using it with the encore_entry...() functions.

Cheers!

rpayanm commented 2 years ago

ok @weaverryan I will do that, thank you for your support!