Skip to content

Commit

Permalink
remove dependencies Nette\Finder and `Nette\Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
passchn committed Sep 20, 2023
1 parent 6028e07 commit c692190
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 57 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.0",
"cakephp/cakephp": "^4.2.0",
"nette/finder": "^2.5",
"nette/utils": "^v3.0.0"
"cakephp/cakephp": "^4.2.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3",
Expand Down
11 changes: 5 additions & 6 deletions src/Utilities/ManifestRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace ViteHelper\Utilities;

use Nette\Utils\Strings;
use stdClass;

class ManifestRecord
Expand Down Expand Up @@ -75,7 +74,7 @@ public function match(string $needle, string $property = 'src'): bool
{
$field = $this->getChunk($property);

return is_string($field) && Strings::contains($field, $needle);
return is_string($field) && str_contains($field, $needle);
}

/**
Expand Down Expand Up @@ -121,7 +120,7 @@ public function matchAll(array $names, string $property = 'src'): bool
*/
public function isLegacy(): bool
{
return Strings::contains($this->chunk->file, 'legacy');
return str_contains($this->chunk->file, 'legacy');
}

/**
Expand All @@ -131,7 +130,7 @@ public function isLegacy(): bool
*/
public function isPolyfill(): bool
{
return Strings::contains($this->chunk->file, 'polyfills');
return str_contains($this->chunk->file, 'polyfills');
}

/**
Expand All @@ -141,7 +140,7 @@ public function isPolyfill(): bool
*/
public function isJavascript(): bool
{
return Strings::endsWith($this->chunk->file, '.js');
return str_ends_with($this->chunk->file, '.js');
}

/**
Expand All @@ -151,7 +150,7 @@ public function isJavascript(): bool
*/
public function isStylesheet(): bool
{
return Strings::endsWith((string)$this->chunk->file, '.css');
return str_ends_with((string)$this->chunk->file, '.css');
}

/**
Expand Down
94 changes: 50 additions & 44 deletions src/Utilities/ViteManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,68 @@

namespace ViteHelper\Utilities;

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use ViteHelper\Exception\ManifestNotFoundException;

/**
* Reads the information in the manifest.json file provided by ViteJs after running 'vite build'
*/
class ViteManifest
{
/**
* Returns the manifest records as a Collection
*
* @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance
* @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[]
* @throws \ViteHelper\Exception\ManifestNotFoundException
* @internal
*/
public static function getRecords(ViteHelperConfig $config): ManifestRecords
{
$manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST);
/**
* Returns the manifest records as a Collection
*
* @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance
* @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[]
* @throws \ViteHelper\Exception\ManifestNotFoundException
* @internal
*/
public static function getRecords(ViteHelperConfig $config): ManifestRecords
{
$manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST);

try {
$json = FileSystem::read($manifestPath);
if (!is_readable($manifestPath)) {
throw new ManifestNotFoundException(
"No valid manifest.json found at path {$manifestPath}. Did you build your js?",
);
}

$json = str_replace([
"\u0000",
], '', $json);
$json = @file_get_contents($manifestPath);

$manifest = Json::decode($json);
} catch (\Exception $e) {
throw new ManifestNotFoundException(
"No valid manifest.json found at path {$manifestPath}. Did you build your js? Error: {$e->getMessage()}"
);
}
if ($json === false) {
throw new ManifestNotFoundException('Could not parse manifest.json');
}

$manifestArray = [];
foreach (get_object_vars($manifest) as $property => $value) {
$manifestArray[$property] = new ManifestRecord($property, $value, $config);
}
$json = str_replace(
[
"\u0000",
],
'',
$json
);

/**
* Legacy Polyfills must come first.
*/
usort($manifestArray, function ($file) {
/** @var \ViteHelper\Utilities\ManifestRecord $file */
return $file->isPolyfill() ? 0 : 1;
});
$manifest = json_decode($json, false, 512, JSON_THROW_ON_ERROR);

/**
* ES-module scripts must come last.
*/
usort($manifestArray, function ($file) {
/** @var \ViteHelper\Utilities\ManifestRecord $file */
return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0;
});
$manifestArray = [];
foreach (get_object_vars($manifest) as $property => $value) {
$manifestArray[$property] = new ManifestRecord($property, $value, $config);
}

return new ManifestRecords($manifestArray, $manifestPath);
}
/**
* Legacy Polyfills must come first.
*/
usort($manifestArray, function ($file) {
/** @var \ViteHelper\Utilities\ManifestRecord $file */
return $file->isPolyfill() ? 0 : 1;
});

/**
* ES-module scripts must come last.
*/
usort($manifestArray, function ($file) {
/** @var \ViteHelper\Utilities\ManifestRecord $file */
return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0;
});

return new ManifestRecords($manifestArray, $manifestPath);
}
}
14 changes: 10 additions & 4 deletions src/View/Helper/ViteScriptsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Cake\Collection\CollectionInterface;
use Cake\Utility\Text;
use Cake\View\Helper;
use Nette\Utils\Arrays;
use Nette\Utils\Strings;
use ViteHelper\Exception\ConfigurationException;
use ViteHelper\Exception\InvalidArgumentException;
use ViteHelper\Utilities\ConfigDefaults;
Expand Down Expand Up @@ -52,7 +50,7 @@ public function isDev(?ViteHelperConfig $config = null): bool

$needles = $config->read('development.hostNeedles', ConfigDefaults::DEVELOPMENT_HOST_NEEDLES);
foreach ($needles as $needle) {
if (Strings::contains((string)$this->getView()->getRequest()->host(), $needle)) {
if (str_contains((string)$this->getView()->getRequest()->host(), $needle)) {
return true;
}
}
Expand Down Expand Up @@ -241,7 +239,15 @@ private function getFilesForDevelopment(array $options, ViteHelperConfig $config
. 'Be sure to set the ViteHelper.development.' . $configOption . ' config or pass entries to the helper.'
);
}
if (!Arrays::isList($files)) {

$arrayIsList = static function (mixed $value) : bool {
return is_array($value) && (PHP_VERSION_ID < 80100
? !$value || array_keys($value) === range(0, count($value) - 1)
: array_is_list($value)
);
};

if (!$arrayIsList($files)) {
throw new ConfigurationException(sprintf(
'Expected entryPoints to be a List (array with int-keys) with at least one entry, but got %s.',
gettype($files) === 'array' ? 'a relational array' : gettype($files),
Expand Down

0 comments on commit c692190

Please sign in to comment.