Skip to content

Commit 768a820

Browse files
committed
Allow value for crossorigin attribute to be configurable, defaults to anonymous
1 parent 9f1e101 commit 768a820

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

src/Asset/TagRenderer.php

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

1919
private $packages;
2020

21+
private $crossorigin;
22+
2123
public function __construct(
2224
$entrypointLookupCollection,
23-
Packages $packages
25+
Packages $packages,
26+
string $crossorigin
2427
) {
2528
if ($entrypointLookupCollection instanceof EntrypointLookupInterface) {
2629
@trigger_error(sprintf('The "$entrypointLookupCollection" argument in method "%s()" must be an instance of EntrypointLookupCollection.', __METHOD__), E_USER_DEPRECATED);
@@ -37,6 +40,7 @@ public function __construct(
3740
}
3841

3942
$this->packages = $packages;
43+
$this->crossorigin = $crossorigin;
4044
}
4145

4246
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
@@ -52,7 +56,7 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
5256

5357
if (isset($integrityHashes[$filename])) {
5458
$attributes['integrity'] = $integrityHashes[$filename];
55-
$attributes['crossorigin'] = 'anonymous';
59+
$attributes['crossorigin'] = $this->crossorigin;
5660
}
5761

5862
$scriptTags[] = sprintf(
@@ -78,7 +82,7 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
7882

7983
if (isset($integrityHashes[$filename])) {
8084
$attributes['integrity'] = $integrityHashes[$filename];
81-
$attributes['crossorigin'] = 'anonymous';
85+
$attributes['crossorigin'] = $this->crossorigin;
8286
}
8387

8488
$scriptTags[] = sprintf(

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function getConfigTreeBuilder()
3434
->isRequired()
3535
->info('The path where Encore is building the assets - i.e. Encore.setOutputPath()')
3636
->end()
37+
->scalarNode('crossorigin')
38+
->info('crossorigin value when Encore.enableIntegrityHashes is used, can be anonymous (default) or use-credentials')
39+
->end()
3740
->booleanNode('cache')
3841
->info('Enable caching of the entry point file(s)')
3942
->defaultFalse()

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function load(array $configs, ContainerBuilder $container)
5151
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
5252
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
5353
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
54+
55+
$container->getDefinition('webpack_encore.tag_renderer')
56+
->replaceArgument(2, $config['crossorigin']??'anonymous');
5457
}
5558

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

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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 type="string" id="webpack_encore.crossorigin" />
1920
</service>
2021

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

tests/Asset/TagRendererTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testRenderScriptTags()
3333
->willReturnCallback(function($path) {
3434
return 'http://localhost:8080'.$path;
3535
});
36-
$renderer = new TagRenderer($entrypointCollection, $packages);
36+
$renderer = new TagRenderer($entrypointCollection, $packages, 'anonymous');
3737

3838
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
3939
$this->assertContains(
@@ -64,7 +64,7 @@ public function testRenderScriptTagsWithBadFilename()
6464
->willReturnCallback(function($path) {
6565
return 'http://localhost:8080'.$path;
6666
});
67-
$renderer = new TagRenderer($entrypointCollection, $packages);
67+
$renderer = new TagRenderer($entrypointCollection, $packages, 'anonymous');
6868

6969
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
7070
$this->assertContains(
@@ -110,7 +110,7 @@ public function testRenderScriptTagsWithinAnEntryPointCollection()
110110
->willReturnCallback(function($path) {
111111
return 'http://localhost:8080'.$path;
112112
});
113-
$renderer = new TagRenderer($entrypointCollection, $packages);
113+
$renderer = new TagRenderer($entrypointCollection, $packages, 'anonymous');
114114

115115
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
116116
$this->assertContains(
@@ -160,7 +160,7 @@ public function testRenderScriptTagsWithHashes()
160160
->willReturnCallback(function ($path) {
161161
return 'http://localhost:8080' . $path;
162162
});
163-
$renderer = new TagRenderer($entrypointCollection, $packages, true);
163+
$renderer = new TagRenderer($entrypointCollection, $packages, 'anonymous');
164164

165165
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
166166
$this->assertContains(

0 commit comments

Comments
 (0)