Skip to content

Commit

Permalink
add assets inside entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Jul 18, 2022
1 parent 8179fca commit 5a1cf87
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 17 deletions.
4 changes: 2 additions & 2 deletions install/package.json
Expand Up @@ -6,7 +6,7 @@
},
"license": "UNLICENSED",
"devDependencies": {
"vite": "~2.9",
"vite-plugin-symfony": "^0.3.0"
"vite": "^3.0",
"vite-plugin-symfony": "^0.4.0"
}
}
16 changes: 3 additions & 13 deletions src/Asset/ViteAssetVersionStrategy.php
Expand Up @@ -11,18 +11,16 @@ class ViteAssetVersionStrategy implements VersionStrategyInterface
{
private string $entrypointsPath;
private array $entrypointsData;
private string $assetsPath;
private ?array $assetsData = null;
private bool $strictMode;

/**
* @param string $entrypointsPath Absolute path to the entrypoints file
* @param bool $strictMode Throws an exception for unknown paths
*/
public function __construct(string $entrypointsPath, string $assetsPath, bool $strictMode = true)
public function __construct(string $entrypointsPath, bool $strictMode = true)
{
$this->entrypointsPath = $entrypointsPath;
$this->assetsPath = $assetsPath;
$this->strictMode = $strictMode;

if (($scheme = parse_url($this->entrypointsPath, \PHP_URL_SCHEME)) && 0 === strpos($scheme, 'http')) {
Expand Down Expand Up @@ -57,19 +55,11 @@ private function getassetsPath(string $path): ?string
} catch (\JsonException $e) {
throw new RuntimeException(sprintf('Error parsing JSON from entrypoints file "%s": ', $this->entrypointsPath) . $e->getMessage(), previous: $e);
}

if (is_file($this->assetsPath)) {
try {
$this->assetsData = json_decode(file_get_contents($this->assetsPath), true, flags: \JSON_THROW_ON_ERROR);
} catch (Exception $e) {
throw new RuntimeException(sprintf('Error parsing JSON from assets file "%s": ', $this->assetsPath) . $e->getMessage(), previous: $e);
}
}
}

if ($this->entrypointsData['isProd']) {
if (isset($this->assetsData[$path])) {
return $this->assetsData[$path];
if (isset($this->entrypointsData['assets'][$path])) {
return $this->entrypointsData['assets'][$path];
}
} else {
return $this->entrypointsData['viteServer']['origin'] . $this->entrypointsData['viteServer']['base'] . $path;
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Expand Up @@ -20,7 +20,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->addDefaultsIfNotSet()
->children()
->scalarNode('host')->defaultValue('localhost')->end()
->integerNode('port')->defaultValue(3000)->end()
->integerNode('port')->defaultValue(5173)->end()
->booleanNode('https')->defaultFalse()->end()
->end()
->end()
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services.yaml
Expand Up @@ -39,5 +39,4 @@ services:
Pentatrion\ViteBundle\Asset\ViteAssetVersionStrategy:
arguments:
- "%kernel.project_dir%/public%pentatrion_vite.base%entrypoints.json"
- "%kernel.project_dir%/public%pentatrion_vite.base%assets.json"
- true

0 comments on commit 5a1cf87

Please sign in to comment.