-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
WebpackEncoreExtension.php
99 lines (80 loc) · 4.08 KB
/
WebpackEncoreExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
* This file is part of the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <fabien@symfony.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\WebpackEncoreBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
final class WebpackEncoreExtension extends Extension
{
private const ENTRYPOINTS_FILE_NAME = 'entrypoints.json';
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config'));
$loader->load('services.xml');
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$factories = [];
$cacheKeys = [];
if (false !== $config['output_path']) {
$factories['_default'] = $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache'], $config['strict_mode']);
$cacheKeys['_default'] = $config['output_path'].'/'.self::ENTRYPOINTS_FILE_NAME;
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
->setArgument(1, '_default');
}
foreach ($config['builds'] as $name => $path) {
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache'], $config['strict_mode']);
$cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME;
}
$container->getDefinition('webpack_encore.entrypoint_lookup.cache_warmer')
->replaceArgument(0, $cacheKeys);
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
if (false !== $config['output_path']) {
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
}
$defaultAttributes = [];
if (false !== $config['crossorigin']) {
$defaultAttributes['crossorigin'] = $config['crossorigin'];
}
$container->getDefinition('webpack_encore.tag_renderer')
->replaceArgument(2, $defaultAttributes);
if ($config['preload']) {
if (!class_exists(AddLinkHeaderListener::class)) {
throw new \LogicException('To use the "preload" option, the WebLink component must be installed. Try running "composer require symfony/web-link".');
}
} else {
$container->removeDefinition('webpack_encore.preload_assets_event_listener');
}
}
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled, bool $strictMode): Reference
{
$id = $this->getEntrypointServiceId($name);
$arguments = [
$path.'/'.self::ENTRYPOINTS_FILE_NAME,
$cacheEnabled ? new Reference('webpack_encore.cache') : null,
$name,
$strictMode,
];
$definition = new Definition(EntrypointLookup::class, $arguments);
$definition->addTag('kernel.reset', ['method' => 'reset']);
$container->setDefinition($id, $definition);
return new Reference($id);
}
private function getEntrypointServiceId(string $name): string
{
return sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
}
}