Skip to content

Commit

Permalink
Merge branch 'release/1.0.5' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed May 14, 2021
2 parents 06db2b2 + ca65efc commit 43a7dc6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file.

## 1.0.5 - 2021.05.14
### Added
* Moved the live reload through Twig errors to the ViteService so that plugins can get it too
* Added `.inline()` to allow for inlining of local or remote files in your templates, with a caching layer

### Changed
* Use `registerJsFile()` instead of `registerScript()`
* Make the cache last for 30 seconds with `devMode` on
* Refactored to `ViteVariableInterface` & `ViteVariableTrait`

## 1.0.4 - 2021.05.08
### Added
* Added the `devServerInternal` setting back in, along with `checkDevServer` for people who want the fallback behavior (https://github.com/nystudio107/craft-vite/issues/2)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "1.0.4",
"version": "1.0.5",
"keywords": [
"craft",
"cms",
Expand All @@ -23,7 +23,7 @@
],
"require": {
"craftcms/cms": "^3.0.0",
"nystudio107/craft-plugin-vite": "^1.0.3"
"nystudio107/craft-plugin-vite": "^1.0.4"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CONTAINER?=$(shell basename $(dir $(CURDIR)))-docs
DOCKERRUN=docker container run \
--name ${CONTAINER} \
--rm \
-p 3000:3000 \
-p 3002:3002 \
-t \
-v `pwd`:/app \
${CONTAINER}:${TAG}
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
lang: 'en-US',
themeConfig: {
repo: 'nystudio107/craft-vite',
docsDir: 'docs',
docsDir: 'docs/docs',
docsBranch: 'v1',
algolia: {
apiKey: '',
indexName: 'craft-vite'
Expand Down
18 changes: 18 additions & 0 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ This works exactly the way the `.script()` function works, but instead of output

This is primarily useful in plugins that must exist inside of the CP, or other things that leverage the Yii2 AssetBundles and dependencies.

### The `.inline()` function

The Vite plugin also includes a `.inline()` function that inlines the contents of a local file (via path) or remote file (via URL) in your templates.

Yii2 aliases and/or environment variables may be used, and a caching layer is used so that remote files will be kept in the cache until it is cleared, for performance reasons.

URL example:

```twig
{{ craft.vite.inline("https://example.com/my-file.txt") }}
```

Path example:

```twig
{{ craft.vite.inline("@webroot/my-file.txt") }}
```

### Other Options

The `.script()` and `.register()` functions accept additional options as well:
Expand Down
10 changes: 10 additions & 0 deletions docs/docs/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
server: {
host: '0.0.0.0',
port: 3002,
strictPort: true,
}
});
40 changes: 1 addition & 39 deletions src/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
namespace nystudio107\vite;

use nystudio107\vite\models\Settings;
use nystudio107\vite\variables\ViteVariable;

use nystudio107\pluginvite\services\ViteService;
use nystudio107\pluginvite\variables\ViteVariable;

use Craft;
use craft\base\Plugin;
use craft\events\RegisterCacheOptionsEvent;
use craft\utilities\ClearCaches;
use craft\web\Application;
use craft\web\twig\variables\CraftVariable;
use craft\web\View;

use yii\base\Event;

Expand Down Expand Up @@ -129,13 +127,6 @@ function (RegisterCacheOptionsEvent $event) {
);
}
);
// delay attaching event handler to the view component after it is fully configured
$app = Craft::$app;
if ($app->getConfig()->getGeneral()->devMode) {
$app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
});
}
// Log that the plugin has loaded
Craft::info(
Craft::t(
Expand All @@ -159,35 +150,6 @@ public function clearAllCaches()
// Protected Methods
// =========================================================================

/**
* Inject the error entry point JavaScript for auto-reloading of Twig error
* pages
*/
protected function injectErrorEntry()
{
$response = Craft::$app->getResponse();
if ($response->isServerError || $response->isClientError) {
$settings = $this->getSettings();
/** @var Settings $settings */
if (!empty($settings->errorEntry) && $settings->useDevServer) {
try {
$errorEntry = $settings->errorEntry;
if (is_string($errorEntry)) {
$errorEntry = [$errorEntry];
}
foreach ($errorEntry as $entry) {
$tag = $this->vite->script($entry);
if ($tag !== null) {
echo $tag;
}
}
} catch (\Throwable $e) {
// That's okay, Vite will have already logged the error
}
}
}
}

/**
* Returns the custom Control Panel cache options.
*
Expand Down
24 changes: 24 additions & 0 deletions src/variables/ViteVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Vite plugin for Craft CMS 3.x
*
* Allows the use of the Vite.js next generation frontend tooling with Craft CMS
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2021 nystudio107
*/

namespace nystudio107\vite\variables;

use nystudio107\pluginvite\variables\ViteVariableInterface;
use nystudio107\pluginvite\variables\ViteVariableTrait;

/**
* @author nystudio107
* @package Vite
* @since 1.0.5
*/
class ViteVariable implements ViteVariableInterface
{
use ViteVariableTrait;
}

0 comments on commit 43a7dc6

Please sign in to comment.