Skip to content

Commit

Permalink
add absolute_url to assets, add compatibility to legacy plugin and ha…
Browse files Browse the repository at this point in the history
…shs, add absolute_url config option
  • Loading branch information
lhapaipai committed Oct 6, 2023
1 parent 8a05e92 commit d02f853
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
36 changes: 23 additions & 13 deletions src/Asset/EntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

namespace Pentatrion\ViteBundle\Asset;

use Symfony\Component\Routing\RouterInterface;

class EntrypointRenderer
{
private $entrypointsLookup;
private $tagRenderer;
private $router;
private $useAbsoluteUrl;

private $returnedViteClients = [];
private $returnedReactRefresh = [];

private $hasReturnedViteLegacyScripts = false;

public function __construct(EntrypointsLookup $entrypointsLookup, TagRenderer $tagRenderer, ?\Symfony\Component\Routing\RouterInterface $router = null)
{
public function __construct(
EntrypointsLookup $entrypointsLookup,
TagRenderer $tagRenderer,
$useAbsoluteUrl,
RouterInterface $router = null
) {
$this->entrypointsLookup = $entrypointsLookup;
$this->tagRenderer = $tagRenderer;
$this->useAbsoluteUrl = $useAbsoluteUrl;
$this->router = $router;
}

Expand All @@ -26,6 +34,8 @@ public function renderScripts(string $entryName, array $options = [], $buildName
return '';
}

$useAbsoluteUrl = $this->shouldUseAbsoluteURL($options, $buildName);

$content = [];
$viteServer = $this->entrypointsLookup->getViteServer($buildName);

Expand Down Expand Up @@ -65,7 +75,7 @@ public function renderScripts(string $entryName, array $options = [], $buildName
$content[] = $this->tagRenderer->renderTag('script', [
'nomodule' => true,
'crossorigin' => true,
'src' => $fileWithHash['path'],
'src' => $this->completeURL($fileWithHash['path'], $useAbsoluteUrl),
'id' => 'vite-legacy-polyfill',
]);
}
Expand All @@ -76,7 +86,7 @@ public function renderScripts(string $entryName, array $options = [], $buildName
foreach ($this->entrypointsLookup->getJSFiles($entryName, $buildName) as $fileWithHash) {
$attributes = array_merge([
'type' => 'module',
'src' => $this->shouldUseAbsoluteURL($options, $buildName) ? $this->getAbsoluteURL($fileWithHash['path']) : $fileWithHash['path'],
'src' => $this->completeURL($fileWithHash['path'], $useAbsoluteUrl),
'integrity' => $fileWithHash['hash'],
], $options['attr'] ?? []);
$content[] = $this->tagRenderer->renderScriptFile($attributes, '', $buildName);
Expand All @@ -88,7 +98,7 @@ public function renderScripts(string $entryName, array $options = [], $buildName

$content[] = $this->tagRenderer->renderScriptFile([
'nomodule' => true,
'data-src' => $this->entrypointsLookup->getLegacyJSFile($entryName, $buildName),
'data-src' => $this->completeURL($this->entrypointsLookup->getLegacyJSFile($entryName, $buildName), $useAbsoluteUrl),
'id' => $id,
'crossorigin' => true,
'class' => 'vite-legacy-entry',
Expand All @@ -98,20 +108,20 @@ public function renderScripts(string $entryName, array $options = [], $buildName
return implode(PHP_EOL, $content);
}

private function getAbsoluteURL(string $path)
private function completeURL(string $path, $useAbsoluteUrl = false)
{
if ($this->router === null) {
if (false === $useAbsoluteUrl || null === $this->router) {
return $path;
}

return $this->router->getContext()->getScheme() . '://' . $this->router->getContext()->getHost() . $path;
return $this->router->getContext()->getScheme().'://'.$this->router->getContext()->getHost().$path;
}

private function shouldUseAbsoluteURL(array $options, $buildName)
{
$viteServer = $this->entrypointsLookup->getViteServer($buildName);

return false === $viteServer && isset($options['absoluteURL']) && true === $options['absoluteURL'];
return false === $viteServer && $this->useAbsoluteUrl || (isset($options['absolute_url']) && true === $options['absolute_url']);
}

public function renderLinks(string $entryName, array $options = [], $buildName = null): string
Expand All @@ -120,27 +130,27 @@ public function renderLinks(string $entryName, array $options = [], $buildName =
return '';
}

$viteServer = $this->entrypointsLookup->getViteServer($buildName);
$useAbsoluteUrl = $this->shouldUseAbsoluteURL($options, $buildName);

$content = [];

foreach ($this->entrypointsLookup->getCSSFiles($entryName, $buildName) as $fileWithHash) {
$content[] = $this->tagRenderer->renderLinkStylesheet($this->shouldUseAbsoluteURL($options, $buildName) ? $this->getAbsoluteURL($fileWithHash['path']) : $fileWithHash['path'], array_merge([
$content[] = $this->tagRenderer->renderLinkStylesheet($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), array_merge([
'integrity' => $fileWithHash['hash'],
], $options['attr'] ?? []), $buildName);
}

if ($this->entrypointsLookup->isProd($buildName)) {
foreach ($this->entrypointsLookup->getJavascriptDependencies($entryName, $buildName) as $fileWithHash) {
$content[] = $this->tagRenderer->renderLinkPreload($this->shouldUseAbsoluteURL($options, $buildName) ? $this->getAbsoluteURL($fileWithHash['path']) : $fileWithHash['path'], [
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
}
}

if ($this->entrypointsLookup->isProd($buildName) && isset($options['preloadDynamicImports']) && true === $options['preloadDynamicImports']) {
foreach ($this->entrypointsLookup->getJavascriptDynamicDependencies($entryName, $buildName) as $fileWithHash) {
$content[] = $this->tagRenderer->renderLinkPreload($this->shouldUseAbsoluteURL($options, $buildName) ? $this->getAbsoluteURL($fileWithHash['path']) : $fileWithHash['path'], [
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
}
Expand Down
30 changes: 25 additions & 5 deletions src/Asset/ViteAssetVersionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@
use Symfony\Component\Asset\Exception\AssetNotFoundException;
use Symfony\Component\Asset\Exception\RuntimeException;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
use Symfony\Component\Routing\RouterInterface;

class ViteAssetVersionStrategy implements VersionStrategyInterface
{
private string $publicPath;
private array $builds;
private $useAbsoluteUrl;
private $router;

private string $manifestPath;
private string $entrypointsPath;
private $manifestData = null;
private $entrypointsData = null;
private $manifestData;
private $entrypointsData;
private ?array $build = null;
private bool $strictMode;

public function __construct(string $publicPath, array $builds, string $defaultBuildName, bool $strictMode = true)
{
public function __construct(
string $publicPath,
array $builds,
string $defaultBuildName,
$useAbsoluteUrl,
RouterInterface $router = null,
bool $strictMode = true,
) {
$this->publicPath = $publicPath;
$this->builds = $builds;
$this->strictMode = $strictMode;
$this->useAbsoluteUrl = $useAbsoluteUrl;
$this->router = $router;

$this->setBuildName($defaultBuildName);

Expand Down Expand Up @@ -53,6 +64,15 @@ public function applyVersion(string $path): string
return $this->getassetsPath($path) ?: $path;
}

private function completeURL(string $path)
{
if (false === $this->useAbsoluteUrl || null === $this->router) {
return $path;
}

return $this->router->getContext()->getScheme().'://'.$this->router->getContext()->getHost().$path;
}

private function getassetsPath(string $path): ?string
{
if (null === $this->manifestData) {
Expand All @@ -78,7 +98,7 @@ private function getassetsPath(string $path): ?string

if (false !== $this->manifestData) {
if (isset($this->manifestData[$path])) {
return $this->build['base'].$this->manifestData[$path]['file'];
return $this->completeURL($this->build['base'].$this->manifestData[$path]['file']);
}
} else {
return $this->entrypointsData['viteServer']['origin'].$this->entrypointsData['viteServer']['base'].$path;
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Throw exception when entry is not present in the entrypoints file')
->defaultValue(false)
->end()
->booleanNode('absolute_url')
->info('Prepend the rendered link and script tags with an absolute URL.')
->defaultValue(false)
->end()
->arrayNode('script_attributes')
->info('Key/value pair of attributes to render on all script tags')
->example('{ defer: true, referrerpolicy: "origin" }')
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/PentatrionViteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public function load(array $configs, ContainerBuilder $container): void
}

$container->setParameter('pentatrion_vite.public_directory', self::preparePublicDirectory($config['public_directory']));

$container->setParameter('pentatrion_vite.default_build', $defaultBuild);
$container->setParameter('pentatrion_vite.builds', $builds);

$container->setParameter('pentatrion_vite.absolute_url', $config['absolute_url']);
$container->setParameter('pentatrion_vite.proxy_origin', $config['proxy_origin']);
$container->setParameter('pentatrion_vite.throw_on_missing_entry', $config['throw_on_missing_entry']);

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
arguments:
- "@vite.entrypoints_lookup"
- "@vite.tag_renderer"
- "%pentatrion_vite.absolute_url%"
- "@?router"
public: true

Expand Down Expand Up @@ -55,4 +56,6 @@ services:
- "%kernel.project_dir%%pentatrion_vite.public_directory%"
- "%pentatrion_vite.builds%"
- "%pentatrion_vite.default_build%"
- "%pentatrion_vite.absolute_url%"
- "@?router"
- true

0 comments on commit d02f853

Please sign in to comment.