Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
24 changes: 21 additions & 3 deletions src/services/ViteService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Vite plugin for Craft CMS 3.x
*
Expand Down Expand Up @@ -46,7 +47,12 @@ class ViteService extends Component
/**
* @var string File system path (or URL) to the Vite-built manifest.json
*/
public $manifestPath;
public string $manifestPath = '';

/**
* @var string File system path to the Vite-built assets
*/
public string $distPath = '';

/**
* @var string The public URL to the dev server (what appears in `<script src="">` tags
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand All @@ -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);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/variables/ViteVariableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
}
Expand Down