Skip to content

Commit 0b607f7

Browse files
committed
refactor based on @stof feedback
1 parent 5eac7b3 commit 0b607f7

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

src/Asset/TagRenderer.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,14 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
5050
$integrityHashes = ($entryPointLookup instanceof IntegrityDataProviderInterface) ? $entryPointLookup->getIntegrityData() : [];
5151

5252
foreach ($entryPointLookup->getJavaScriptFiles($entryName) as $filename) {
53-
$attributes = [
54-
'src' => $this->getAssetPath($filename, $packageName),
55-
];
53+
54+
$attributes = $this->defaultAttributes;
55+
$attributes['src'] = $this->getAssetPath($filename, $packageName);
5656

5757
if (isset($integrityHashes[$filename])) {
5858
$attributes['integrity'] = $integrityHashes[$filename];
5959
}
6060

61-
if (isset($this->defaultAttributes['crossorigin']) && false !== $this->defaultAttributes['crossorigin']) {
62-
$attributes['crossorigin'] = $this->defaultAttributes['crossorigin'];
63-
}
64-
6561
$scriptTags[] = sprintf(
6662
'<script %s></script>',
6763
$this->convertArrayToAttributes($attributes)
@@ -78,19 +74,15 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
7874
$integrityHashes = ($entryPointLookup instanceof IntegrityDataProviderInterface) ? $entryPointLookup->getIntegrityData() : [];
7975

8076
foreach ($entryPointLookup->getCssFiles($entryName) as $filename) {
81-
$attributes = [
82-
'rel' => 'stylesheet',
83-
'href' => $this->getAssetPath($filename, $packageName),
84-
];
77+
78+
$attributes = $this->defaultAttributes;
79+
$attributes['rel'] = 'stylesheet';
80+
$attributes['href'] = $this->getAssetPath($filename, $packageName);
8581

8682
if (isset($integrityHashes[$filename])) {
8783
$attributes['integrity'] = $integrityHashes[$filename];
8884
}
8985

90-
if (isset($this->defaultAttributes['crossorigin']) && false !== $this->defaultAttributes['crossorigin']) {
91-
$attributes['crossorigin'] = $this->defaultAttributes['crossorigin'];
92-
}
93-
9486
$scriptTags[] = sprintf(
9587
'<link %s>',
9688
$this->convertArrayToAttributes($attributes)

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public function load(array $configs, ContainerBuilder $container)
5252
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
5353
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
5454

55+
$defaultAttributes = [];
56+
57+
if (false !== $config['crossorigin']){
58+
$defaultAttributes['crossorigin'] = $config['crossorigin'];
59+
}
60+
5561
$container->getDefinition('webpack_encore.tag_renderer')
56-
->replaceArgument(2, [
57-
'crossorigin' => $config['crossorigin'],
58-
]);
62+
->replaceArgument(2, $defaultAttributes);
5963
}
6064

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

tests/Asset/TagRendererTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public function testRenderScriptTags()
3737

3838
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
3939
$this->assertContains(
40-
'<script src="http://localhost:8080/build/file1.js" crossorigin="anonymous"></script>',
40+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file1.js"></script>',
4141
$output
4242
);
4343
$this->assertContains(
44-
'<script src="http://localhost:8080/build/file2.js" crossorigin="anonymous"></script>',
44+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file2.js"></script>',
4545
$output
4646
);
4747
}
@@ -68,7 +68,7 @@ public function testRenderScriptTagsWithBadFilename()
6868

6969
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
7070
$this->assertContains(
71-
'<script src="http://localhost:8080/build/file&lt;&quot;bad_chars.js" crossorigin="anonymous"></script>',
71+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file&lt;&quot;bad_chars.js"></script>',
7272
$output
7373
);
7474
}
@@ -114,17 +114,17 @@ public function testRenderScriptTagsWithinAnEntryPointCollection()
114114

115115
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
116116
$this->assertContains(
117-
'<script src="http://localhost:8080/build/file1.js" crossorigin="anonymous"></script>',
117+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file1.js"></script>',
118118
$output
119119
);
120120
$output = $renderer->renderWebpackScriptTags('my_entry', null, 'second');
121121
$this->assertContains(
122-
'<script src="http://localhost:8080/build/file2.js" crossorigin="anonymous"></script>',
122+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file2.js"></script>',
123123
$output
124124
);
125125
$output = $renderer->renderWebpackScriptTags('my_entry', 'specific_package', 'third');
126126
$this->assertContains(
127-
'<script src="http://localhost:8080/build/file3.js" crossorigin="anonymous"></script>',
127+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file3.js"></script>',
128128
$output
129129
);
130130
}
@@ -164,11 +164,11 @@ public function testRenderScriptTagsWithHashes()
164164

165165
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
166166
$this->assertContains(
167-
'<script src="http://localhost:8080/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc" crossorigin="anonymous"></script>',
167+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc"></script>',
168168
$output
169169
);
170170
$this->assertContains(
171-
'<script src="http://localhost:8080/build/file2.js" integrity="sha384-ymG7OyjISWrOpH9jsGvajKMDEOP/mKJq8bHC0XdjQA6P8sg2nu+2RLQxcNNwE/3J" crossorigin="anonymous"></script>',
171+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file2.js" integrity="sha384-ymG7OyjISWrOpH9jsGvajKMDEOP/mKJq8bHC0XdjQA6P8sg2nu+2RLQxcNNwE/3J"></script>',
172172
$output
173173
);
174174
}

0 commit comments

Comments
 (0)