Skip to content

Commit 81db0d5

Browse files
committed
feature #54 Added strict mode #43 (karser)
This PR was merged into the master branch. Discussion ---------- Added strict mode #43 This PR introduces `strict_mode` which is true by default so it doesn't change the original bundle behavior. If set to false it suppresses all the exceptions thrown because of missing builds. It makes sense to disable strict mode for test environment where you don't need to build your assets. ``` # config/packages/test/webpack_encore.yaml webpack_encore: strict_mode: false ``` Here is the original issue #43 Commits ------- a74a5a2 Added strict mode
2 parents 2384cfd + a74a5a2 commit 81db0d5

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

src/Asset/EntrypointLookup.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ class EntrypointLookup implements EntrypointLookupInterface, IntegrityDataProvid
2929

3030
private $cache;
3131

32-
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null)
32+
private $strictMode;
33+
34+
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null, bool $strictMode = true)
3335
{
3436
$this->entrypointJsonPath = $entrypointJsonPath;
3537
$this->cache = $cache;
3638
$this->cacheKey = $cacheKey;
39+
$this->strictMode = $strictMode;
3740
}
3841

3942
public function getJavaScriptFiles(string $entryName): array
@@ -69,7 +72,7 @@ private function getEntryFiles(string $entryName, string $key): array
6972
{
7073
$this->validateEntryName($entryName);
7174
$entriesData = $this->getEntriesData();
72-
$entryData = $entriesData['entrypoints'][$entryName];
75+
$entryData = $entriesData['entrypoints'][$entryName] ?? [];
7376

7477
if (!isset($entryData[$key])) {
7578
// If we don't find the file type then just send back nothing.
@@ -87,7 +90,7 @@ private function getEntryFiles(string $entryName, string $key): array
8790
private function validateEntryName(string $entryName)
8891
{
8992
$entriesData = $this->getEntriesData();
90-
if (!isset($entriesData['entrypoints'][$entryName])) {
93+
if (!isset($entriesData['entrypoints'][$entryName]) && $this->strictMode) {
9194
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));
9295

9396
if (isset($entriesData['entrypoints'][$withoutExtension])) {
@@ -113,6 +116,9 @@ private function getEntriesData(): array
113116
}
114117

115118
if (!file_exists($this->entrypointJsonPath)) {
119+
if (!$this->strictMode) {
120+
return [];
121+
}
116122
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
117123
}
118124

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function getConfigTreeBuilder()
4343
->info('Enable caching of the entry point file(s)')
4444
->defaultFalse()
4545
->end()
46+
->booleanNode('strict_mode')
47+
->info('Throw an exception if the entrypoints.json file is missing or an entry is missing from the data')
48+
->defaultTrue()
49+
->end()
4650
->arrayNode('builds')
4751
->useAttributeAsKey('name')
4852
->normalizeKeys(false)

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public function load(array $configs, ContainerBuilder $container)
3636
$cacheKeys = [];
3737

3838
if (false !== $config['output_path']) {
39-
$factories['_default'] = $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache']);
39+
$factories['_default'] = $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache'], $config['strict_mode']);
4040
$cacheKeys['_default'] = $config['output_path'].'/'.self::ENTRYPOINTS_FILE_NAME;
4141
}
4242

4343
foreach ($config['builds'] as $name => $path) {
44-
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache']);
44+
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache'], $config['strict_mode']);
4545
$cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME;
4646
}
4747

@@ -64,10 +64,15 @@ public function load(array $configs, ContainerBuilder $container)
6464
->replaceArgument(2, $defaultAttributes);
6565
}
6666

67-
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference
67+
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled, bool $strictMode): Reference
6868
{
6969
$id = $this->getEntrypointServiceId($name);
70-
$arguments = [$path.'/'.self::ENTRYPOINTS_FILE_NAME, $cacheEnabled ? new Reference('webpack_encore.cache') : null, $name];
70+
$arguments = [
71+
$path.'/'.self::ENTRYPOINTS_FILE_NAME,
72+
$cacheEnabled ? new Reference('webpack_encore.cache') : null,
73+
$name,
74+
$strictMode,
75+
];
7176
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));
7277

7378
return new Reference($id);

tests/IntegrationTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ public function testCacheWarmer()
106106
$this->assertEquals(['_default' => 0, 'different_build' => 1], $data[0]);
107107
}
108108

109+
/**
110+
* @expectedException \Twig\Error\RuntimeError
111+
* @expectedExceptionMessageRegExp /Could not find the entrypoints file/
112+
*/
113+
public function testEnabledStrictMode_throwsException_ifBuildMissing()
114+
{
115+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
116+
$kernel->outputPath = 'missing_build';
117+
$kernel->builds = ['different_build' => 'missing_build'];
118+
$kernel->boot();
119+
$container = $kernel->getContainer();
120+
$container->get('twig')->render('@integration_test/template.twig');
121+
}
122+
123+
public function testDisabledStrictMode_ignoresMissingBuild()
124+
{
125+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
126+
$kernel->outputPath = 'missing_build';
127+
$kernel->strictMode = false;
128+
$kernel->builds = ['different_build' => 'missing_build'];
129+
$kernel->boot();
130+
$container = $kernel->getContainer();
131+
$html = $container->get('twig')->render('@integration_test/template.twig');
132+
self::assertSame('', trim($html));
133+
}
134+
109135
public function testAutowireableInterfaces()
110136
{
111137
$kernel = new WebpackEncoreIntegrationTestKernel(true);
@@ -118,6 +144,11 @@ public function testAutowireableInterfaces()
118144
class WebpackEncoreIntegrationTestKernel extends Kernel
119145
{
120146
private $enableAssets;
147+
public $strictMode = true;
148+
public $outputPath = __DIR__.'/fixtures/build';
149+
public $builds = [
150+
'different_build' => __DIR__.'/fixtures/different_build'
151+
];
121152

122153
public function __construct($enableAssets)
123154
{
@@ -152,12 +183,11 @@ public function registerContainerConfiguration(LoaderInterface $loader)
152183
]);
153184

154185
$container->loadFromExtension('webpack_encore', [
155-
'output_path' => __DIR__.'/fixtures/build',
186+
'output_path' => $this->outputPath,
156187
'cache' => true,
157188
'crossorigin' => false,
158-
'builds' => [
159-
'different_build' => __DIR__.'/fixtures/different_build',
160-
],
189+
'builds' => $this->builds,
190+
'strict_mode' => $this->strictMode,
161191
]);
162192

163193
$container->register(WebpackEncoreCacheWarmerTester::class)

0 commit comments

Comments
 (0)