Skip to content

Commit 3149c3e

Browse files
committed
first commit
0 parents  commit 3149c3e

File tree

7 files changed

+358
-0
lines changed

7 files changed

+358
-0
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[*]
2+
charset = utf-8
3+
indent_size = 4
4+
indent_style = space
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
8+
[*.{js,ts}]
9+
block_comment_start = /*
10+
block_comment = *
11+
block_comment_end = */
12+
13+
[*.md]
14+
trim_trailing_whitespace = false

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# dependencies
2+
vendor/
3+
4+
# project specific
5+
assets/
6+
manifest.json
7+
test.php

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Jan T. Sott
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Wordpress Vite Assets
2+
3+
> Adds assets from a [Vite](https://vitejs.dev/) manifest to the Wordpress head
4+
5+
[![Packagist](https://flat.badgen.net/packagist/license/idleberg/wordpress-vite-assets)](https://packagist.org/packages/idleberg/wordpress-vite-assets)
6+
[![Packagist](https://flat.badgen.net/packagist/v/idleberg/wordpress-vite-assets)](https://packagist.org/packages/idleberg/wordpress-vite-assets)
7+
8+
## Installation
9+
10+
`composer require idleberg/wordpress-vite-assets`
11+
12+
## Usage
13+
14+
```php
15+
use Idleberg\WordpressViteAssets\WordpressViteAssets;
16+
17+
$viteAssets = new WordpressViteAssets("path/to/manifest.json");
18+
$viteAssets->addAction();
19+
```
20+
21+
### Methods
22+
23+
#### `addAction`
24+
25+
Usage: `addAction(array|string $entries, int $priority = 0)`
26+
27+
Writes tags for entries specified in the manifest to the page header
28+
29+
- script entrypoint
30+
- preloads for imported scripts
31+
- style tags
32+
33+
#### `getScriptTag`
34+
35+
Usage: `getScriptTag(string $fileName)`
36+
37+
Returns the script tag for an entry in the manifest
38+
39+
#### `getStyleTags`
40+
41+
Usage: `getStyleTags(string $fileName)`
42+
43+
Returns the style tags for an entry in the manifest
44+
45+
#### `getPreloadTags`
46+
47+
Usage: `getPreloadTags(string $fileName)`
48+
49+
Returns the preload tags for an entry in the manifest
50+
51+
## License
52+
53+
This work is licensed under [The MIT License](LICENSE)

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "idleberg/wordpress-vite-assets",
3+
"description": "Adds assets from a Vite manifest to the Wordpress head",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"wordpress",
8+
"vite",
9+
"vitejs",
10+
"vite manifest"
11+
],
12+
"minimum-stability": "dev",
13+
"prefer-stable": true,
14+
"autoload": {
15+
"psr-4": {
16+
"Idleberg\\WordpressViteAssets\\": "src"
17+
}
18+
},
19+
"authors": [
20+
{
21+
"name": "Jan T. Sott",
22+
"email": "idleberg@users.noreply.github.com"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.0.0",
27+
"idleberg/vite-manifest": "^0.3.0"
28+
}
29+
}

composer.lock

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WordpressViteAssets.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Jan T. Sott
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to
8+
* deal in the Software without restriction, including without limitation the
9+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10+
* sell copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
* DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
namespace Idleberg\WordpressViteAssets;
26+
27+
use Idleberg\ViteManifest\ViteManifest;
28+
29+
class WordpressViteAssets
30+
{
31+
private $vm;
32+
33+
public function __construct(string $manifestFile)
34+
{
35+
$this->vm = new ViteManifest($manifestFile);
36+
}
37+
38+
/**
39+
* Writes tags for entries specified in the manifest to the page header
40+
*
41+
* @param array|string $entry
42+
* @return void
43+
*/
44+
public function addAction(array|string $entries, int $priority = 0): string
45+
{
46+
if (!function_exists('add_action')) {
47+
throw new \Exception("WordPress function add_action() not found");
48+
}
49+
50+
$entries = is_array($entries) ? $entries : [$entries];
51+
52+
add_action('wp_head', function () {
53+
foreach($entries as $entry) {
54+
$scriptTag = $this->getScriptTag($entry);
55+
56+
if ($scriptTag) {
57+
echo $scriptTag . PHP_EOL;
58+
}
59+
60+
foreach($this->getPreloadTags($entry) as $preloadTag) {
61+
echo $preloadTag . PHP_EOL;
62+
}
63+
64+
foreach($this->getStyleTags($entry) as $styleTag) {
65+
echo $styleTag . PHP_EOL;
66+
}
67+
}
68+
}, $priority);
69+
}
70+
71+
/**
72+
* Returns the script tag for an entry in the manifest
73+
*
74+
* @param string $entry
75+
* @return string
76+
*/
77+
public function getScriptTag(string $entry): string
78+
{
79+
$url = $this->vm->getEntrypoint($entry);
80+
81+
if (!$url) {
82+
return null;
83+
}
84+
85+
return "<script type=\"module\" src=\"{$url['src']}\" crossorigin integrity=\"{$url['integrity']}\"></script>";
86+
}
87+
88+
/**
89+
* Returns the style tags for an entry in the manifest
90+
*
91+
* @param string $entry
92+
* @return array
93+
*/
94+
public function getStyleTags(string $entry): array
95+
{
96+
return array_map(function ($url) {
97+
return "<link rel=\"stylesheet\" href=\"{$url['href']}\" crossorigin integrity=\"{$url['integrity']}\" />";
98+
}, $this->vm->getStyles($entry));
99+
}
100+
101+
/**
102+
* Returns the preload tags for an entry in the manifest
103+
*
104+
* @param string $entry
105+
* @return array
106+
*/
107+
public function getPreloadTags(string $entry): array
108+
{
109+
return array_map(function($import) {
110+
return "<link rel=\"modulepreload\" href=\"{$import['href']}\">";
111+
}, $this->vm->getImports($entry));
112+
}
113+
}

0 commit comments

Comments
 (0)