-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
188 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dakujem\Peat; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* A friction reducer used for simple scripts and easy integration, or for configuration troubleshooting. | ||
* | ||
* Do not use this in high-load production environments for performance reasons (see readme for information). | ||
* | ||
* @author Andrej Rypak <[email protected]> | ||
*/ | ||
final class ViteHelper | ||
{ | ||
/** | ||
* This is used to extract assets (HTML tags) for a given entry point (typically `main.js`) from a manifest file | ||
* generated by Vite. The manifest is generated when building a bundle by running `vite build`. | ||
* This can directly be echoed into your HTML template. | ||
* | ||
* This call does not use cache and reads the manifest file directly. | ||
* | ||
* @param string $entryName typically `main.js` | ||
* @param string $manifestFile server path to Vite-generated manifest file | ||
* @param string $assetPathPrefix absolute or relative path from your document root (/public, /www, /web, etc.) to the dir where the manifest file is located | ||
* @param string|null $relativeOffset offset of the current script to the public root; this value comes before $assetPathPrefix and is used when $assetPathPrefix contains a relative path | ||
* @return ViteEntryAsset | ||
*/ | ||
public static function extractAssets( | ||
string $entryName, | ||
string $manifestFile, | ||
string $assetPathPrefix = '', | ||
?string $relativeOffset = null | ||
): ViteEntryAsset { | ||
$bridgeService = new ViteBridge($manifestFile, null, $assetPathPrefix); | ||
$locator = $bridgeService->makePassiveEntryLocator(); | ||
$entry = $locator->entry($entryName, $relativeOffset); | ||
if ($entry === null) { | ||
throw new RuntimeException(sprintf('Vite entry named %s not found in given manifest located at %s.', $entryName, $manifestFile)); | ||
} | ||
return $entry; | ||
} | ||
|
||
/** | ||
* This is used to populate assets (HTML tags) for a given entry point (typically `main.js`) | ||
* when using the development server of Vite (e.g. localhost). | ||
* | ||
* @param string $entryName typically `main.js` | ||
* @param string $developmentServerUrl the URL where Vite server listens, by default this would be http://localhost:5173 | ||
* @return ViteEntryAsset | ||
*/ | ||
public static function populateDevelopmentAssets( | ||
string $entryName, | ||
string $developmentServerUrl | ||
): ViteEntryAsset { | ||
return (new ViteServerLocator($developmentServerUrl))->entry($entryName); | ||
} | ||
} |