Skip to content

Commit 22c971c

Browse files
committed
Allow for no attribute by default, and allow it to be configurable and future proofed array
1 parent d1866c5 commit 22c971c

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ webpack_encore:
2525
# If multiple builds are defined (as shown below), you can disable the default build:
2626
# output_path: false
2727

28-
# if using Encore.enableIntegrityHashes() specify the crossorigin attribute value (default: anonymous)
29-
# crossorigin: 'use-credentials'
28+
# if using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
29+
# crossorigin: 'anonymous'
3030

3131
# if you have multiple builds:
3232
# builds:

src/Asset/TagRenderer.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ final class TagRenderer
1818

1919
private $packages;
2020

21-
private $crossorigin;
21+
private $defaultAttributes;
2222

2323
public function __construct(
2424
$entrypointLookupCollection,
2525
Packages $packages,
26-
string $crossorigin
26+
array $defaultAttributes
2727
) {
2828
if ($entrypointLookupCollection instanceof EntrypointLookupInterface) {
2929
@trigger_error(sprintf('The "$entrypointLookupCollection" argument in method "%s()" must be an instance of EntrypointLookupCollection.', __METHOD__), E_USER_DEPRECATED);
@@ -40,7 +40,7 @@ public function __construct(
4040
}
4141

4242
$this->packages = $packages;
43-
$this->crossorigin = $crossorigin;
43+
$this->defaultAttributes = $defaultAttributes;
4444
}
4545

4646
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
@@ -56,7 +56,10 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
5656

5757
if (isset($integrityHashes[$filename])) {
5858
$attributes['integrity'] = $integrityHashes[$filename];
59-
$attributes['crossorigin'] = $this->crossorigin;
59+
}
60+
61+
if (isset($this->defaultAttributes['crossorigin']) && false !== $this->defaultAttributes['crossorigin']) {
62+
$attributes['crossorigin'] = $this->defaultAttributes['crossorigin'];
6063
}
6164

6265
$scriptTags[] = sprintf(
@@ -82,7 +85,10 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
8285

8386
if (isset($integrityHashes[$filename])) {
8487
$attributes['integrity'] = $integrityHashes[$filename];
85-
$attributes['crossorigin'] = $this->crossorigin;
88+
}
89+
90+
if (isset($this->defaultAttributes['crossorigin']) && false !== $this->defaultAttributes['crossorigin']) {
91+
$attributes['crossorigin'] = $this->defaultAttributes['crossorigin'];
8692
}
8793

8894
$scriptTags[] = sprintf(

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public function getConfigTreeBuilder()
3535
->info('The path where Encore is building the assets - i.e. Encore.setOutputPath()')
3636
->end()
3737
->enumNode('crossorigin')
38-
->defaultValue('anonymous')
39-
->values(['anonymous', 'use-credentials'])
38+
->defaultFalse()
39+
->values([false, 'anonymous', 'use-credentials'])
4040
->info('crossorigin value when Encore.enableIntegrityHashes is used, can be anonymous (default) or use-credentials')
4141
->end()
4242
->booleanNode('cache')

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public function load(array $configs, ContainerBuilder $container)
5353
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
5454

5555
$container->getDefinition('webpack_encore.tag_renderer')
56-
->replaceArgument(2, $config['crossorigin']);
56+
->replaceArgument(2, [
57+
'crossorigin' => $config['crossorigin'],
58+
]);
5759
}
5860

5961
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference

src/Resources/config/services.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
1717
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
1818
<argument type="service" id="assets.packages" />
19-
<argument /><!-- crossorigin -->
19+
<argument type="collection">
20+
<argument key="crossorigin" />
21+
</argument>
2022
</service>
2123

2224
<service id="webpack_encore.twig_entry_files_extension" class="Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension">

0 commit comments

Comments
 (0)