-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvite.theme
110 lines (93 loc) · 2.89 KB
/
vite.theme
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
<?php
use Drupal\Core\Site\Settings;
/**
* Implements hook_library_info_alter().
*/
function vite_library_info_alter(&$libraries, $extension) {
if ($extension !== 'vite') {
return;
}
$local = Settings::get('hot_module_replacement');
// Replace library paths so they are ready for either Dev or Prd.
foreach ($libraries as $library => $settings) {
foreach ($settings['css'] as $type => $paths) {
foreach ($paths as $path => $options) {
_vite_replace_library($libraries[$library]['css'][$type], $path, $options);
}
}
foreach ($settings['js'] as $path => $options) {
_vite_replace_library($libraries[$library]['js'], $path, $options);
}
}
// Exit if local development. Everything after will be for production.
if ($local) {
return;
}
// Remove the HMR client library.
unset($libraries['hot-module-replacement']);
// Read the production Manifest file to find CSS files imported via JS files.
$manifest_file = file_get_contents(__DIR__ . '/dist/manifest.json');
$manifest = json_decode($manifest_file);
foreach ($manifest as $key => $data) {
// Ignore files that will be imported via js.
if (preg_match('/^_/', $key)) {
continue;
}
if (!empty($data->css)) {
foreach ($data->css as $css_file) {
// Search through each library.
foreach ($libraries as $library => $settings) {
foreach ($settings['js'] as $path => $options) {
$js_path = 'dist/' . $data->file;
if ($path == $js_path) {
$libraries[$library]['css']['component']["dist/${css_file}"] = [];
}
}
}
}
}
}
}
/**
* Replace an asset path with one that works with Vite.
*
* @param array $library
* The library to be altered.
* @param string $path
* The file path and name.
* @param array $options
* Any settings that were part of the original file's settings.
*/
function _vite_replace_library(array &$library, string $path, array $options): void {
// Ignore external paths
if (preg_match('/^(http|:\/\/)/', $path)) {
return;
}
$local = Settings::get('hot_module_replacement');
$dir = 'dist';
// Remove the old library info.
unset($library[$path]);
if ($local) {
$dir = 'http://localhost:12321';
$options['type'] = 'external';
if (preg_match('/.m?js$/', $path)) {
$options['crossorigin'] = TRUE;
}
}
else {
// Convert .scss files to .css
$path = preg_replace('/.s[ac]ss$/', '.css', $path);
// Strip off all but the filename.
$path = basename($path);
}
// Prepend the local development url.
$path = "${dir}/${path}";
// Add in the new altered library.
$library[$path] = $options;
}
/**
* Implements hook_preprocess_html().
*/
function vite_preprocess_html(&$variables) {
$variables['#attached']['drupalSettings']['path']['themeUrl'] = \Drupal::theme()->getActiveTheme()->getPath();
}