-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVite.module.php
More file actions
127 lines (107 loc) · 4.04 KB
/
Vite.module.php
File metadata and controls
127 lines (107 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace ProcessWire;
/**
* An Vite adapter for ProcessWire
*
* @package Totoglu Vite
* @author Iskender TOTOGLU <iskender@totoglu.com>
* @link https://totoglu.com
* @copyright Iskender TOTOGLU
* @license https://opensource.org/licenses/MIT
*
* @property string $buildDirectory The path to the build directory.
* @property ?string $hotFile The path to the "hot" file.
* @property ?string $integrity The key to check for integrity hashes within the manifest.
* @property string $manifest The name of the manifest file.
* @property ?string $nonce The key to check for nonce hashes within the manifest.
*/
class Vite extends WireData implements Module
{
public static function getModuleInfo()
{
return [
'title' => 'Vite',
'summary' => __("Integrates Vite.js with ProcessWire for a modern frontend development workflow. This module simplifies asset bundling by automatically generating the correct script and style tags for your Vite-powered assets. It supports Hot Module Replacement (HMR) for instant feedback during development and reads Vite's manifest file in production for versioned/hashed assets, enabling efficient cache-busting.", __FILE__),
'version' => 3,
'icon' => 'code',
'singular' => true,
'autoload' => true,
'requires' => [
'ProcessWire>=3.0.0'
]
];
}
/**
* @inheritDoc
*/
public function __construct()
{
$this->wire('classLoader')->addNamespace('Totoglu\Vite', __DIR__ . '/src');
require_once __DIR__ . '/functions.php';
/** @var Config $config */
$config = $this->wire('config');
$this->set('rootPath', $config->paths->templates);
$this->set('rootUrl', $config->urls->templates);
$this->set('buildDirectory', 'build');
$this->set('hotFile', 'hot');
$this->set('integrity', 'integrity');
$this->set('manifest', 'manifest.json');
$this->set('nonce', null);
}
public function wired()
{
$this->wire('vite', $this);
}
public function init() {}
public function ready() {}
/**
* Recursively copy stub files to destination
*/
protected function copyStubFilesRecursive(string $sourceDir, string $destDir, string $relativePath = ''): void
{
$dir = new \DirectoryIterator($sourceDir . $relativePath);
foreach ($dir as $fileInfo) {
if ($fileInfo->isDot()) continue;
$sourcePath = $fileInfo->getPathname();
$relPath = $relativePath . '/' . $fileInfo->getFilename();
$destPath = $destDir . $relPath;
if ($fileInfo->isDir()) {
// Create directory if it doesn't exist
if (!is_dir($destPath)) {
$this->message("Creating directory: " . $relPath);
wireMkdir($destPath);
}
// Recursively copy files in subdirectory
$this->copyStubFilesRecursive($sourceDir, $destDir, $relPath);
} else {
// Skip if file already exists
if (file_exists($destPath)) {
$this->message("File already exists (skipping): " . $relPath);
continue;
}
// Copy the file
$this->message("Copying file: " . $relPath);
copy($sourcePath, $destPath);
}
}
}
/**
* Copy stub files to site directory
*/
protected function copyStubFiles(): void
{
$stubsDir = __DIR__ . '/stubs';
$siteDir = $this->wire('config')->paths->site;
if (!is_dir($stubsDir)) {
$this->error("Stubs directory not found: $stubsDir");
return;
}
$this->copyStubFilesRecursive($stubsDir, $siteDir);
$this->message("Vite stub files have been copied to site templates directory.");
}
public function ___install()
{
$this->copyStubFiles();
}
}