Skip to content

Commit

Permalink
Package Datastar script
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 30, 2024
1 parent 3b6d7b2 commit 545c73b
Show file tree
Hide file tree
Showing 6 changed files with 2,376 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Release Notes for Spark

## 1.0.0-alpha.2 - Unreleased
## 1.0.0-alpha.2 - 2024-10-30

### Changed

- Spark now requires Craft 5.0.0 or later.
- The Datastar script is now packaged with the plugin, rather than served from a CDN.

## 1.0.0-alpha.1 - 2024-10-28

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This plugin is licensed for free under the MIT License.

## Requirements

This plugin requires [Craft CMS](https://craftcms.com/) 5.4.0 or later.
This plugin requires [Craft CMS](https://craftcms.com/) 5.0.0 or later.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-spark",
"description": "A real-time, template-driven hypermedia framework for Craft.",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.2",
"type": "craft-plugin",
"license": "proprietary",
"require": {
Expand Down
13 changes: 6 additions & 7 deletions src/Spark.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use craft\base\Plugin;
use nystudio107\autocomplete\events\DefineGeneratorValuesEvent;
use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator;
use putyourlightson\spark\assets\DatastarAssetBundle;
use putyourlightson\spark\models\SettingsModel;
use putyourlightson\spark\models\StoreModel;
use putyourlightson\spark\services\ResponseService;
Expand Down Expand Up @@ -74,13 +75,11 @@ private function registerScript(): void
return;
}

if (Craft::$app->getRequest()->getIsSiteRequest()) {
$url = 'https://cdn.jsdelivr.net/npm/@sudodevnull/datastar@' . self::DATASTAR_VERSION;
Craft::$app->getView()->registerJsFile($url, [
'type' => 'module',
'defer' => true,
]);
}
$bundle = Craft::$app->getView()->registerAssetBundle(DatastarAssetBundle::class);

// Register the JS file explicitly so that it will be output when using template caching.
$url = Craft::$app->getView()->getAssetManager()->getAssetUrl($bundle, $bundle->js[0]);
Craft::$app->getView()->registerJsFile($url, $bundle->jsOptions);
}

private function registerAutocompleteEvent(): void
Expand Down
29 changes: 29 additions & 0 deletions src/assets/DatastarAssetBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace putyourlightson\spark\assets;

use craft\web\AssetBundle;
use putyourlightson\spark\Spark;

class DatastarAssetBundle extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = __DIR__ . '/../resources/lib/datastar/' . Spark::DATASTAR_VERSION;

/**
* @inheritdoc
*/
public $js = [
'datastar.js',
];

/**
* @inheritdoc
*/
public $jsOptions = [
'type' => 'module',
'defer' => true,
];
}
Loading

0 comments on commit 545c73b

Please sign in to comment.