symfony / webpack-encore-bundle

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

TagRenderer EntrypointLookupCollectionInterface , encore_entry_script_tag issue #223

Closed KIMKYEONGYONG closed 11 months ago

KIMKYEONGYONG commented 11 months ago

my webpack.js

    .addEntry("app","./resources/js/app.js")
    .addEntry("auth", "./resources/js/login.js")
    .addEntry("media", "./resources/js/media.js")

html

    {% block javascripts %}
    {{ parent() }}
    {{ encore_entry_script_tags('media')}}
{% endblock %}

media.js

$(function () {
    console.log('test media')
});

app.js

$(function () {
    console.log('test app')
});

container_bindings.php ( => yaml.xml)

 'webpack_encore.entrypoint_lookup_collection' => static function(): EntrypointLookupCollectionInterface  {
        $serviceLocator = new ServiceLocator(['_default' => function() {
            return new EntrypointLookup(DIST_PATH . '/entrypoints.json');
        }]);
        return new EntrypointLookupCollection($serviceLocator);
    },
    'webpack_encore.packages'               => static fn(): Packages => new Packages(
        new Package(new JsonManifestVersionStrategy(DIST_PATH . '/manifest.json'))
    ),
    'webpack_encore.tag_renderer' => static fn(ContainerInterface $container): TagRenderer => new TagRenderer(
        $container->get('webpack_encore.entrypoint_lookup_collection'),
        $container->get('webpack_encore.packages')
    ),

conosle reuslt test app 2 test media 2

why js is 2 time run??

weaverryan commented 11 months ago

I'm not sure :). Each file should only run 1 time. Perhaps you can throw an error in the file to see where each "call" is coming from.

KIMKYEONGYONG commented 11 months ago

html v1)

{% block javascripts %}

    {{ encore_entry_script_tags('media')}}
{% endblock %}

console result

test media ( 1 call)

html v2)

{% block javascripts %}
    {{ parent() }}

{% endblock %}

console result

test app ( 1 call)

html v3)

{% block javascripts %}
    {{ parent() }}
    {{ encore_entry_script_tags('media')}}
{% endblock %}

console result

app.js:29 test app.  
media.js:4 test media
app.js:29 test app
media.js:4 test media

why call console 2 time???

KIMKYEONGYONG commented 11 months ago

The cause I think is this: TagRenderer entrypointLookupCollection

(yaml.xml) 2 call yaml.xml

'webpack_encore.entrypoint_lookup_collection' => static function(): EntrypointLookupCollectionInterface  {
        $serviceLocator = new ServiceLocator(['_default' => function() {
            return new EntrypointLookup(DIST_PATH . '/entrypoints.json');
        }]);
        return new EntrypointLookupCollection($serviceLocator);
    },
    'webpack_encore.packages'               => static fn(): Packages => new Packages(
        new Package(new JsonManifestVersionStrategy(DIST_PATH . '/manifest.json'))
    ),
    'webpack_encore.tag_renderer' => static fn(ContainerInterface $container): TagRenderer => new TagRenderer(
        $container->get('webpack_encore.entrypoint_lookup_collection'),
        $container->get('webpack_encore.packages')
    ),

1 call yaml.xml

    'webpack_encore.packages'               => static fn() => new Packages(
        new Package(new JsonManifestVersionStrategy(DIST_PATH . '/manifest.json'))
    ),
    'webpack_encore.tag_renderer'           => static fn(ContainerInterface $container) => new TagRenderer(
        new EntrypointLookup(DIST_PATH . '/entrypoints.json'),
        $container->get('webpack_encore.packages')
    ),

but 1 call yaml.xml is trigger_error !!

KIMKYEONGYONG commented 11 months ago

before my code << encore_entry_script_tags * count is call ( new Instance )

'webpack_encore.entrypoint_lookup_collection' => static function(): EntrypointLookupCollectionInterface  {
        $serviceLocator = new ServiceLocator(['_default' => function() {
            return new EntrypointLookup(DIST_PATH . '/entrypoints.json');  
        }]);
        return new EntrypointLookupCollection($serviceLocator);
    },

after my code

  'webpack_encore.entrypoint_lookup_collection' => static function(): EntrypointLookupCollectionInterface  {
        $entrypointLookup = new EntrypointLookup(DIST_PATH . '/entrypoints.json');
        $serviceLocator =  new ServiceLocator(['_default' => function () use ($entrypointLookup) {
            return $entrypointLookup;
        }]);
        return new EntrypointLookupCollection($serviceLocator);
    },