Skip to content

Commit

Permalink
Merge branch 'release/4.0.12' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Aug 14, 2024
2 parents 7eefbaf + cc7bd81 commit ddb7ace
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Plugin Vite Changelog

## 4.0.12 - 2024.08.13
### Added
* Add a `craft.vite.integrity()` method that will extract the integrity hash (for building a Content Security Policy)
* Added an `includeScriptOnloadHandler` config setting that allows you to disable the adding of an `onload` handler on the `<script>` tags (useful when implementing a Content Security Policy)

### Changed
* Filter out empty attributes so they don't render on the `<script>` tags

### Fixed
* Use `strrpos` instead of `strpos` when attempting to extract a file name without the hash ([#28](https://github.com/nystudio107/craft-plugin-vite/pull/28))

## 4.0.11 - 2024.06.12
### Added
* By default, only load the Vite AssetBundle if the request is a CP request or a preview request. This can be overridden via the `useForAllRequests` VitePluginService property ([#27](https://github.com/nystudio107/craft-plugin-vite/issues/27))
Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MAJOR_VERSION?=4
PLUGINDEV_PROJECT_DIR?=/Users/andrew/webdev/sites/plugindev/cms_v${MAJOR_VERSION}/
VENDOR?=nystudio107
PROJECT_PATH?=${VENDOR}/$(shell basename $(CURDIR))

.PHONY: dev release

# Start up the buildchain dev server
dev:
# Start up the docs dev server
docs:
${MAKE} -C docs/ dev
# Run code quality tools, tests, and build the buildchain & docs in preparation for a release
release: --code-quality --code-tests --buildchain-clean-build --docs-clean-build
# The internal targets used by the dev & release targets
--buildchain-clean-build:
--code-quality:
${MAKE} -C ${PLUGINDEV_PROJECT_DIR} -- ecs check vendor/${PROJECT_PATH}/src --fix
${MAKE} -C ${PLUGINDEV_PROJECT_DIR} -- phpstan analyze -c vendor/${PROJECT_PATH}/phpstan.neon
--code-tests:
--docs-clean-build:
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nystudio107/craft-plugin-vite",
"description": "Plugin Vite is the conduit between Craft CMS plugins and Vite, with manifest.json & HMR support",
"version": "4.0.11",
"version": "4.0.12",
"keywords": [
"craftcms",
"plugin",
Expand Down
22 changes: 20 additions & 2 deletions src/helpers/ManifestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public static function legacyManifestTags(string $path, bool $asyncCss = true, a
/**
* Extract an entry file URL from all of the entries in the manifest
*
* @param string $path
* @return string
*/
public static function extractEntry(string $path): string
Expand Down Expand Up @@ -233,6 +234,23 @@ public static function extractEntry(string $path): string
return '';
}

/**
* Extract an integrity hash for the given $path from the entries in the manifest
*
* @param string $path
* @return string
*/
public static function extractIntegrity(string $path): string
{
foreach (self::$manifest as $entryKey => $entry) {
if (strpos($entryKey, $path) !== false) {
return $entry['integrity'] ?? '';
}
}

return '';
}

/**
* Extract any asset files from all of the entries in the manifest
*
Expand Down Expand Up @@ -320,11 +338,11 @@ protected static function filenameWithoutHash(string $path): string
$pathInfo = pathinfo($path);
$filename = $pathInfo['filename'];
$extension = $pathInfo['extension'];
$hashPos = strpos($filename, '.') ?: strlen($filename);
$hashPos = strrpos($filename, '.') ?: strlen($filename);
$hash = substr($filename, $hashPos);
// Vite 5 now uses a `-` to separate the version hash, so account for that as well
if (empty($hash) && str_contains($filename, '-')) {
$hash = substr($filename, strpos($filename, '-'));
$hash = substr($filename, strrpos($filename, '-'));
}
$filename = str_replace($hash, '', $filename);

Expand Down
35 changes: 35 additions & 0 deletions src/services/ViteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class ViteService extends Component
*/
public bool $includeModulePreloadShim = true;

/**
* @var bool Whether an onload handler should be added to <script> tags to fire a custom event when the script has loaded
*/
public bool $includeScriptOnloadHandler = true;

// Protected Properties
// =========================================================================

Expand Down Expand Up @@ -143,6 +148,11 @@ public function init(): void
*/
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): void
{
// Filter out empty attributes, but preserve boolean values
$preserveBools = fn($value) => is_bool($value) || !empty($value);
$scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools);
$cssTagAttrs = array_filter($cssTagAttrs, $preserveBools);

if ($this->devServerRunning()) {
$this->devServerRegister($path, $scriptTagAttrs);

Expand Down Expand Up @@ -312,6 +322,20 @@ public function entry(string $path): string
return FileHelper::createUrl($this->serverPublic, $entry);
}

/**
* Return the integrity hash (or an empty string if not present) for the given entry
*
* @param string $path
*
* @return string
*/
public function integrity(string $path): string
{
ManifestHelper::fetchManifest($this->manifestPath);

return ManifestHelper::extractIntegrity($path);
}

/**
* Return the URL for the given asset
*
Expand Down Expand Up @@ -408,6 +432,11 @@ public function invalidateCaches(): void
*/
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
{
// Filter out empty attributes, but preserve boolean values
$preserveBools = fn($value) => is_bool($value) || !empty($value);
$scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools);
$cssTagAttrs = array_filter($cssTagAttrs, $preserveBools);

if ($this->devServerRunning()) {
return $this->devServerScript($path, $scriptTagAttrs);
}
Expand Down Expand Up @@ -514,6 +543,9 @@ protected function manifestRegisterTags(array $tags, array $legacyTags): void
$url = FileHelper::createUrl($this->serverPublic, $tag['url']);
switch ($tag['type']) {
case 'file':
if (!$this->includeScriptOnloadHandler) {
unset($tag['options']['onload']);
}
$view->registerJsFile(
$url,
$tag['options'],
Expand Down Expand Up @@ -595,6 +627,9 @@ protected function manifestScriptTags(array $tags, array $legacyTags): array
$url = FileHelper::createUrl($this->serverPublic, $tag['url']);
switch ($tag['type']) {
case 'file':
if (!$this->includeScriptOnloadHandler) {
unset($tag['options']['onload']);
}
$lines[] = HtmlHelper::jsFile($url, $tag['options']);
break;
case 'css':
Expand Down
14 changes: 14 additions & 0 deletions src/variables/ViteVariableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public function entry(string $path): Markup
);
}

/**
* Return the integrity hash (or an empty string if not present) for the given entry
*
* @param string $path
*
* @return string
*/
public function integrity(string $path): string
{
return Template::raw(
$this->viteService->integrity($path)
);
}

/**
* Return the URL for the given asset
*
Expand Down

0 comments on commit ddb7ace

Please sign in to comment.