diff --git a/src/helpers/FileHelper.php b/src/helpers/FileHelper.php index 8489f4a..e31652e 100644 --- a/src/helpers/FileHelper.php +++ b/src/helpers/FileHelper.php @@ -141,6 +141,19 @@ public static function createUrl(string $url, string $path): string return rtrim($url, '/') . '/' . trim($path, '/'); } + /** + * Combine a path with dist to create a full path + * @param string $url + * @param string $path + * + * @return string + */ + public static function createPath(string $distPath, string $path): string + { + $distPath = (string)Craft::parseEnv($distPath); + return rtrim($distPath, '/') . '/' . trim($path, '/'); + } + /** * Fetch a script file * diff --git a/src/services/ViteService.php b/src/services/ViteService.php index e947abf..204f76b 100755 --- a/src/services/ViteService.php +++ b/src/services/ViteService.php @@ -1,4 +1,5 @@ ` tags @@ -350,10 +356,11 @@ public function publicAsset(string $path): string * Return the URL for the asset from the manifest.json file * * @param string $path + * @param boolean $returnFilePath * * @return string */ - public function manifestAsset(string $path): string + public function manifestAsset(string $path, bool $returnFilePath = false): string { ManifestHelper::fetchManifest($this->manifestPath); $assets = ManifestHelper::extractAssetFiles(); @@ -362,6 +369,9 @@ public function manifestAsset(string $path): string $assetKey = end($assetKeyParts); foreach ($assets as $key => $value) { if ($key === $assetKey) { + if ($returnFilePath) { + return FileHelper::createPath($this->distPath, $path); + } return FileHelper::createUrl($this->serverPublic, $value); } } @@ -370,7 +380,15 @@ public function manifestAsset(string $path): string // manifest, so check there, too $entry = ManifestHelper::extractEntry($path); - return $entry === '' ? '' : FileHelper::createUrl($this->serverPublic, $entry); + if ($entry === '') { + return ''; + } + + if ($returnFilePath) { + return FileHelper::createPath($this->distPath, $entry); + } + + return FileHelper::createUrl($this->serverPublic, $entry); } /** diff --git a/src/variables/ViteVariableTrait.php b/src/variables/ViteVariableTrait.php index 78815da..1360819 100755 --- a/src/variables/ViteVariableTrait.php +++ b/src/variables/ViteVariableTrait.php @@ -103,15 +103,22 @@ public function asset(string $path, bool $public=false): Markup /** * Inline the contents of a local file (via path) or remote file (via URL) in your templates. - * Yii2 aliases and/or environment variables may be used + * Yii2 aliases and/or environment variables may be used. + * If $manifest is true, the local file path will be returned from the manifest * * @param string $pathOrUrl + * @param boolean $manifest * * @return Markup */ - public function inline(string $pathOrUrl): Markup + public function inline(string $pathOrUrl, bool $manifest=false): Markup { - $file = $this->viteService->fetch($pathOrUrl); + if ($manifest) { + $manifestPath = $this->viteService->manifestAsset($pathOrUrl, true); + $file = $this->viteService->fetch($manifestPath); + } else { + $file = $this->viteService->fetch($pathOrUrl); + } if ($file === null) { $file = ''; }