Skip to content

Commit b8c983e

Browse files
committed
Adding a way to return ALL CSS and JS files - i.e. disable returned file tracking
1 parent 5c0f659 commit b8c983e

File tree

6 files changed

+119
-4
lines changed

6 files changed

+119
-4
lines changed

src/Asset/EntrypointLookup.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class EntrypointLookup implements EntrypointLookupInterface, IntegrityDataProvid
3333

3434
private $strictMode;
3535

36+
private $trackReturnedFiles = true;
37+
3638
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null, bool $strictMode = true)
3739
{
3840
$this->entrypointJsonPath = $entrypointJsonPath;
@@ -70,6 +72,18 @@ public function reset()
7072
$this->returnedFiles = [];
7173
}
7274

75+
/**
76+
* Can be used to disable file tracking.
77+
*
78+
* When file tracking is disabled, *all* CSS and JS files will be
79+
* returned from getJavaScriptFiles() and getCssFiles() including
80+
* those that were previously returned.
81+
*/
82+
public function enableReturnedFileTracking(bool $shouldTrackReturnedFiles)
83+
{
84+
$this->trackReturnedFiles = $shouldTrackReturnedFiles;
85+
}
86+
7387
private function getEntryFiles(string $entryName, string $key): array
7488
{
7589
$this->validateEntryName($entryName);
@@ -81,8 +95,14 @@ private function getEntryFiles(string $entryName, string $key): array
8195
return [];
8296
}
8397

84-
// make sure to not return the same file multiple times
8598
$entryFiles = $entryData[$key];
99+
100+
// if tracking is disabled, return everything & do not mutate returnedFiles list
101+
if (!$this->trackReturnedFiles) {
102+
return $entryFiles;
103+
}
104+
105+
// make sure to not return the same file multiple times
86106
$newFiles = array_values(array_diff($entryFiles, $this->returnedFiles));
87107
$this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
88108

src/Twig/EntryFilesTwigExtension.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Symfony\WebpackEncoreBundle\Twig;
1111

1212
use Psr\Container\ContainerInterface;
13+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
1314
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
1415
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
1516
use Twig\Extension\AbstractExtension;
@@ -31,6 +32,8 @@ public function getFunctions()
3132
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
3233
new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
3334
new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
35+
new TwigFunction('encore_disable_file_tracking', [$this, 'disableReturnedFileTracking']),
36+
new TwigFunction('encore_enable_file_tracking', [$this, 'enableReturnedFileTracking']),
3437
];
3538
}
3639

@@ -58,6 +61,27 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
5861
->renderWebpackLinkTags($entryName, $packageName, $entrypointName);
5962
}
6063

64+
public function disableReturnedFileTracking(string $entrypointName = '_default')
65+
{
66+
$this->changeReturnedFileTracking(false, $entrypointName);
67+
}
68+
69+
public function enableReturnedFileTracking(string $entrypointName = '_default')
70+
{
71+
$this->changeReturnedFileTracking(true, $entrypointName);
72+
}
73+
74+
private function changeReturnedFileTracking(bool $isEnabled, string $entrypointName)
75+
{
76+
$lookup = $this->getEntrypointLookup($entrypointName);
77+
78+
if (!$lookup instanceof EntrypointLookup) {
79+
throw new \LogicException('In order to use encore_disable_returned_file_tracking/encore_enable_returned_file_tracking, the EntrypointLookupInterface must be an instance of EntrypointLookup.');
80+
}
81+
82+
$lookup->enableReturnedFileTracking($isEnabled);
83+
}
84+
6185
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
6286
{
6387
return $this->container->get('webpack_encore.entrypoint_lookup_collection')

tests/Asset/EntrypointLookupTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ public function testGetJavaScriptFilesReturnsUniqueFilesOnly()
8787
);
8888
}
8989

