Skip to content

Commit

Permalink
Merge branch 'main' into wandi/absolute-url
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Oct 6, 2023
2 parents d02f853 + 1a7c800 commit fedfd28
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v4.2.0

- add `vite_mode` twig function

## v4.0.1

- fix conditional imports generate modulepreloads for everything
Expand Down
28 changes: 22 additions & 6 deletions src/Asset/EntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class EntrypointRenderer

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

private $hasReturnedViteLegacyScripts = false;

Expand Down Expand Up @@ -142,23 +143,38 @@ public function renderLinks(string $entryName, array $options = [], $buildName =

if ($this->entrypointsLookup->isProd($buildName)) {
foreach ($this->entrypointsLookup->getJavascriptDependencies($entryName, $buildName) as $fileWithHash) {
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
if (false === \in_array($fileWithHash['path'], $this->returnedPreloadedScripts, true)) {
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
$this->returnedPreloadedScripts[] = $fileWithHash['path'];
}
}
}

if ($this->entrypointsLookup->isProd($buildName) && isset($options['preloadDynamicImports']) && true === $options['preloadDynamicImports']) {
foreach ($this->entrypointsLookup->getJavascriptDynamicDependencies($entryName, $buildName) as $fileWithHash) {
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
if (false === \in_array($fileWithHash['path'], $this->returnedPreloadedScripts, true)) {
$content[] = $this->tagRenderer->renderLinkPreload($this->completeURL($fileWithHash['path'], $useAbsoluteUrl), [
'integrity' => $fileWithHash['hash'],
], $buildName);
$this->returnedPreloadedScripts[] = $fileWithHash['path'];
}
}
}

return implode(PHP_EOL, $content);
}

public function getMode(string $buildName = null): ?string
{
if (!$this->entrypointsLookup->hasFile($buildName)) {
return null;
}

return $this->entrypointsLookup->isProd() ? 'prod' : 'dev';
}

public function reset()
{
// resets the state of this service
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/EntrypointsLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getLegacyJSFile($entryName, $buildName = null): string

$legacyEntryName = $entryInfos['entryPoints'][$entryName]['legacy'];

return $entryInfos['entryPoints'][$legacyEntryName]['js'][0];
return $entryInfos['entryPoints'][$legacyEntryName]['js'][0]['path'];
}

private function throwIfEntryIsMissing(string $entryName, string $buildName = null): void
Expand Down
12 changes: 9 additions & 3 deletions src/Twig/EntryFilesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ public function __construct(EntrypointRenderer $entrypointRenderer)
public function getFunctions(): array
{
return [
new TwigFunction('vite_entry_script_tags', [$this, 'renderViteScriptTags'], ['is_safe' => ['html']]),
new TwigFunction('vite_entry_link_tags', [$this, 'renderViteLinkTags'], ['is_safe' => ['html']]),
];
new TwigFunction('vite_entry_script_tags', [$this, 'renderViteScriptTags'], ['is_safe' => ['html']]),
new TwigFunction('vite_entry_link_tags', [$this, 'renderViteLinkTags'], ['is_safe' => ['html']]),
new TwigFunction('vite_mode', [$this, 'getViteMode']),
];
}

public function getViteMode(string $buildName = null): ?string
{
return $this->entrypointRenderer->getMode($buildName);
}

public function renderViteScriptTags(string $entryName, array $options = [], $buildName = null): string
Expand Down

0 comments on commit fedfd28

Please sign in to comment.