From 0e5bd1e8d01c4bc7819bc3f58ab1f0db880ff7cc Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 31 Jan 2024 14:48:26 -0500 Subject: [PATCH 01/15] refactor: Clean up run steps --- .github/workflows/code-analysis.yaml | 26 +++++++++++++++++--------- composer.json | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/code-analysis.yaml b/.github/workflows/code-analysis.yaml index d9db16d..48a8979 100644 --- a/.github/workflows/code-analysis.yaml +++ b/.github/workflows/code-analysis.yaml @@ -23,12 +23,20 @@ jobs: name: ${{ matrix.actions.name }} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - # see https://github.com/shivammathur/setup-php - - uses: shivammathur/setup-php@v2 - with: - php-version: 8.0 - coverage: none - # composer install cache - https://github.com/ramsey/composer-install - - uses: "ramsey/composer-install@v2" - - run: ${{ matrix.actions.run }} + - uses: actions/checkout@v4 + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + - name: Setup PHP + id: setup-php + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: 'ctype,curl,dom,iconv,imagick,intl,json,mbstring,openssl,pcre,pdo,reflection,spl,zip' + ini-values: post_max_size=256M, max_execution_time=180, memory_limit=512M + tools: composer:v2 + - name: Install Composer dependencies + run: composer install --no-interaction --no-ansi --no-progress + - run: ${{ matrix.actions.run }} diff --git a/composer.json b/composer.json index e8bf55d..17b9d8c 100755 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "craftcms/rector": "dev-main" }, "scripts": { - "phpstan": "vendor/bin/phpstan --ansi --memory-limit=1G", + "phpstan": "phpstan --ansi --memory-limit=1G", "check-cs": "ecs check --ansi", "fix-cs": "ecs check --fix --ansi" }, From 442e80c51433ae7ad205e43f9f0f450b82b38e35 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 5 Feb 2024 18:17:19 -0500 Subject: [PATCH 02/15] fix: Fixed an issue where the wrong CSS hash would be returned if you were using Vite 3 or earlier ([#80](https://github.com/nystudio107/craft-vite/issues/80)) --- src/services/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/Helper.php b/src/services/Helper.php index e3b2cd5..283a6cd 100755 --- a/src/services/Helper.php +++ b/src/services/Helper.php @@ -109,7 +109,7 @@ public function getCssHash($path): string $moduleFilename = $modulePath['filename']; $moduleHash = substr($moduleFilename, strpos($moduleFilename, '.') + 1); // Vite 5 now uses a `-` to separate the version hash, so account for that as well - if (str_contains($moduleHash, '-')) { + if (str_contains($moduleHash, '-') && substr_count($moduleFilename, '.') === 1) { $moduleHash = substr($moduleHash, strpos($moduleHash, '-') + 1); } From 89b37c45c7848c81763f03eecdce442d5617b57c Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 5 Feb 2024 18:18:05 -0500 Subject: [PATCH 03/15] chore: Version 4.0.9 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f431a6..5f71d9e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## 4.0.9 - UNRELEASED +### Fixed +* Fixed an issue where the wrong CSS hash would be returned if you were using Vite 3 or earlier ([#80](https://github.com/nystudio107/craft-vite/issues/80)) + ## 4.0.8 - 2024.01.30 ### Added * If the `devServer` is running, the `ViteService::fetch()` method will try to use the `devServerInternal` URL first, falling back on the `devServerPublic` so that `craft.vite.inline()` can pull from the `devServer` if it is running ([#22](https://github.com/nystudio107/craft-plugin-vite/issues/22)) diff --git a/composer.json b/composer.json index 17b9d8c..5ab9b52 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-vite", "description": "Allows the use of the Vite.js next generation frontend tooling with Craft CMS", "type": "craft-plugin", - "version": "4.0.8", + "version": "4.0.9", "keywords": [ "craft", "cms", From 2e72e8cdf7bafb2c34b397f136ac4fca3e5520a6 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 8 Feb 2024 16:31:36 -0500 Subject: [PATCH 04/15] refactor: Use `%currentWorkingDirectory%` --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 44554e4..9ad1308 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ includes: - - vendor/craftcms/phpstan/phpstan.neon + - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon parameters: level: 5 From d581d2990a2575485a5bd5d6845819dde414a9ae Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 16 Feb 2024 07:23:00 -0500 Subject: [PATCH 05/15] Vite docs --- docs/docs/index.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/docs/docs/index.md b/docs/docs/index.md index 55e9d27..c24f704 100755 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -911,6 +911,44 @@ So for example: ) }} ``` +## Craft Cloud + +Craft Cloud deploys build artifacts to a CDN, so you'll need to configure the plugin to use the CDN URL: + +```php + \craft\cloud\Helper::artifactUrl('dist/manifest.json'), + 'serverPublic' => \craft\cloud\Helper::artifactUrl('dist/'), +]; +``` + +The `\craft\cloud\Helper::artifactUrl()` function will return a URL like +`https://cdn.craft.com/{uuid}/builds/{uuid}/artifacts/dist/` in a Craft Cloud environment, and `@web/dist/` otherwise. + +If you'd like to use a different path all together when working locally, +you can use the `\craft\cloud\Helper::isCraftCloud()`: + +```php + \craft\cloud\Helper::isCraftCloud() ? \craft\cloud\Helper::artifactUrl('dist/manifest.json') : '@webroot/dist/manifest.json', + 'publicPath' => \craft\cloud\Helper::artifactUrl('dist/'), +]; +``` + +Additionally, your Vite config should have [`base`](https://vitejs.dev/config/shared-options.html#base) set to use the same CDN URL. +In Craft Cloud's build pipeline, this is exposed as an `CRAFT_CLOUD_ARTIFACT_BASE_URL` environment variable. + +```javascript +// vite.config.js +export default ({ command }) => ({ + base: command === 'serve' ? '' : `${process.env.CRAFT_CLOUD_ARTIFACT_BASE_URL || ''}/dist/`, +}) +``` + ## Vite Roadmap Some things to do, and ideas for potential features: From bfe664ee7b5893f3d75e701a95dfde553f016895 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 16 Feb 2024 09:47:12 -0500 Subject: [PATCH 06/15] Clear cache on deploy --- composer.json | 1 + src/Vite.php | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/composer.json b/composer.json index 5ab9b52..afa1227 100755 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "nystudio107/craft-plugin-vite": "^4.0.9" }, "require-dev": { + "craftcms/cloud": "^1.37", "craftcms/ecs": "dev-main", "craftcms/phpstan": "dev-main", "craftcms/rector": "dev-main" diff --git a/src/Vite.php b/src/Vite.php index 9299638..4a80327 100755 --- a/src/Vite.php +++ b/src/Vite.php @@ -13,6 +13,8 @@ use Craft; use craft\base\Model; use craft\base\Plugin; +use craft\cloud\cli\controllers\UpController; +use craft\events\CancelableEvent; use craft\events\RegisterCacheOptionsEvent; use craft\events\TemplateEvent; use craft\utilities\ClearCaches; @@ -146,6 +148,17 @@ static function(TemplateEvent $event) { self::$templateName = $event->template; } ); + // Clears cache after craft cloud/up is run, which Craft Cloud runs on deploy + // Handler: UpController::EVENT_AFTER_UP + if (class_exists(UpController::class)) { + Event::on( + UpController::class, + UpController::EVENT_AFTER_UP, + function (CancelableEvent $event) { + $this->clearAllCaches(); + } + ); + } } /** From 0a5a19c0954439b861bb3490e466a349988157c8 Mon Sep 17 00:00:00 2001 From: August Miller Date: Fri, 16 Feb 2024 15:02:13 -0800 Subject: [PATCH 07/15] Readme cleanup --- docs/docs/index.md | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/docs/docs/index.md b/docs/docs/index.md index c24f704..053ca0c 100755 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -913,34 +913,43 @@ So for example: ## Craft Cloud -Craft Cloud deploys build artifacts to a CDN, so you'll need to configure the plugin to use the CDN URL: +During a [build](https://craftcms.com/knowledge-base/cloud-builds), Craft Cloud deploys static assets to a CDN, so you’ll need to configure the plugin to use the appropriate URLs: ```php \craft\cloud\Helper::artifactUrl('dist/manifest.json'), - 'serverPublic' => \craft\cloud\Helper::artifactUrl('dist/'), + 'manifestPath' => CloudHelper::artifactUrl('dist/.vite/manifest.json'), + 'serverPublic' => CloudHelper::artifactUrl('dist/'), ]; ``` -The `\craft\cloud\Helper::artifactUrl()` function will return a URL like -`https://cdn.craft.com/{uuid}/builds/{uuid}/artifacts/dist/` in a Craft Cloud environment, and `@web/dist/` otherwise. +This helper function returns a CDN URL that includes your project and environment identifiers, like this: + +``` +https://cdn.craft.com/{project-uuid}/builds/{environment-uuid}/artifacts/dist/ +``` + +Outside of Cloud, this behaves as though it were prepended with `@web`. -If you'd like to use a different path all together when working locally, -you can use the `\craft\cloud\Helper::isCraftCloud()`: +If you’d prefer to use an on-disk `manifestPath` when working locally (instead of a URL), the `CloudHelper::isCraftCloud()` function lets you switch based on the environment: ```php \craft\cloud\Helper::isCraftCloud() ? \craft\cloud\Helper::artifactUrl('dist/manifest.json') : '@webroot/dist/manifest.json', - 'publicPath' => \craft\cloud\Helper::artifactUrl('dist/'), + 'manifestPath' => CloudHelper::isCraftCloud() ? CloudHelper::artifactUrl('dist/.vite/manifest.json') : '@webroot/dist/.vite/manifest.json', + 'serverPublic' => CloudHelper::artifactUrl('dist/'), ]; ``` -Additionally, your Vite config should have [`base`](https://vitejs.dev/config/shared-options.html#base) set to use the same CDN URL. -In Craft Cloud's build pipeline, this is exposed as an `CRAFT_CLOUD_ARTIFACT_BASE_URL` environment variable. +Additionally, your Vite config should have [public `base`](https://vitejs.dev/guide/build.html#public-base-path) set to use the same CDN URL. In Craft Cloud’s build pipeline, this is exposed as an [`CRAFT_CLOUD_ARTIFACT_BASE_URL` environment variable](https://craftcms.com/knowledge-base/cloud-builds#build-command): ```javascript // vite.config.js From 807eeed75b314662ce467422239852f3f944c367 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 28 Feb 2024 18:12:05 -0500 Subject: [PATCH 08/15] chore: Delete vestigial `.craftplugin` file --- .craftplugin | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .craftplugin diff --git a/.craftplugin b/.craftplugin deleted file mode 100644 index 4412ee4..0000000 --- a/.craftplugin +++ /dev/null @@ -1 +0,0 @@ -{"pluginName":"Vite","pluginDescription":"Allows the use of the Vite.js next generation frontend tooling with Craft CMS","pluginVersion":"1.0.0","pluginAuthorName":"nystudio107","pluginVendorName":"nystudio107","pluginAuthorUrl":"https://nystudio107.com","pluginAuthorGithub":"nystudio107","codeComments":"","pluginComponents":["services","settings","twigextensions","variables"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"Connector","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"} \ No newline at end of file From 8c1930da6db2dc264ee8f4861139e304ca0f4528 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 28 Feb 2024 18:24:01 -0500 Subject: [PATCH 09/15] chore: Bump deps --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index afa1227..dc548ed 100755 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "nystudio107/craft-plugin-vite": "^4.0.9" }, "require-dev": { - "craftcms/cloud": "^1.37", + "craftcms/cloud": "^1.41.0", "craftcms/ecs": "dev-main", "craftcms/phpstan": "dev-main", "craftcms/rector": "dev-main" From 60e3f2a3c4bbd9c216c4c06ac9d67befc53216ff Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 28 Feb 2024 18:24:09 -0500 Subject: [PATCH 10/15] chore: Version 4.0.9 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f71d9e..6a6b55f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. ## 4.0.9 - UNRELEASED +### Added +* Add documentation for Craft Cloud usage with Vite ([#83](https://github.com/nystudio107/craft-vite/pull/83)) +* Add support for clearing Vite caches in response to Craft Cloud's `UpController::EVENT_AFTER_UP` event ([#83](https://github.com/nystudio107/craft-vite/pull/83)) + ### Fixed * Fixed an issue where the wrong CSS hash would be returned if you were using Vite 3 or earlier ([#80](https://github.com/nystudio107/craft-vite/issues/80)) From a78883f1894bb2aa1fc22167be7cc725da9f0e7f Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 28 Feb 2024 22:36:45 -0500 Subject: [PATCH 11/15] docs: Add callout tip for Asynchronous CSS, clean up docs --- docs/docs/index.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/docs/index.md b/docs/docs/index.md index 053ca0c..851a76f 100755 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -359,8 +359,8 @@ services: expose: - '3000' environment: - - HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80,${DDEV_MAILHOG_PORT}:8025,3001:3000 - - HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80,${DDEV_MAILHOG_HTTPS_PORT}:8025,3000:3000 + - HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80,${DDEV_MAILPIT_PORT}:8025,3001:3000 + - HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80,${DDEV_MAILPIT_HTTPS_PORT}:8025,3000:3000 ``` In your `vite.config.js`, the `server.host` should to be set to `0.0.0.0`, and `server.port` set to `3000`: @@ -501,8 +501,16 @@ JavaScript file via Twig in a `