forked from andrefelipe/vite-php-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c184578
commit a6189e0
Showing
17 changed files
with
1,443 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
public/assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"main.js": { | ||
"file": "assets/main.13a1cd52.js", | ||
"src": "main.js", | ||
"isEntry": true, | ||
"imports": [ | ||
"_vendor.7580ca5a.js" | ||
], | ||
"css": [ | ||
"assets/main.5924f01a.css" | ||
], | ||
"assets": [ | ||
"assets/logo.3b714202.png" | ||
] | ||
}, | ||
"_vendor.7580ca5a.js": { | ||
"file": "assets/vendor.7580ca5a.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,114 @@ | ||
<?php | ||
// Helpers here serve as example. Change to suit your needs. | ||
|
||
// For a real-world example check here https://github.com/wp-bond/bond/blob/master/src/Tooling/Vite.php | ||
// For a real-world example check here: | ||
// https://github.com/wp-bond/bond/blob/master/src/Tooling/Vite.php | ||
// https://github.com/wp-bond/boilerplate/tree/master/app/themes/boilerplate | ||
|
||
// on the links above there is also example for @vitejs/plugin-legacy | ||
|
||
|
||
// Some dev/prod mechanism would exist in your project | ||
// Handling manualy here, change to test both cases | ||
define('IS_DEVELOPMENT', true); | ||
|
||
|
||
// Vite Client that must be loaded during development | ||
function viteClient(): string | ||
function vite($entry): string | ||
{ | ||
// not required on production | ||
if (!IS_DEVELOPMENT) { | ||
return jsTag($entry) | ||
. jsPreloadImports($entry) | ||
. cssTag($entry); | ||
} | ||
|
||
|
||
// Helpers to print tags | ||
|
||
function jsTag(string $entry): string | ||
{ | ||
$url = IS_DEVELOPMENT | ||
? 'http://localhost:3000/' . $entry | ||
: assetUrl($entry); | ||
|
||
if (!$url) { | ||
return ''; | ||
} | ||
return '<script type="module">import "http://localhost:3000/vite/client"</script>'; | ||
return '<script type="module" crossorigin src="' | ||
. $url | ||
. '"></script>'; | ||
} | ||
|
||
function jsPreloadImports(string $entry): string | ||
{ | ||
if (IS_DEVELOPMENT) { | ||
return ''; | ||
} | ||
|
||
// Helper to output style tag | ||
function viteCss(string $name): string | ||
$res = ''; | ||
foreach (importsUrls($entry) as $url) { | ||
$res .= '<link rel="modulepreload" href="' | ||
. $url | ||
. '">'; | ||
} | ||
return $res; | ||
} | ||
|
||
function cssTag(string $entry): string | ||
{ | ||
// not needed on dev, it's inject by Vite | ||
if (IS_DEVELOPMENT) { | ||
return ''; | ||
} | ||
return '<link rel="stylesheet" href="' . | ||
viteAsset($name . '.css') | ||
. '">'; | ||
|
||
$tags = ''; | ||
foreach (cssUrls($entry) as $url) { | ||
$tags .= '<link rel="stylesheet" href="' | ||
. $url | ||
. '">'; | ||
} | ||
return $tags; | ||
} | ||
|
||
// Helper to output the script tag | ||
function viteJs(string $name): string | ||
|
||
// Helpers to locate files | ||
|
||
function getManifest(): array | ||
{ | ||
return '<script type="module" src="' . | ||
viteAsset($name . '.js') | ||
. '"></script>'; | ||
$content = file_get_contents(__DIR__ . '/dist/manifest.json'); | ||
|
||
return json_decode($content, true); | ||
} | ||
|
||
// Helper to locate files | ||
function viteAsset(string $filename): string | ||
function assetUrl(string $entry): string | ||
{ | ||
// Let Vite handle during dev | ||
if (IS_DEVELOPMENT) { | ||
return 'http://localhost:3000/src/' . $filename; | ||
$manifest = getManifest(); | ||
|
||
return isset($manifest[$entry]) | ||
? '/dist/' . $manifest[$entry]['file'] | ||
: ''; | ||
} | ||
|
||
function importsUrls(string $entry): array | ||
{ | ||
$urls = []; | ||
$manifest = getManifest(); | ||
|
||
if (!empty($manifest[$entry]['imports'])) { | ||
foreach ($manifest[$entry]['imports'] as $imports) { | ||
$urls[] = '/dist/' . $manifest[$imports]['file']; | ||
} | ||
} | ||
return $urls; | ||
} | ||
|
||
// Locate hashed files in production | ||
$manifest = json_decode(file_get_contents( | ||
__DIR__ . '/dist/_assets/manifest.json' | ||
), true); | ||
function cssUrls(string $entry): array | ||
{ | ||
$urls = []; | ||
$manifest = getManifest(); | ||
|
||
return '/dist/_assets/' | ||
. ($manifest[$filename] ?? $filename); | ||
if (!empty($manifest[$entry]['css'])) { | ||
foreach ($manifest[$entry]['css'] as $file) { | ||
$urls[] = '/dist/' . $file; | ||
} | ||
} | ||
return $urls; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.