90+
public function testGetJavaScriptFilesWithFileTrackingDisabled()
91+
{
92+
// with file tracking on, grab a few files
93+
$this->entrypointLookup->getJavaScriptFiles('my_entry');
94+
95+
$this->entrypointLookup->enableReturnedFileTracking(false);
96+
$this->assertEquals(
97+
['file1.js', 'file3.js'],
98+
$this->entrypointLookup->getJavaScriptFiles('other_entry'),
99+
'file1.js is returned even though it was also returned above',
100+
);
101+
102+
$this->assertEquals(
103+
// file1.js is returned even though it was also returned above
104+
['file1.js', 'file3.js'],
105+
$this->entrypointLookup->getJavaScriptFiles('other_entry'),
106+
'repeat calls always return all the files'
107+
);
108+
109+
$this->entrypointLookup->enableReturnedFileTracking(true);
110+
$this->assertEquals(
111+
['file3.js'],
112+
$this->entrypointLookup->getJavaScriptFiles('other_entry'),
113+
'file1.js is not returned once tracking is re-enabled'
114+
);
115+
}
116+
90117
public function testGetCssFiles()
91118
{
92119
$this->assertEquals(

tests/EventListener/ExceptionListenerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Symfony\WebpackEncoreBundle\Tests\EventListener;
1111

1212
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1314
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1415
use Symfony\Component\HttpKernel\HttpKernelInterface;
1516
use PHPUnit\Framework\TestCase;
@@ -39,7 +40,8 @@ public function testItResetsAllEntrypointLookups()
3940

4041
$request = new Request();
4142
$exception = new \Exception();
42-
$event = new GetResponseForExceptionEvent(
43+
$exceptionClass = class_exists(ExceptionEvent::class) ? ExceptionEvent::class : GetResponseForExceptionEvent::class;
44+
$event = new $exceptionClass(
4345
$this->createMock(HttpKernelInterface::class),
4446
$request,
4547
HttpKernelInterface::MASTER_REQUEST,

tests/IntegrationTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public function testTwigIntegration()
6969
);
7070
}
7171

72+
public function testTwigIntegrationWithTrackingDisabled()
73+
{
74+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
75+
$kernel->boot();
76+
$container = $kernel->getContainer();
77+
78+
$html = $container->get('twig')->render('@integration_test/template_disable_tracking.twig');
79+
80+
$this->assertStringContainsStringCount(
81+
'/build/file1.js',
82+
$html,
83+
// 1 time for my_entry, again for other_entry
84+
2
85+
);
86+
}
87+
7288
public function testEntriesAreNotRepeatedWhenAlreadyOutputIntegration()
7389
{
7490
$kernel = new WebpackEncoreIntegrationTestKernel(true);
@@ -176,6 +192,23 @@ public function testAutowireDefaultBuildArgument()
176192
// Testing that it doesn't throw an exception is enough
177193
$this->assertTrue(true);
178194
}
195+
196+
private function assertStringContainsStringCount(string $needle, $haystack, int $expectedCount)
197+
{
198+
$actualCount = substr_count($haystack, $needle);
199+
200+
$this->assertSame(
201+
$expectedCount,
202+
$actualCount,
203+
sprintf(
204+
'Expected to find string "%s" in haystack "%s" "%d" times but only found it "%d" times',
205+
$needle,
206+
$haystack,
207+
$expectedCount,
208+
$actualCount
209+
)
210+
);
211+
}
179212
}
180213

181214
abstract class AbstractWebpackEncoreIntegrationTestKernel extends Kernel
@@ -208,6 +241,9 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
208241
{
209242
$container->loadFromExtension('framework', [
210243
'secret' => 'foo',
244+
'router' => [
245+
'utf8' => true,
246+
],
211247
'assets' => [
212248
'enabled' => $this->enableAssets,
213249
],
@@ -264,9 +300,9 @@ public function renderFoo()
264300
}
265301
}
266302

267-
if (method_exists(AbstractWebpackEncoreIntegrationTestKernel::class, 'configureRouting')) {
303+
if (Kernel::VERSION_ID >= 50100) {
268304
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
269-
protected function configureRouting(RoutingConfigurator $routes): void
305+
protected function configureRoutes(RoutingConfigurator $routes): void
270306
{
271307
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
272308
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% do encore_disable_file_tracking() %}
2+
{{ encore_entry_script_tags('my_entry') }}
3+
{{ encore_entry_link_tags('my_entry') }}
4+
{{ encore_entry_script_tags('other_entry') }}
5+
{{ encore_entry_link_tags('other_entry') }}
6+
{% do encore_enable_file_tracking() %}

0 commit comments

Comments
 (0)