|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Vite plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Allows the use of the Vite.js next generation frontend tooling with Craft CMS |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\pluginvite\services; |
|
12
|
|
|
|
|
13
|
|
|
use Craft; |
|
14
|
|
|
use craft\helpers\App; |
|
15
|
|
|
use nystudio107\pluginvite\helpers\FileHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
|
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
19
|
|
|
* @package Vite |
|
|
|
|
|
|
20
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
|
|
22
|
|
|
class VitePluginService extends ViteService |
|
23
|
|
|
{ |
|
24
|
|
|
// Constants |
|
25
|
|
|
// ========================================================================= |
|
26
|
|
|
|
|
27
|
|
|
const MANIFEST_FILE_NAME = 'manifest.json'; |
|
28
|
|
|
|
|
29
|
|
|
// Public Properties |
|
30
|
|
|
// ========================================================================= |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
|
|
|
|
|
33
|
|
|
* @var string AssetBundle class name to get the published URLs from |
|
34
|
|
|
*/ |
|
35
|
|
|
public $assetClass; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
|
|
|
|
|
38
|
|
|
* @var string The environment variable to look for in order to enable the devServer; the value doesn't matter, |
|
39
|
|
|
* it just needs to exist |
|
40
|
|
|
*/ |
|
41
|
|
|
public $pluginDevServerEnvVar = 'VITE_PLUGIN_DEVSERVER'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
|
|
|
|
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
*/ |
|
|
|
|
|
|
46
|
|
|
public function init() |
|
47
|
|
|
{ |
|
48
|
|
|
// See if the $pluginDevServerEnvVar env var exists, and if not, don't run off of the dev server |
|
49
|
|
|
$useDevServer = (bool)App::env($this->pluginDevServerEnvVar); |
|
50
|
|
|
if ($useDevServer === false) { |
|
51
|
|
|
$this->useDevServer = false; |
|
52
|
|
|
} |
|
53
|
|
|
parent::init(); |
|
54
|
|
|
// If we're in a plugin, make sure the caches are unique |
|
55
|
|
|
if ($this->assetClass) { |
|
56
|
|
|
$this->cacheKeySuffix = $this->assetClass; |
|
57
|
|
|
} |
|
58
|
|
|
$this->invalidateCaches(); |
|
59
|
|
|
// If we have no asset bundle class, or the dev server is running, don't swap in our `/cpresources/` paths |
|
60
|
|
|
if (!$this->assetClass || $this->devServerRunning()) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
// Map the $manifestPath and $serverPublic to the hashed `/cpresources/` path & URL for our AssetBundle |
|
64
|
|
|
$bundle = new $this->assetClass(); |
|
65
|
|
|
$baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
|
66
|
|
|
$bundle->sourcePath, |
|
67
|
|
|
true |
|
|
|
|
|
|
68
|
|
|
); |
|
69
|
|
|
$this->manifestPath = FileHelper::createUrl($bundle->sourcePath, self::MANIFEST_FILE_NAME); |
|
70
|
|
|
if ($baseAssetsUrl !== false) { |
|
71
|
|
|
$this->serverPublic = $baseAssetsUrl; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|