diff --git a/.github/workflows/code-analysis.yaml b/.github/workflows/code-analysis.yaml new file mode 100644 index 0000000..b571cde --- /dev/null +++ b/.github/workflows/code-analysis.yaml @@ -0,0 +1,34 @@ +name: Code Analysis + +on: + pull_request: null + push: + branches: + - develop-v5 + workflow_dispatch: +permissions: + contents: read +jobs: + code_analysis: + strategy: + fail-fast: false + matrix: + actions: + - + name: 'PHPStan' + run: composer phpstan + - + name: 'Coding Standards' + run: composer fix-cs + 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.2 + coverage: none + # composer install cache - https://github.com/ramsey/composer-install + - uses: "ramsey/composer-install@v2" + - run: ${{ matrix.actions.run }} diff --git a/CHANGELOG.md b/CHANGELOG.md index b87a7a2..99dcbd0 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to this project will be documented in this file. +## 5.0.0-beta.2 - 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)) +* Add `phpstan` and `ecs` code linting +* Add `code-analysis.yaml` GitHub action + +### Changed +* PHPstan code cleanup +* ECS code cleanup + ## 5.0.0-beta.1 - 2024.01.21 ### Added - Initial beta release diff --git a/composer.json b/composer.json index 99b778b..fa0cc02 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": "5.0.0-beta.1", + "version": "5.0.0-beta.2", "keywords": [ "craft", "cms", @@ -24,7 +24,17 @@ ], "require": { "craftcms/cms": "^5.0.0-alpha.1", - "nystudio107/craft-plugin-vite": "^5.0.0-beta.1" + "nystudio107/craft-plugin-vite": "^5.0.0-beta.2" + }, + "require-dev": { + "craftcms/ecs": "dev-main", + "craftcms/phpstan": "dev-main", + "craftcms/rector": "dev-main" + }, + "scripts": { + "phpstan": "vendor/bin/phpstan --ansi --memory-limit=1G", + "check-cs": "ecs check --ansi", + "fix-cs": "ecs check --fix --ansi" }, "config": { "allow-plugins": { diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..1ca291d --- /dev/null +++ b/docs/README.md @@ -0,0 +1,74 @@ +# `vite` docs + +This buildchain is a self-contained build system for the `vite` documentation. + +## Overview + +The buildchain uses [VitePress](https://vitepress.dev/) via a Docker container to facilitate writing the docs as [markdown](https://vitepress.dev/guide/markdown), linting them via [textlint](https://textlint.github.io/), building them as HTML files with bundled assets, and publishing them automatically via a [GitHub action](https://docs.github.com/en/actions). + +It also uses a [Rollup](https://rollupjs.org/) [sitemap plugin](https://github.com/aminnairi/rollup-plugin-sitemap) to generate a `sitemap.xml` for the generated docs. + +The markdown sources for the docs and assets are in the `docs/docs/` directory. + +The built distribution docs are created via the `build-and-deploy-docs.yaml` + +## Prerequisites + +To run the buildchain for development purposes: + +- You must have [Docker Desktop](https://www.docker.com/products/docker-desktop/) (or the equivalent) installed + +## Commands + +This buildchain uses `make` as an interface to the buildchain. The following commands are available from the `buildchain/` directory: + +- `make image-build` - Build the Docker image & run `npm install`. This command must be run once before using the Docker container. +- `make dev` - Start Vite HMR dev server while writing/editing the docs. Click on the link displayed in the terminal to open the docs up +- `make lint` - Run `textlint` on the docs, reporting on any errors and warnings +- `make fix` - Run `textlint` on the docs, automatically fixing any errors, and reporting any warnings +- `make clean` - Remove `node_modules/` and `package-lock.json` to start clean (need to run `make image-build` after doing this, see below) +- `make npm XXX` - Run an `npm` command inside the container, e.g.: `make npm run lint` or `make npm install` +- `make ssh` - Open up a shell session into the buildchain Docker container +- `make build` - Do a local distribution build of the docs; normally not needed since they are built & deployed via GitHub action + +## Docs versioning + +Each major version of the plugin corresponds to a major version of Craft. + +Each major version of the plugin has separate documentation that needs to be updated when changes span plugin versions. + +The latest version of the docs that correspond to the latest version of the plugin is always the root of the docs tree, with older versions appearing in sub-directories: + +``` +│ index.html +├── v4 +│ └── index.html +├── v3 +│ └── index.html +``` + +The docs are entirely separate, but linked to eachother via a version menu, configured in the `docs/docs/.vitepress/config.ts` file. + +## Algolia Docsearch + +The docs uses [Algolia Docsearch](https://docsearch.algolia.com/) to index them, and allow for easy searching via a search field with auto-complete. + +Algolia Docsearch is configured in the `docs/docs/.vitepress/config.ts` file. + +## textlint + +The buildchain uses [textlint](https://textlint.github.io/) to automatically fix errors on build, and also issue writing style warnings. + +`textlint` automatically uses any rules added to the `docs/package.json` file, which can be overridden or customized via the `docs/.textlintrc.js` file. + +See the [textlint docs](https://textlint.github.io/docs/getting-started.html) for details. + +## Overriding environment variables + +The `Makefile` contains sane defaults for most things, but you can override them via environment variables if you need to. + +For instance, if you want to change the `DOCS_DEST` environment variable to change where `make build` builds the docs locally, you can set it before running any buildchain `make` commands: +```bash +env DOCS_DEST=../path/to/some/dir make build +``` +...or use any other method for [setting environment variables](https://www.twilio.com/blog/how-to-set-environment-variables.html). This environment variable needs to be set in the shell where you run the buildchain's various `make` commands from, so setting it in your project's `.env` file won't work. diff --git a/docs/docs/index.md b/docs/docs/index.md index 6eb2854..19b4938 100755 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -32,6 +32,8 @@ To install the plugin, follow these instructions. 3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Vite. +4. Install Vite. Vite's *[Getting Started Guide](https://vitejs.dev/guide/)* provides various installation options. + ## Vite Overview Vite is a bridge between Craft CMS/Twig and the next generation frontend build tool [Vite.js](https://vitejs.dev/) @@ -126,6 +128,15 @@ hosted on a CDN or such. ### Configuring Vite.js +### Vite 5.0 or later + +As of Vite 5.0 or later, there's been a [change to where the `manifest.json`](https://vitejs.dev/guide/migration#manifest-files-are-now-generated-in-vite-directory-by-default) is generated by default. + +It is now placed inside a `.vite/` directory by default. You can accomodate this in either of the following ways: + +* Change the `manifest` setting in your `vite.config.js` file to `manifest.json` (it can not either be a `bool` or a file path `string`) +* Change the `manifestPath` setting in your `config/vite.php` file to `'@webroot/dist/.vite/manifest.json'` + #### Basic Config Here’s a basic `vite.config.js` for use as a [Vite config](https://vitejs.dev/config/): @@ -138,7 +149,7 @@ export default ({command}) => ({ outDir: '../cms/web/dist/', rollupOptions: { input: { - app: './src/js/app.ts', + app: 'src/js/app.ts', } }, }, @@ -168,7 +179,7 @@ export default ({command}) => ({ outDir: '../cms/web/dist/', rollupOptions: { input: { - app: './src/js/app.ts', + app: 'src/js/app.ts', } }, }, @@ -202,7 +213,7 @@ export default ({command}) => ({ outDir: '../cms/web/dist/', rollupOptions: { input: { - app: './src/js/app.ts', + app: 'src/js/app.ts', } }, }, @@ -254,7 +265,7 @@ export default ({command}) => ({ outDir: '../cms/web/dist/', rollupOptions: { input: { - app: './src/js/app.ts', + app: 'src/js/app.ts', } }, }, diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..85860c2 --- /dev/null +++ b/ecs.php @@ -0,0 +1,13 @@ +paths([ + __DIR__ . '/src', + __FILE__, + ]); + $ecsConfig->parallel(); + $ecsConfig->sets([SetList::CRAFT_CMS_4]); +}; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..44554e4 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +includes: + - vendor/craftcms/phpstan/phpstan.neon + +parameters: + level: 5 + paths: + - src diff --git a/src/Vite.php b/src/Vite.php index f66425c..9299638 100755 --- a/src/Vite.php +++ b/src/Vite.php @@ -112,7 +112,7 @@ protected function installEventListeners(): void Event::on( CraftVariable::class, CraftVariable::EVENT_INIT, - function (Event $event) { + function(Event $event) { /** @var CraftVariable $variable */ $variable = $event->sender; $variable->set('vite', [ @@ -125,7 +125,7 @@ function (Event $event) { Event::on( ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, - function (RegisterCacheOptionsEvent $event) { + function(RegisterCacheOptionsEvent $event) { Craft::debug( 'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS', __METHOD__ @@ -142,11 +142,10 @@ function (RegisterCacheOptionsEvent $event) { Event::on( View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, - static function (TemplateEvent $event) { + static function(TemplateEvent $event) { self::$templateName = $event->template; } ); - } /** diff --git a/src/models/Settings.php b/src/models/Settings.php index 03463fe..bfe89da 100755 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -101,7 +101,7 @@ public function rules(): array 'includeReactRefreshShim', 'includeModulePreloadShim', ], - 'boolean' + 'boolean', ], [ [ @@ -111,13 +111,13 @@ public function rules(): array 'cacheKeySuffix', 'devServerInternal', ], - 'string' + 'string', ], [ [ 'errorEntry', ], - 'string' + 'string', ], ]; } diff --git a/src/services/ServicesTrait.php b/src/services/ServicesTrait.php index 2f000c1..a61b897 100755 --- a/src/services/ServicesTrait.php +++ b/src/services/ServicesTrait.php @@ -36,8 +36,8 @@ public static function config(): array return [ 'components' => [ 'helper' => HelperService::class, - 'vite' => PluginConfigHelper::serviceDefinitionFromConfig('vite', ViteService::class) - ] + 'vite' => PluginConfigHelper::serviceDefinitionFromConfig('vite', ViteService::class), + ], ]; } diff --git a/src/translations/en/vite.php b/src/translations/en/vite.php index 94cd6df..490dddc 100755 --- a/src/translations/en/vite.php +++ b/src/translations/en/vite.php @@ -15,5 +15,5 @@ */ return [ 'Vite Cache' => 'Vite Cache', - '{name} plugin loaded' => '{name} plugin loaded' + '{name} plugin loaded' => '{name} plugin loaded', ]; diff --git a/src/variables/ViteVariable.php b/src/variables/ViteVariable.php index f8dc344..584007d 100755 --- a/src/variables/ViteVariable.php +++ b/src/variables/ViteVariable.php @@ -39,7 +39,7 @@ class ViteVariable implements ViteVariableInterface public function includeCriticalCssTags(?string $name = null, array $attributes = []): Markup { return Template::raw( - Vite::$plugin->helper->getCriticalCssTags($name, $attributes) ?? '' + Vite::$plugin->helper->getCriticalCssTags($name, $attributes) ); } @@ -55,7 +55,7 @@ public function includeCriticalCssTags(?string $name = null, array $attributes = public function getCssInlineTags(string $path, array $attributes = []): string { return Template::raw( - Vite::$plugin->helper->getCssInlineTags($path, $attributes) ?? '' + Vite::$plugin->helper->getCssInlineTags($path, $attributes) ); } @@ -68,7 +68,7 @@ public function getCssInlineTags(string $path, array $attributes = []): string public function getCssHash($path): Markup { return Template::raw( - Vite::$plugin->helper->getCssHash($path) ?? '' + Vite::$plugin->helper->getCssHash($path) ); } }